PAPracticeAutomation
Se

Java ยท JUnit 5

Element handling with Selenium

15 copy-ready Selenium recipes, each linked to a live challenge page to run them against.

Conventions
// Helpers used below (plus static imports of ExpectedConditions.* and Assertions.*):
By tid(String id) { return By.cssSelector("[data-testid='" + id + "']"); }
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

1Type into inputs & read output

Practise on /elements/text-box โ†’
driver.findElement(tid("full-name-input")).sendKeys("Ada Lovelace");
driver.findElement(tid("submit-text-box")).click();
assertEquals("Name: Ada Lovelace",
    wait.until(visibilityOfElementLocated(tid("output-name"))).getText());
driver.findElement(tid("checkbox-select-all")).click();
assertTrue(driver.findElement(tid("checkbox-documents")).isSelected());
new Select(driver.findElement(tid("native-select"))).selectByVisibleText("Mumbai");
assertEquals("Selected: Mumbai", driver.findElement(tid("native-select-result")).getText());
driver.findElement(tid("autocomplete-input")).sendKeys("Ben");
wait.until(elementToBeClickable(tid("autocomplete-option-bengaluru"))).click();
assertEquals("Picked: Bengaluru", driver.findElement(tid("autocomplete-result")).getText());

5Find a table row by its text

Practise on /elements/web-tables โ†’
driver.findElement(By.xpath(
    "//tr[@data-testid='table-row'][contains(., 'Priya')]"
  + "//*[starts-with(@data-testid, 'delete-row-')]")).click();
assertEquals(2, driver.findElements(tid("table-row")).size());

6Double-click & right-click

Practise on /elements/buttons โ†’
Actions actions = new Actions(driver);
actions.doubleClick(driver.findElement(tid("double-click-me"))).perform();
actions.contextClick(driver.findElement(tid("right-click-me"))).perform();
assertTrue(driver.findElement(tid("right-click-result")).isDisplayed());

7JS alert, confirm & prompt

Practise on /alerts โ†’
driver.findElement(tid("prompt-button")).click();
Alert prompt = wait.until(alertIsPresent());
prompt.sendKeys("Ada");
prompt.accept();
assertEquals("You entered Ada", driver.findElement(tid("prompt-result")).getText());

driver.findElement(tid("confirm-button")).click();
wait.until(alertIsPresent()).dismiss();

8Iframes & nested iframes

Practise on /frames โ†’
driver.switchTo().frame(driver.findElement(tid("single-frame")));
driver.findElement(tid("single-frame-button")).click();
assertEquals("Clicked inside iframe", driver.findElement(tid("single-frame-result")).getText());
driver.switchTo().defaultContent();

driver.switchTo().frame(driver.findElement(tid("outer-frame")))
      .switchTo().frame(driver.findElement(tid("innermost-frame")));

9New tabs & windows

Practise on /windows โ†’
String original = driver.getWindowHandle();
driver.findElement(tid("new-tab-button")).click();
wait.until(numberOfWindowsToBe(2));
for (String h : driver.getWindowHandles())
  if (!h.equals(original)) driver.switchTo().window(h);
assertTrue(wait.until(visibilityOfElementLocated(tid("new-tab-heading"))).isDisplayed());
// Actions.dragAndDrop can't fire HTML5 drag events in Chrome โ€” dispatch them with JS.
((JavascriptExecutor) driver).executeScript(
    "const [s, t] = arguments, dt = new DataTransfer();"
  + "s.dispatchEvent(new DragEvent('dragstart', {bubbles: true, dataTransfer: dt}));"
  + "t.dispatchEvent(new DragEvent('drop', {bubbles: true, dataTransfer: dt}));",
    driver.findElement(tid("draggable-box")), driver.findElement(tid("accept-zone")));
String path = new java.io.File("src/test/resources/sample.txt").getAbsolutePath();
driver.findElement(tid("file-input")).sendKeys(path);
assertTrue(driver.findElement(tid("uploaded-file-info")).getText().contains("sample.txt"));
new Actions(driver).moveToElement(driver.findElement(tid("tooltip-trigger"))).perform();
wait.until(visibilityOfElementLocated(tid("tooltip")));

13Wait for dynamically loaded content

Practise on /dynamic/dynamic-loading โ†’
driver.findElement(tid("loading-1-start")).click();
WebElement result = wait.until(visibilityOfElementLocated(tid("loading-1-result")));
assertTrue(result.getText().contains("Hello World!"));
SearchContext root = driver.findElement(By.cssSelector("shadow-counter")).getShadowRoot();
root.findElement(By.cssSelector("#inc")).click();
assertEquals("Count: 1", root.findElement(By.cssSelector("#count")).getText());
driver.findElement(tid("key-input")).sendKeys(Keys.ENTER);
assertEquals("You entered: Enter", driver.findElement(tid("key-result")).getText());

Practising another tool?