Selenium Mock Part - 2 FAQ's

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()
This works only for dropdowns created using the <select> tag.
For non-select dropdowns, I use click and sendKeys logic.

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.

  • Selenium 4 uses W3C WebDriver Protocol, Selenium 3 doesn’t.
  • No need for manual driver setup in Selenium 4.
  • Introduced Relative Locators in Selenium 4.
  • Selenium 4 provides built-in DevTools support.
  • Better support for cross-browser testing.
  1. The Test Script sends commands using the WebDriver API.
  2. Commands are translated to JSON Wire Protocol (Selenium 3) or W3C Protocol (Selenium 4).
  3. The protocol passes commands to the respective Browser Driver like ChromeDriver or GeckoDriver.
  4. The Browser Driver communicates directly with the actual browser.
  5. The response is returned back to the script via the same path.
  • Use the Actions class to perform keyboard operations.
  • Simulate keys like Keys.ENTER, Keys.TAB, Keys.BACK_SPACE, etc.
  • For combinations like CTRL + A, use keyDown() and keyUp() methods.
  • Useful for form submissions, navigating between fields, and keyboard shortcuts.
  • Use driver.getWindowHandles() to get all window IDs.
  • Loop through the IDs and switch using driver.switchTo().window(windowId).
  • Check the title or URL of the window before switching.
  • Useful for handling popups, new tabs, or external links.
  • Before opening a new window, store the parent window ID using driver.getWindowHandle().
  • After handling the child window, use driver.switchTo().window(parentId) to return.
  • Ensures returning to the main test flow after child window actions.
  • Critical when multiple windows are part of the same test case.

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.

  • Implicit Wait waits dynamically until the element is available.
  • Thread.sleep() pauses for a fixed time regardless of element state.
  • Implicit wait is more efficient and reduces test run time.
  • Thread.sleep() increases execution time and is not flexible.
  • Implicit wait is set globally; sleep() is used line-by-line.
  • Implicit Wait applies to all elements globally.
  • Explicit Wait applies to a specific element or condition.
  • Explicit wait allows custom conditions like visibility, clickability.
  • Implicit wait does not support conditions.
  • Explicit wait provides better control in dynamic scenarios.
  • WebDriverWait is a subclass of FluentWait.
  • WebDriverWait uses default polling (500ms), FluentWait allows custom polling time.
  • FluentWait can ignore specific exceptions like NoSuchElementException.
  • FluentWait is more flexible for complex wait conditions.
  • WebDriverWait is easier to use for common scenarios.
  1. elementToBeClickable(By locator) – waits for element to be clickable.
  2. visibilityOfElementLocated(By locator) – waits until element is visible.
  3. alertIsPresent() – waits for alert pop-up to appear.
  4. titleContains("text") – waits until page title contains specific text.
  5. presenceOfElementLocated(By locator) – waits for element presence in DOM.
  • I use try-catch blocks to gracefully handle failures during test execution.
  • I catch specific exceptions like NoSuchElementException, TimeoutException, etc., to handle known issues.
  • I always log the exception details for debugging and reporting purposes.
  • I capture screenshots at the point of failure to analyze the issue visually.
  • I also use explicit waits to avoid synchronization-related exceptions.
  • 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.
  • This exception occurs when the DOM has changed after the element was located.
  • The element becomes stale, meaning its reference is no longer valid.
  • It usually happens after a page refresh, navigation, or dynamic DOM update.
  • The solution is to re-locate the element before interacting again.
  • It's common when working with AJAX or dynamic web apps.
  • It occurs when Selenium fails to locate an element using the given locator.
  • It can happen if the element is not yet loaded on the page.
  • Incorrect locator or changes in UI structure may throw this exception.
  • It’s also caused by lack of proper synchronization (e.g., no wait).
  • It is most common during test automation in dynamic web pages.
  • Use explicit waits (WebDriverWait) to wait until the element is visible/clickable.
  • Double-check the locator to ensure it matches the latest DOM.
  • Wrap the logic inside a try-catch block to prevent test failure.
  • Use proper page load and wait strategies to handle slow-loading elements.
  • Use isDisplayed() or isPresent() checks before actions.
Fetch all options inside a dropdown using getOptions method and check if it is currently selected using isSelected method.

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 key
  • keyRelease() – 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:

  • Static – Data is fixed.
  • Dynamic – Data keeps changing.