The Selenium suite comprises several components, including Selenium WebDriver, Selenium IDE, Selenium Grid, and Selenium Remote Control (now deprecated). These components serve different purposes in automating web applications.
Automation testing is the use of software tools to execute pre-scripted tests on a software application. Advantages include:
Exceptions in Selenium are similar to exceptions in other programming languages. The most common exceptions in Selenium are:
The different types of frameworks are:
Some file types that can be used as data sources are Excel, XML, text, CSV, etc.
Page Object Model | Page Factory |
---|---|
It is a class that represents the web page and has functionality. | It is a way to initialise the web elements within the page object when the instance is created. |
Selenium WebDriver works with a variety of browsers, including:
This makes it flexible for testing across different browser environments.
Selenium 4 is the latest version of the Selenium testing tool. It brings new features and betterments over older versions. It offers better support for modern web testing needs, with updated browser compatibility, enhanced debugging, and user-friendly updates for testing purposes.
This command will prompt the Selenium WebDriver to open the specified URL in the web browser.
X-Path is used to find the WebElement in web pages. It is also helps in identify the dynamic elements.
TypeKeys() will trigger JavaScript events in most cases, whereas .type() wonโt. Type key initialises the value attribute using JavaScript, whereas .typekeys() simulates the user typing.
Assert: Assert checks whether an element is on the page or not. The test will stop if the step fails and the asserted element is not available. In other words, when the check fails, the test will terminated.
Verify: The verify command will check whether the element is on the page; if it is not available, then the test will be executed. In verification, all the commands will run irrespective of whether the tests fail.
It is defined as an interface that changes the behaviour of the system. Listeners allow the customisation of reports and logs. Listeners are of two types:
Selenium is preferred for web applications due to its:
Selenese is the language used by Selenium to write test scripts. Different types of Selenium commands include:
Locators are used to identify web elements on a page. Examples include:
The exception test in Selenium involves handling exceptions that may occur during script execution. It ensures graceful handling of errors and prevents script termination.
The "findElement()" function yields the initial matching element, whereas "findElements()" provides a collection of all matching elements. For example:
WebElement element = driver.findElement(By.id("elementId"));
List <WebElement> elements = driver.findElements(By.className("className"));
The fundamental steps to develop a test case using Selenium WebDriver are:
Multiple strategies can be used to identify WebElements, including using name, partial link test, link test, ID, class name, and tag name.
The findElement() method retrieves a single WebElement that corresponds to the given locator, whereas findElements() returns a collection of all WebElements that match the specified locator.
The getText() method returns the text that is visible within a WebElement, while getAttribute() provides the value of a specified attribute associated with that WebElement.
Selenium 2.0 is a tool that makes the development of automated tests for web applications easier. It merges the original Selenium project with the WebDriver project. Selenium RC has been outdated since the merge but was used for backward compatibility.
Selenium 3.0 is the extended version of Selenium 2.0. It is fundamentally backwards compatible and does not rely on Selenium RC. The latest version includes various bug fixes and improved stability.
Different types of annotations that are used in Selenium include:
You can select the nth-child element using XPath with the following expression: (//parent-element/*)[n].
Single slash โ/ โ
Single Slash (/): This initiates a selection from the document node and allows you to form โabsoluteโ path expressions.
Double Slash (//): This starts a selection that can match elements anywhere within the document, enabling the creation of โrelativeโ path expressions.
You can capture a screenshot by utilising the TakeScreenshot function. The getScreenshotAs() method allows you to save that screenshot. Example:
File scrFile = ((TakeScreenshot)driver).getScreenshotAs(outputType.FILE);
Yes, it is possible. We can enter or send a text without using the sendKeys() method. This can be done using JavaScriptExecutor. The DOM method of identification of an element can be used to go to that particular document, get the element by its ID (login) and then send the text by value.
Take a look at the below sample code:
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementById(โLogin').value=Test text without sendkeys");
As there will be a pop-up during the login process, it's essential to use the command explicitly to check for the presence of the alert. We should only input the username and password credentials if the alert is displayed. Below is a sample code demonstrating the explicit wait command and how to verify the alert.
WebDriverWait wait = new WebDriverWait(driver, 10);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.authenticateUsing(new UserAndPassword(**username**, **password**));
To byepass a particular test method in TestNG, you can set the 'enabled' attribute of the test annotation to false. For example:
```java
@Test(enabled = false)
```
The 'enabled' parameter is set to true, so it is unnecessary to explicitly state it as true when declaring the annotation.
It is possible to locate an element in Selenium with the help of:
The fundamental purpose of Selenium WebDriver is when application testing. Hence it is not possible to use it to test APIs or web services.
The Same Origin Policy is a security mechanism implemented in web browsers which restricts a script on one webpage from accessing content on another webpage unless both share the same origin (including the URL scheme, hostname, and port). This policy is vital for preventing harmful scripts from one site from obtaining sensitive information from another.
For instance, a JavaScript application running on google.com can interact with all pages within Googleโs domain (like google.com/login or google.com/mail), but it cannot access content from different domains (such as yahoo.com).
To bypass this restriction, Selenium RC created a server that functions as a proxy, making it appear that Selenium and the web application originate from the same source. This allows Selenium to conduct tests on applications across different domains with greater flexibility.
XML documents can be navigated using XPath. XPath is frequently employed in Selenium to identify elements on a webpage.
"/" in XPath denotes the direct child, while "//" denotes any descendant. For example, "/div" looks for a direct child "div," and "//div" looks for any descendant "div" at any level.
In Selenium, "type" is used to enter text into a text field, while "typeAndWait" additionally waits for the page to reload after entering the text.
Selenium provides implicit and explicit waits to ensure the web page is fully loaded. Implicit waits set a default timeout, while explicit waits wait for a specific condition to be met before proceeding.
An alternative to driver.get() is driver.navigate().to("url"), which also navigates the browser to the specified URL.
Four parameters that need to be passed in Selenium are:
Clicking a Link by Full Text: driver.findElement(By.linkText("Menu")).click();
This command locates a link on the page with the exact text "Todayโs deals" and clicks it, redirecting the user to the linked page.
Clicking a Link by Partial Text: driver.findElement(By.partialLinkText("marks")).click();
To handle dropdowns in Selenium, you can use the Select class. Here's a step-by-step approach:
Data-driven testing is an approach where test cases are executed with multiple test data sets. It helps in testing different scenarios without duplicating test code. To implement data-driven testing in Selenium:
Selenium cannot automate Captcha because its primary purpose is to prevent bots and automated programs from accessing sensitive information. As a result, automation test engineers are required to manually enter the Captcha, while the other fields can be filled in automatically.
It is a mechanism that involves more than one component to work parallel with each other. Synchronisation can be classified into two categories:
The WebElement is an interface in Selenium used to represent an HTML element on a web page. It provides methods to interact with the web elements, such as clicking, entering text, and getting the value. It identifies and manipulates the web elements in the automation testing process.
If the file is on the same machine or on a mapped network drive, it is straightforward: We have to just type the โpathโ of the file in the FileUpload control.
Example:
driver = webdriver.Firefox()
element = driver.find_element_by_id("fileUpload")
element.send_keys("C:myfile.txt")
Selenium supports many programming languages, including Python, Java, C#, Ruby, and JavaScript. PHP, on the other hand, is not natively supported by Selenium. While third-party libraries and frameworks can be used to integrate Selenium with PHP, Selenium does not have built-in support for PHP.
Selenium is consistently evolving, and there may be future updates or third-party integrations that offer support for PHP or other languages.
A wait statement in Selenium is used to pause the execution of a test script until a certain condition is satisfied or a specific duration has elapsed. This synchronisation approach ensures that the web application under test has fully loaded or that particular elements are ready for interaction, thereby minimising timing-related problems that could result in test failures.
They are two common techniques used to find web elements in Selenium, each with its own syntax and applications. CSS selectors utilise a syntax derived from CSS, enabling the selection of elements based on their attributes and their relationships to other elements. In contrast, XPath is grounded in XML syntax, allowing for navigation through the HTML or XML tree structure to identify elements. Although both methods are generally supported by modern browsers, XPath tends to offer more reliable behaviour, while CSS selectors can exhibit slight variations across different browsers.
In Selenium, HTTP errors can be handled by checking the HTTP status code of the web page response. HTTP status codes indicate whether a web page was loaded successfully or if there was an error.
To manage HTTP errors in Selenium, you can follow these steps:
In Selenium, a hard assertion is a type of assertion that results in a test failure if the expected condition is not met. If the condition outlined in a hard assertion is not satisfied, the test script will halt immediately and be marked as failed, preventing any further execution of the subsequent steps or test cases.
In simpler terms, a hard assertion functions as an assert statement that interrupts test execution and raises an exception if the anticipated condition isn't fulfilled. This is different from a soft assertion, which permits the test script to proceed even if the expected condition fails, allowing for the accumulation of test results to be reported at the conclusion of the test.
To target a specific area within an element, you should employ the clickAt command. The clickAt command takes an element locator along with x and y coordinates as its parameters, formatted as follows: clickAt (locator, cordString).
Selenium WebDriver is the programming interface of Selenium that provides a way to interact with web browsers. It allows users to write scripts in programming languages to automate browser actions.
No, Selenium WebDriver is not a library. It is a framework that provides a set of APIs (Application Programming Interfaces) for interacting with web browsers.
Selenium WebDriver is an interface. Different browser-specific implementations of this interface are provided, such as ChromeDriver, FirefoxDriver, etc.
There are three main WebDriver APIs in Selenium:
Selenium WebDriver is compatible with various programming languages such as Java, Python, C#, Ruby, and JavaScript.
WebdriverIO is a modern test automation framework for native mobile and web applications. The library is built with JavaScript on top of standard libraries and is pretty straightforward to get up and running with using NPM. Many developers use WebdriverIO because it works natively with Node.js and presents an updated way of automated testing in their projects.
You can find an individual element on a webpage using the findElement() method. In this way, you can locate and perform specific actions on your webpage elements, such as buttons or text fields. After identifying an element, you would be able to perform several kinds of actions, such as clicking, typing text, and reading content, just like a user would do on your website.
You can use the contains() function to find the elements, knowing only partial information on their identification. For instance, if you know that somewhere in the name, "user" is present: //*[contains(@name,'user')]. Very useful if you work with content in which some parts of identifiers are generated dynamically.
HTMLUnit Driver is the fastest one. It provides your test with very high speeds.
Here's a simple example of how you might check the text:
javaCopyWebElement el = driver.findElement(By.id("ElementID")
String text = el.getText();
Assert.assertEquals("Element Text", text);
You can use special keyboard actions when pressing multiple keys together (like ALT, SHIFT, and CONTROL). Here's a simple way to do this:
Here is the syntax for the same:
Actions actions = new Actions(driver)
actions.keyDown(Keys.ALT).keyDown(Keys.SHIFT) .keyDown(Keys.CONTROL).sendKeys('test') .build() .perform();
You can upload files using the sendKeys command and the file's location, e.g., element.sendKeys (file path). Ensure your webpage has an input element with type="file" to handle the upload. This approach mimics how users usually upload files through a browser.
These are two different ways to add delays in your tests:
With sleep(), your test pauses once for a specific time (like waiting 2 seconds) before moving on. It's like taking a quick break at one specific point.
For example, for 2 seconds, wait and use the thread.sleep(2000)
With setSpeed(), each action in your test will pause for the time you specify. This is helpful when you're demonstrating features or working with slower applications. It's like adding a slight pause between each step.
Ex: Similarly, for 2-second pause, use - selenium.setSpeed(โ2000โ)
Heightened-privilege browsers operate in a special mode designed to grant extended permissions beyond standard browser operations. This mode serves a similar purpose to Proxy Injection, enabling websites to perform actions that are typically restricted. The main distinction is that the browser is launched with elevated capabilities in heightened-privilege mode. This allows Selenium Core to interact directly with the Application Under Test (AUT), including reading and writing its content, without routing all AUT data through the Selenium RC server.
There are two straightforward ways to submit forms:
Use the submit() method directly: element.submit()
Click the submit button using the click() method
You can find all frames on a page using driver.findElements(By.xpath("//iframe")). This creates a list of frames you can check until you find what you want. It's like looking through each window in a building until you find the right one.
You can handle colours in a web driver by using the getCSSValue(arg0) method. This method retrieves the specified CSS property value, such as colour, by providing the property name as an argument.
You can adjust your browser window size using maximise () to make it full screen or minimise () to shrink it. This is helpful when you must test how your website looks at different sizes.
Automation works particularly well for:
Some situations where manual testing makes more sense:
Selenium WebDriver is compatible with various programming languages, such as Java, Python, C#, Ruby, and JavaScript.
The SearchContext interface is the super interface of WebDriver. It defines the basic methods for finding elements.
Java WebElement element = driver.findElement(By.xpath("//input[@id='username']"));
This line of code uses XPath to locate a web element, specifically an input field with the id attribute equal to 'username', and assigns it to the variable "element."
No, the Selenium Server is not mandatory for executing Selenium WebDriver scripts. Selenium Server is mainly used in Selenium Grid setups for parallel execution.
The given command is an example of locating a web element using XPath. It finds an input field with the id attribute 'username' and assigns the corresponding WebElement to the variable 'element.'
You can enable a disabled HTML element by modifying its attributes using the .removeAttribute("disabled") method. Below is an example using a JavaScript executor to enable a textbox:
String script = โdocument.getElementsByName('name')[0].removeAttribute('disabled');";
javascript.executeScript(script);
The navigate().forward() command is used to move the browser forward by one page in its history. It works like clicking the forward button in the browser.
Regular expressions can be used in Selenium by prefixing a text value with the keyword regexp. This treats the text as a regular expression.
You can enter text into a textbox by using the sendKeys() method with the following code:
WebElement searchTextBox = driver.findElement(By.id("srch"));
searchTextBox.sendKeys("searchTerm");
The typeAndWait command is utilised when an event reloads following the input.
Cookies can be deleted in Selenium using the deleteAllCookies() method. Here's an example:
driver.manage().deleteAllCookies();
You can double-click on an element using the Actions class.
Actions action = new Actions(driver);
WebElement e =driver.findElement(By.id("elementId"));
action.doubleClick(e).perform();
Pairs of keys and values that are utilised to set up properties specific to the browser, including the platform, browser version, and various other configurations.
You can find all links by locating elements with the tag name a.
The code is:
List<WebElement> link = driver.findElements(By.tagName("a"));
Selenium Grid allows for the execution of test scripts across different platforms and browsers at the same time. This method not only saves time but also facilitates testing in diverse environments.
A node is a machine connected to the grid hub that is responsible for executing test scripts. Unlike the hub, which manages test distribution, the grid can have multiple nodes to run tests on different environments.
Selenium 4 introduces several new features, including:
The hub is the central server in Selenium Grid that manages and controls test executions across connected machines.
The testng.xml file in TestNG serves as a configuration file for defining and organizing test suites, test groups, and test methods. It provides flexibility in:
This file simplifies managing complex test scenarios and ensures efficient test execution.
Selenium offers the following WebDriver APIs:
Both methods navigate to a URL, but driver.get() is a simpler form, while driver.navigate().to("url") is part of the navigation interface and offers additional navigation options.
driver.close() closes the current browser window, while driver.quit() method shuts down all open browser windows and ends the WebDriver session. driver.quit() is generally preferred to ensure a clean shutdown of the WebDriver and release associated resources.
Selenium WebDriver is not suitable for automating:
An Object Repository in Selenium WebDriver is a centralised location where web elements and their locators are stored. It helps maintain and organise locators separately from the test scripts, promoting reusability and ease of maintenance. Object Repositories can be implemented using properties files, XML files, or using in-built mechanisms in certain test automation frameworks.
A data-driven framework is a test automation framework that separates the test data from the test script. It allows testers to write automated tests in a way that is independent of the test data.
CSS selectors use .className to find elements belonging to a specific class. For example, the student will select all elements with the class student.
CSS selectors use #idValue to find elements by their ID. For instance, #studentId will select the element with the ID studentId.
CSS selectors use [attribute=value] to target elements with a specific attribute and value. For example, [type=id] will select elements where the type attribute has the value id.
When web elements change dynamically during software development and XPath functions fail, XPath axes can be used to identify these elements more effectively.
XPath axes are methods for locating nodes relative to a context (current) node in an XML document. They are particularly useful for finding dynamic elements that standard locators cannot identify.
A context node refers to a specific node in the Document Object Model (DOM) that is a starting point for locating or interacting with web elements. It helps narrow the search scope, improving efficiency and accuracy when working with complex or nested web elements.
In Selenium IDE, Verification points are implemented using Selenese Verify and Assert commands. In Selenium WebDriver: There are no built-in verification features. Testers can code custom verification points, such as:
FireFix Driver fulfills the WebDriver interface. It belongs to Java class.
SearchContext is the WebDriverโs super interface.
Webdriver driver = new FirefoxDriver();
In this line, 'WebDriver' acts as an interface, and we are establishing a reference for the type of WebDriver by creating an instance of the FirefoxDriver class.
Selenium Test Tool | Silk Test Tool |
---|---|
Silk Test is a paid tool. | Selenium is free and open-source. |
Limited to Internet Explorer and Firefox. | Compatible with various browsers, including Internet Explorer, Firefox, Safari, and Opera. |
Uses its test scripting language. | Supports multiple languages like Java, Ruby, and Perl. |
Suitable for testing client-server applications. | Designed specifically for web application testing. |
To detect broken images on a webpage:
To switch between frames, use the driver.switchTo().frame() method with one of the following:
Index: Specify the frame index starting at 0.
Name/ID: Use the name or ID attribute of the frame.
WebElement: Use a previously identified WebElement representing the frame.
Recovery mechanisms depend on the programming language being used. For Java: Implement exception handling using try-catch blocks to manage unexpected scenarios during test execution.
Selenium provides various methods for handling browser windows, pop-ups, and alerts.
Alerts
Use `driver.switchTo().alert()`. Next,`accept()`, `dismiss()`, and `sendKeys()` to interact with the alert.
Pop-ups
Locate the pop-up element and interact with it like you do with any other web element.
New Windows
First, obtain a list of all the open window handles using `driver.getWindowHandles()โ.
Next, use `driver. switch ().window(windowHandle)` to use a specific window.
The implicit wait is a global setting that instructs Selenium to wait several seconds before throwing an exception if it cannot locate an immediately available element. The implicit wait applies to all subsequent โfind elementโ commands.
Explicit wait, on the other hand, is an exact wait mechanism that waits for a specific condition like element visibility or clickable status to become true before moving on to the next step in the code.
TestNG is an open-source, sophisticated testing framework that developers and testers use to automate tests for software applications. On the other hand, JUnit is an equally popular open-source unit testing framework used widely by Java developers and testers to enhance the code quality in Java applications.
Comparatively, TestNG offers more benefits than JUnit. It allows developers to:
Here are some methods to scroll down a page using JavaScript:
Using scrollBy()
This method allows you to scroll the webpage with a specific number of pixels.
To get the CSS properties of an element in Selenium WebDriver, use the getCssValue() method:
To retrieve the background colour of an element with ID "header", use this code:
String backgroundColor = driver.findElement(By.id("header")).getCssValue("background-color");
This code finds the element with the ID โheaderโ and gets its background colour. You can change โbackground-colourโ to any other CSS property name to see its value.
javaCopySearchContext shadow = driver.findElement(By.cssSelector("parent-element")).getShadowRoot();
WebElement elementInShadowDOM = shadow.findElement(By.cssSelector("element-in-shadow"));
This approach helps you interact with elements inside shadow DOM, which is common in modern web components.
The most effective approach combines several strategies:
javaCopy@Test(threadPoolSize = 3, invocationCount = 10, timeOut = 1000)
public class TestClass {
ThreadLocal driver = new ThreadLocal<>();
@BeforeMethod
public void setup() {
driver.set(new ChromeDriver());
}
}
This ensures thread safety while maximizing resource utilization.
javaCopyWebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("loadingSpinner")));
wait.until(ExpectedConditions.and(
ExpectedConditions.presenceOfElementLocated(By.id("content")),
ExpectedConditions.elementToBeClickable(By.id("content"))
));
javaCopyLogEntries logs = driver.manage().logs().get(LogType.PERFORMANCE);
for (LogEntry entry : logs) {
if (entry.getMessage().contains("Network.responseReceived")) {
// Analyze network performance
}
}
javaCopyChromeOptions options = new ChromeOptions();
HashMap<String, Object> prefs = new HashMap<>();
prefs.put("download.default_directory", downloadPath);
options.setExperimentalOption("prefs", prefs);
javaCopyChromeOptions options = new ChromeOptions();
Map<String, Object> coordinates = new HashMap<>();
coordinates.put("latitude", 37.7749);
coordinates.put("longitude", -122.4194);
options.setExperimentalOption("geoLocation", coordinates);
To improve the performance of Selenium tests, you can:
To handle dynamic web elements, you can:
You can manage cookies in Selenium by:
To handle the StaleElementReferenceException, you can:
Integration with email APIs or services is a must to test email content in Selenium. To do this, you can:
Selenium WebDriver offers several essential functionalities:
The primary issue is that the "findElement" operation may not work as intended when running tests on browsers not officially supported by WebDriver.
Selenium WebDriver can help detect broken images on a webpage by following these steps:
The Actions class is used to hover over web elements. Example:
java
Copy code
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.id("searchBoxID"))).perform();
A Selenium Maven project uses Maven for dependency management and build automation in Selenium test scripts.
Use the Select class to handle dropdowns. Example:
WebElement dropdownElement = driver.findElement(By.id("dropdownID"));
Select dropdown = new Select(dropdownElement);
// Options to select
dropdown.selectByIndex(1);
dropdown.selectByValue("value");
dropdown.selectByVisibleText("VisibleText");
Broken links can be detected by:
Examples include QTP, TestComplete, RFT, and Silk Test.
Examples include LoadRunner, JMeter, WebLoad, NeoLoad, Silk Performer, HP Performance Center, Gatling, and Apache.
Common tools include Jenkins, Travis CI, CircleCI, AWS CodePipeline, Azure DevOps, and Bitbucket Pipelines.
The primary drawback is that it can prolong test execution, as WebDriver will wait for the entire duration before timing out. This may lead to flakiness if elements appear after the specified timeout or introduce delays even when the element is already available.
To launch different browsers in Selenium WebDriver, you can utilise browser-specific drivers. Here's a breakdown of the process for some commonly used browsers:
Ensure you have the appropriate driver executable for the browser installed and set up in your system path.
You can refresh a web page in Selenium WebDriver using several approaches:
Using driver.navigate().refresh(): driver.navigate().refresh();
Using driver.get() with the current URL: driver.get(driver.getCurrentUrl());
Using the sendKeys() method to simulate a keyboard shortcut: driver.findElement(By.tagName("body")).sendKeys(Keys.F5);
Using JavaScript Executor: JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("location.reload()");
These methods effectively reload the page and can be chosen based on your specific requirements.
To invoke an application in Selenium WebDriver, you primarily use the get() or navigate().to() methods:
Using get() Method: driver.get("https://example.com");
This is the simplest method to open a web application.
Using navigate().to() Method: driver.navigate().to("https://example.com");
This method is useful if you need navigation features like back or forward.
driver.navigate().to("https://www.codingninjas.com/")- To navigate the provided URL
driver.navigate().refresh() - To refresh the current page
driver.navigate().forward()- Similar to clicking the forward button on any browser
driver.navigate().back()- Similar to using the back button on any browser
The following mouse functions that can be performed with Selenium are:
mouseMove(WebElement element, long xOffset, long yOffset)
click(WebElement element)
mouseUp(WebElement element)
mouseMove(WebElement element, long xOffset, long yOffset)
mouseDown(WebElement element)
mouseMove(WebElement element)
doubleClick(WebElement element)
To handle this scenario, you can use:
To handle a slow-loading element in Selenium, you can:
The Get command retrieves the inner text of a specified web element. It doesn't require any parameters and returns a value for the type string. This command is commonly used to verify web pages' messages, labels, errors, and other text elements.
A headless browser is a web browser that runs without a graphical user interface (GUI). In Selenium, popular browsers like Chrome and Firefox can operate in headless mode, allowing tests to execute without displaying a browser window. This is especially beneficial for accelerating test execution and conducting tests on servers or environments that lack a graphical user interface (GUI).
The approach depends on the type of authentication:
Selenium Grid is a tool designed to run tests in parallel across several machines and browser configurations, improving efficiency and coverage. It works with two main components:
Hub: The main server that receives test requests and distributes them to available nodes based on the desired browser and platform.
Nodes: Environments or machines where tests are executed using specified browser instances and configurations.
These frameworks:
Many developers and testers prefer Selenium over Quick Test Professional (QTP) because Selenium is an open-source automation testing tool that is free, as opposed to QTP, which requires a significant investment. Selenium is also compatible with many programming languages and operating systems, making it easy to work with.
The `getWindowHandle()` method provides a distinct identifier for the current browser window. On the other hand, the `getWindowHandles()` method delivers a collection of unique identifiers for all browser windows that WebDriver has opened.
The combination of Page Object Model (POM) and Java delivers a bounty of benefits such as:
An Action in Selenium WebDriver using Java is a class that offers methods to carry out intricate user interactions such as:
This feature is handy for simulating a userโs interaction with a web page. Hereโs a classic example of executing a drag-and-drop action:
Actions actions = new Actions(driver);
WebElement sourceElement = driver.findElement(By.id(โsourceโ)); WebElement targetElement = driver.findElement(By.id(โtargetโ)); actions.dragAndDrop(sourceElement, targetElement).build().perform();
To handle this scenario, you can use:
Testing SPAs involves:
Key trends include:
MaxSession determines the total number of browser instances, regardless of type/version, that can run concurrently on a remote machine. It overrides MaxInstances settings.
Example: maxSession=2 allows two browsers to run concurrently.
MaxInstances specifies the number of browser instances of the same version that can run simultaneously.
Use the Selenium WebDriverโs Alert API to interact with alerts:
Mixing both causes longer execution times, as Thread.sleep adds a fixed delay, while WebDriver waits dynamically and adjusts based on conditions. Using both together unnecessarily extends the test duration.
The Robot Class automates keyboard and mouse interactions with OS-level pop-ups or applications (e.g., file upload/download).
Advantages:
Key Methods:
Use the Runtime.exec method: Process batchFile = Runtime.getRuntime().exec("path_to_batch_file");
batchFile.waitFor();
Selenium Grid enables parallel test execution across multiple machines, OS, and browsers, reducing execution time and speeding up feedback.
This occurs when a web element is no longer attached to the DOM (e.g., due to page updates). Interacting with such elements throws this exception. Refreshing the element reference can resolve the issue.
Selenium is not a dedicated performance testing tool, but you can integrate it with tools like JMeter or write custom scripts to measure response times for specific actions.
Use getWindowHandles() to retrieve all window/tab handles and switchTo().window(handle) to switch between them.
For a newly opened tab or window:
String parentHandle = driver.getWindowHandle(); // Store parent window handle
Set handles = driver.getWindowHandles();
for (String handle: handles) {
if (!handle.equals(parentHandle)) {
driver.switchTo().window(handle);
// Perform actions on the new tab or window
}
}
driver.switchTo().window(parentHandle); // Switch back to the parent window
Ensure proper synchronisation using explicit waits for tabs/windows to load entirely before switching.
Page Object Model or POM is a design pattern that serves as a directory for web UI elements. Each web page should be associated with its page class, which is tasked with locating the WebElements on that page and executing actions on them. It:
Facilitates separate operations and flows in the UI from Verification and improves code readability.
Enables multiple tests to use the same Object Repository because it is independent of test cases.
Enables reusability of code.
The command `driver.findElement(By.linkText("Today's deals")).click();` locates the element using its link text and then clicks on it, resulting in the user being redirected to the relevant page.
Similarly, `driver.findElement(By.partialLinkText("Service")).click();` identifies the element by utilising a substring of the link specified in the parentheses. This method, `partialLinkText()`, helps to find the desired web element.
Absolutely! You can input text into a textbox by utilising JavaScriptExecutor:
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementById(โemail').value=โabc.efg@xyz.comโ);
Assertion in Selenium refers to a testing method used to determine whether a specific condition is true or false. In the context of Selenium, assertions help confirm the state of elements on a webpage or the results of an action. They can be used to verify the:
Assertions are crucial in confirming your application behaves as expected. Without them, it would be impossible to determine the success or failure of your tests.
XPath consists of two primary types of expressions: absolute and relative.
To handle dynamic tables in Selenium WebDriver, locate the table using a unique identifier like id or class. Use findElements to retrieve all rows (tr) and then iterate through each row to fetch columns (td). Apply conditional checks within the loop to match the desired cell content for specific data. To interact with elements like buttons or links inside the table, construct dynamic XPath expressions targeting particular cells. Handle tables with dynamically loading rows using explicit waits to ensure components are visible before interacting. If the table has pagination, automate navigation by clicking the "Next" button until all pages are processed. This approach simplifies handling dynamic table content and ensures accurate data extraction or interaction.
Parallel execution in Selenium WebDriver allows multiple test cases to run simultaneously, speeding up the testing process. It is typically achieved using Selenium Grid or test frameworks like TestNG or JUnit. Selenium Grid distributes tests across multiple machines and browsers, ensuring efficient resource utilisation. TestNG supports parallel execution through its configuration file (testng.xml) by setting the parallel attribute to "methods," "tests," or "classes" and defining the thread count. This approach reduces overall execution time, enhances test coverage across different environments, and ensures quicker feedback on test results, making it ideal for large-scale or time-sensitive projects.
Handling keyboard and mouse events in Selenium WebDriver involves the use of the Actions class. Here are simple pointers:
Actions actions = new Actions(driver);
To send keys: actions.sendKeys(Keys.ENTER).perform();
To press a key: actions.keyDown(Keys.CONTROL).perform();
To release a key: actions.keyUp(Keys.CONTROL).perform();
To hover over an element: actions.moveToElement(element).perform();
To click: actions.click(element).perform();
To right-click: actions.context-click(element).perform();
To drag and drop: actions.dragAndDrop(source, target).perform();
Use action methods together for complex sequences, ending with perform().
Use moveByOffset(x, y) to interact with specific screen positions.
Handling keyboard and mouse events in Selenium WebDriver involves using the Actions class. Here are simple pointers:
Actions actions = new Actions(driver);
To send keys: actions.sendKeys(Keys.ENTER).perform();
To press a key: actions.keyDown(Keys.CONTROL).perform();
To release a key: actions.keyUp(Keys.CONTROL).perform();
To hover over an element: actions.moveToElement(element).perform();
To click: actions.click(element).perform();
To right-click: actions.context-click(element).perform();
To drag and drop: actions.dragAndDrop(source, target).perform();
Use action methods together for complex sequences, ending with perform().
Use moveByOffset(x, y) to interact with specific screen positions.
In TestNG, the @BeforeTest and @BeforeMethod annotations are used for setup tasks, but they differ in their scope and execution timing:
@BeforeTest: Runs once before any test methods in a tag in the TestNG XML file.
Typically used for setup tasks everyday to all test methods, such as initialising resources shared across tests (e.g., database connection, WebDriver setup).
@BeforeMethod: Runs before every individual test method in a class.
Typically used for tasks specific to each test, such as resetting test data, navigating to a start page, or clearing browser cookies.
Example:
@BeforeTest
public void setupSuite() {
System.out.println("Runs once before all test methods in the suite.");
}
@BeforeMethod
public void setupTest() {
System.out.println("Runs before each test method.");
}
Our meticulously curated section of Selenium interview questions covers all the important queries you may face during your job interview.ย
However, the best way to master Selenium is to understand its basic concepts and their real-world applications. At Aimore Technologies, Chennai, our world-class courses and Selenium training help elevate your software testing and automation skills.ย
Join us today and turn your ambitions into reality!