To scroll the webpage, I use the JavaScriptExecutor in Selenium.
For scrolling down, I run: window.scrollBy(0, 500) — this scrolls down by 500 pixels.
For scrolling up, I run: window.scrollBy(0, -500).
If I want to scroll to a specific element, I use: element.scrollIntoView(true).
When we have standard <select> HTML dropdown, I use the Select class.
I create an object: Select select = new Select(dropdownElement);
I can select using:
selectByVisibleText()selectByValue()selectByIndex()<select> tag. To check this, I again use the Select class.
After creating the dropdown object, I call isMultiple() method.
If it returns true, it’s a multi-select dropdown.
If it returns false, then only one option can be selected.
I use select.getOptions() method, which returns a list of all dropdown options.
This list contains WebElements, and I can loop through them.
Using getText(), I can print or validate each option.
To capture screenshots, I use the TakesScreenshot interface.
First, I typecast WebDriver: (TakesScreenshot) driver.
Then I call getScreenshotAs(OutputType.FILE) to capture the screenshot.
I save the file using something like FileUtils.copyFile() method.
Screenshots are extremely helpful during failures or for test reports.
Keys.ENTER, Keys.TAB, Keys.BACK_SPACE, etc.keyDown() and keyUp() methods.driver.getWindowHandles() to get all window IDs.driver.switchTo().window(windowId).driver.getWindowHandle().driver.switchTo().window(parentId) to return.Waits are used to pause execution until a condition is met. It has 3 types: Implicit Wait, Explicit Wait, and Fluent Wait. It helps to avoid NoSuchElementException for slow-loading elements and improves test stability and performance.
NoSuchElementException.elementToBeClickable(By locator) – waits for element to be clickable.visibilityOfElementLocated(By locator) – waits until element is visible.alertIsPresent() – waits for alert pop-up to appear.titleContains("text") – waits until page title contains specific text.presenceOfElementLocated(By locator) – waits for element presence in DOM.try-catch blocks to gracefully handle failures during test execution.NoSuchElementException, TimeoutException, etc., to handle known issues.NoSuchElementException – element not found.StaleElementReferenceException – element not attached to DOM.TimeoutException – condition not met within wait time.ElementClickInterceptedException – click blocked by another element.ElementNotInteractableException – element present but not clickable.WebDriverWait) to wait until the element is visible/clickable.isDisplayed() or isPresent() checks before actions.Using the isDisplayed method, we can check if an element is visible on a web page.
Using the isEnabled method, we can check if an element is enabled or not.
To clear all cookies stored by the current browser session, you can use the deleteAllCookies() method. This method is called on the WebDriver instance.
Robot is a predefined class present in the java.awt package which is used for performing keyboard actions in a webpage. It has methods like:
keyPress() – Used to press a keykeyRelease() – Used to release a key All the tables present in a webpage are called webtables. First, fetch the rows using tag name tr and iterate through them. Then fetch the data using tag name td and iterate to pick a particular value using if conditions. Fetch headers using tag name th and iterate through them.
Types of Webtables:
Signup