Selenium Mock Part - 1 FAQ's

Selenium is an open-source automation testing tool used to test web applications across different browsers and platforms. It supports multiple programming languages like Java, Python, C#, Ruby, JavaScript, etc., and allows testers to automate browser actions such as clicking, typing, navigating, etc.

Advantages:

  • Open Source and Free of Cost
  • Supports Multiple Languages
  • Cross-Browser Compatibility & Cross-Platform Support
  • Integration with Other Tools
  • Parallel & Remote Execution
  • Large Community Support

Disadvantages:

  • Only for Web Applications
  • No Built-in Reporting
  • Requires Programming Knowledge

Selenium is not a single tool — it’s a suite of tools for automating web application testing. It includes the following 4 main components:

  1. Selenium IDE (Integrated Development Environment) - Used for recording and playback of test scripts
  2. Selenium RC (Remote Control) - It is now deprecated and replaced by Selenium WebDriver
  3. Selenium WebDriver (Most Popular Component) - A programming interface to automate browser actions
  4. Selenium Grid - Used for parallel execution of tests on multiple machines and browsers simultaneously

WebDriver is the main interface in Selenium that is used to control the browser.
WebElement is used to interact with individual HTML elements (buttons, input fields, links, etc.).

WebDriver Methods (Browser level control):

  • get(String url) – Open a URL in the browser
  • getTitle() – Get the current page title
  • getCurrentUrl() – Get the current page URL
  • navigate().to(String url) – Navigate to a URL
  • navigate().back() – Go back to the previous page
  • navigate().forward() – Go forward to next page
  • navigate().refresh() – Refresh the current page
  • manage().window().maximize() – Maximize the browser window

WebElement Methods (Element level control):

  • click() – Click the element
  • sendKeys(String text) – Enter text into an input field
  • clear() – Clear the input field
  • getText() – Get the visible text of the element
  • getAttribute(String name) / getDomProperty(String name) – Get attribute value of the element
  • isDisplayed() – Check if the element is visible
  • isEnabled() – Check if the element is enabled
  • isSelected() – Check if a checkbox or radio button is selected

close() Method:

  • Closes only the current browser window
  • WebDriver session is still active

quit() Method:

  • Closes all browser windows opened by WebDriver
  • Ends the entire WebDriver session

findElement() Method:

  • Returns a single WebElement & finds the first matching element on the page
  • Throws NoSuchElementException if no match is found

findElements() Method:

  • Returns a list of WebElements (List<WebElement>) & finds all matching elements on the page
  • Returns an empty list if no elements are found (no exception)

get() Method:

  • Belongs to the WebDriver interface
  • Used to load a new web page in the current browser
  • Waits until the page is completely loaded
  • Syntax: driver.get("https://omrbranch.com");

navigate().to() Method:

  • Belongs to the Navigation interface (accessed via driver.navigate())
  • Also used to load a new page, just like get()
  • Can be used with browser history navigation
  • Syntax: driver.navigate().to("https://omrbranch.com");

Locators are used to identify elements on a web page (like buttons, input fields, links, etc.). Selenium provides 8 types of locators:

  • id
  • name
  • className
  • xpath
  • css-selector
  • tagname
  • linktext
  • partial-linktext

XPath (XML Path Language) is used in Selenium to locate elements in an HTML document using path expressions.

Absolute XPath:

  • Starts from the root element (html) and follows the entire path
  • Uses single forward slash /
  • It is not recommended because it is very fragile – breaks if the structure changes

Relative XPath:

  • Starts from any intermediate node
  • Uses double forward slash //
  • More flexible and reliable
  • Set Breakpoints – Click beside the code line in Eclipse/IntelliJ to pause execution at that point.
  • Run in Debug Mode – Right-click and select Debug As → Java Application to start step-by-step execution.
  • Step Over (F6) – Executes the current line and moves to the next line.
  • Step Into (F5) – Enters into method calls to check inner logic.

In Selenium, to perform mouse actions like hover or drag-and-drop, I use the Actions class. First, I create an Actions object:

Actions actions = new Actions(driver);

Then I use methods such as moveToElement() for hover, click(), doubleClick(), or contextClick() depending on the action needed. Finally, I always use perform() to execute the complete action. This is especially useful when working with hover-based menus, sliders, or performing complex mouse interactions.

For mouse actions like double-click and right-click, I again use the Actions class. To double-click on an element:

actions.doubleClick(element).perform();

For right-click (context click):

actions.contextClick(element).perform();

These interactions are very helpful when dealing with custom menus or options on a webpage.

When the webpage has frames or iframes, we cannot directly interact with elements inside them unless we switch the focus. To do that, I use:

driver.switchTo().frame();

We can switch using the frame’s index, name/id, or even a WebElement reference. Once the task is complete:

driver.switchTo().defaultContent();

This is important in applications where forms or content are embedded inside frames.

To find all frames in a webpage:

List<WebElement> frames = driver.findElements(By.tagName("iframe")); int totalFrames = frames.size();

This helps when debugging or dynamically switching through multiple frames.

An Alert is a JavaScript-based browser pop-up that blocks interaction with the page until it’s handled. In Selenium, I handle it using:

driver.switchTo().alert();

Types of Alerts:

  • Simple Alert – Just has an OK button.
  • Confirm Alert – Has OK and Cancel buttons.
  • Prompt Alert – Has a text box along with OK and Cancel.

To handle a simple alert, I switch to it using driver.switchTo().alert(). Then I use getText() if I need to read the message. Finally, I use accept() to click OK and close the alert. Since it’s a simple alert, there’s no Cancel or input option.

To handle a Confirm Alert in Selenium, I switch to the alert using driver.switchTo().alert(). This alert has both OK and Cancel buttons. I use accept() if I want to click OK, and dismiss() to click Cancel. It’s usually used when the application is asking for user confirmation before performing an action.

A Prompt Alert not only has OK and Cancel, but also allows text input. To handle it, I first switch using driver.switchTo().alert(). Then I use sendKeys("inputText") to enter data into the alert. Finally, I use either accept() to submit or dismiss() to cancel. This is helpful for alerts that ask for user details or search input.

If I'm inside a nested (child) frame and want to go up one level, I use driver.switchTo().parentFrame(). This takes me to the immediate parent frame of the current one.

To completely exit all frames and return to the main HTML page, I use driver.switchTo().defaultContent(). This command is essential when switching between frames and webpage content.

In cases where sendKeys() doesn’t work properly — like hidden elements or dynamic inputs — I use JavaScriptExecutor. I write a script like: First, I typecast WebDriver with JavaScriptExecutor and then run:

javascriptExecutor.executeScript("arguments[0].value='text';", element); 

This sets the value directly in the DOM. It’s a very reliable workaround when standard typing fails.