



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Selenium WebDriver with Java · Collected by Mohammad Taslim Prepare for beginner and intermediate QA automation interviews. This guide covers Selenium fundamentals, element locators, waits, browser interactions, and test design. The Java snippets use Selenium 4 syntax and assume an active WebDriver session unless stated otherwise.
Selenium is an open-source suite for automating web browsers. WebDriver code can be written in languages such as Java, Python, C#, Ruby, and JavaScript. Common browser targets include Chrome, Edge, Firefox, and Safari. The browser and operating system combination determines which driver you can use.
WebDriver controls browsers from test code. Selenium IDE records and replays browser interactions. Selenium Grid routes WebDriver sessions to remote machines and supports parallel execution across browser and platform configurations.
ChromeDriver, EdgeDriver, FirefoxDriver, and SafariDriver automate their respective browsers. RemoteWebDriver connects to a remote WebDriver endpoint, such as Selenium Grid.
Use it to exercise browser-based workflows for functional, regression, smoke, sanity, cross-browser, and responsive testing. It can also cover integration scenarios through the UI. Define the inputs and expected outcomes first, then compare them with the application’s actual behavior.
Selenium automates browsers. Native desktop and mobile app interfaces require other tools. Test organization, assertions, and reporting typically come from a framework such as JUnit or TestNG. Pixel-level visual comparison requires a separate tool.
Selenium 2 introduced WebDriver to the Selenium project while retaining Selenium Remote Control (RC). The Selenium 3 Java distribution moved the legacy RC client to a separate package. The examples in this guide use Selenium 4.
Element Location
findElement returns the first match and throws NoSuchElementException when there is no match. findElements returns all matches as a list, which is empty when nothing matches.
Use a stable ID or test-specific attribute when the application provides one. Link text and partial link text apply to links. A class-name locator accepts one class token at a time. By.id("email"); By.name("books"); By.linkText("Today’s Deals"); By.partialLinkText("Service"); By.tagName("button"); By.className("inputtext"); By.cssSelector("input[data-testid='email']"); By.xpath("//button[normalize-space(.)='Sign in']");
Use # for an ID, a period for a class, and brackets for an attribute. Combine them to narrow your match. To locate an element by its displayed text, use link text for a link or an XPath text condition. input#email input.inputtext input[name='email'] input.inputtext[name='email']
XPath locates nodes in the page’s DOM. An absolute path starts at the document root and depends on the page’s structure. A search such as //button can find matching nodes anywhere in the document. Use contains() for partial matches, and combine conditions with and or or. /html/body/main/form/button //button[@type='submit'] //button[contains(normalize-space(.), 'Sign')] //input[@type='submit' or @name='btnReset'] //input[@type='submit' and @name='btnLogin']
Browser Interactions with Java
Locate the element, then use sendKeys() for text entry or click() to activate a link or button. Use clear() first when the test needs to replace existing text in an editable field. driver.findElement(By.id("email")).sendKeys("[email protected]"); driver.findElement(By.linkText("Help")).click();
JavaScriptExecutor can scroll the browsing context by a specified number of pixels. A positive vertical offset scrolls down. JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.scrollBy(0, 600)");
Read the title with getTitle() and compare it with the expected value using an assertion from your test framework. This example uses JUnit Jupiter. assertEquals("Account Dashboard", driver.getTitle());
Use Actions.moveToElement().perform() to hover. Call getCssValue() with the CSS property name, such as font-size, to read its computed value. WebElement menu = driver.findElement(By.id("account-menu")); new Actions(driver).moveToElement(menu).perform(); String fontSize = menu.getCssValue("font-size");
Use Select for a native HTML select element. Choose by zero-based index, option value, or visible text. Each line below is an alternative. For a custom dropdown, locate and click its controls. Select books = new Select(driver.findElement(By.id("books"))); books.selectByIndex(0); books.selectByValue("fiction"); books.selectByVisibleText("The Alchemist");
Yes. A script can assign the DOM value directly. This can skip keyboard and input events that the application expects. Use sendKeys() when the test is meant to exercise real text entry. js.executeScript("arguments[0].value = arguments[1];", driver.findElement(By.id("email")), "[email protected]");
Alerts and Test Design
An alert displays a message. A confirmation gives the user an OK or Cancel choice. A prompt accepts text. Switch to the dialog with switchTo().alert(), then call getText(), accept(), or dismiss() as appropriate. Use sendKeys() only for a prompt. Alert prompt = driver.switchTo().alert(); String message = prompt.getText(); prompt.sendKeys("Sample response"); prompt.accept();
A native file picker sits outside the web page. For a file upload, send the file path directly to the HTML input of type file. Native dialogs that require desktop interaction need separate tooling.
Cast the driver to TakesScreenshot and call getScreenshotAs(). This example returns a temporary image file that your test can copy into its reporting directory. File image = ((TakesScreenshot) driver) .getScreenshotAs(OutputType.FILE);
A page object groups the locators and actions for a page or component behind methods such as signIn(). Tests call those methods and assert expected outcomes. Keeping locators in one place makes UI changes easier to maintain.
CAPTCHA is designed to distinguish humans from automated clients. Use approved test keys or disable the CAPTCHA in a controlled test environment when the goal is to test the surrounding workflow.
Browsers restrict how JavaScript from one origin can read data from another. An origin is defined by its scheme, host, and port. WebDriver can navigate across origins or switch to an iframe, while scripts executed in a page are still subject to the browser’s security rules.
Selenese is the name historically used for Selenium IDE commands. Common categories include actions that interact with the browser, accessors that read state, and checks. An assert failure stops the current test, while verify records the failure and lets the test continue.
Selenium documentation at selenium.dev/documentation/ covers WebDriver, Grid, and migration to Selenium