From 52e16ef8981042bc9bbc144f217ee9cf1e1ecd54 Mon Sep 17 00:00:00 2001 From: Dmytro Budym <46810751+dbudim@users.noreply.github.com> Date: Fri, 8 Mar 2024 17:01:58 +0100 Subject: [PATCH] Add code examples for article "Finding Element by Attribute in Selenium" (#16043) --- .../FindElementByAttributeManualTest.java | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 testing-modules/selenium-2/src/test/java/com/baeldung/selenium/find/FindElementByAttributeManualTest.java diff --git a/testing-modules/selenium-2/src/test/java/com/baeldung/selenium/find/FindElementByAttributeManualTest.java b/testing-modules/selenium-2/src/test/java/com/baeldung/selenium/find/FindElementByAttributeManualTest.java new file mode 100644 index 0000000000..e3c8e6fd11 --- /dev/null +++ b/testing-modules/selenium-2/src/test/java/com/baeldung/selenium/find/FindElementByAttributeManualTest.java @@ -0,0 +1,77 @@ +package com.baeldung.selenium.find; + +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import java.time.Duration; + +public class FindElementByAttributeManualTest { + + private WebDriver driver; + private static final Duration TIMEOUT = Duration.ofSeconds(10); + + @BeforeClass + public void init() { + driver = new ChromeDriver(); + } + + @Test + public void whenSearchByAttributeName_thenFindAllElementsWithAttribute() { + driver.get("https://the-internet.herokuapp.com/drag_and_drop"); + + new WebDriverWait(driver, TIMEOUT) + .until(ExpectedConditions.numberOfElementsToBe(By.cssSelector("[draggable]"), 2)); + } + + @Test + public void whenSearchByAttributeExactValue_thenFindExactMatchElement() { + driver.get("https://the-internet.herokuapp.com/forgot_password"); + + new WebDriverWait(driver, TIMEOUT) + .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("[type='submit']"))); + } + + @Test + public void whenSearchByAttributeStartValue_thenFindStartValueMatchElement() { + driver.get("https://the-internet.herokuapp.com/download"); + + new WebDriverWait(driver, TIMEOUT) + .until(ExpectedConditions.numberOfElementsToBeMoreThan(By.cssSelector("[href^='download/']"), 0)); + } + + @Test + public void whenSearchByAttributeEndValue_thenFindEndValueMatchElement() { + driver.get("https://the-internet.herokuapp.com/download"); + + new WebDriverWait(driver, TIMEOUT) + .until(ExpectedConditions.numberOfElementsToBeMoreThan(By.cssSelector("[href$='.pdf']"), 0)); + } + + @Test + public void whenSearchByAttributeContainsValue_thenFindContainsValueMatchElement() { + driver.get("https://the-internet.herokuapp.com/upload"); + + new WebDriverWait(driver, TIMEOUT) + .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("[id*='le-up']"))); + } + + @Test + public void whenSearchByAttributeClassValue_thenFindClassValueMatchElement() { + driver.get("https://the-internet.herokuapp.com/upload"); + + new WebDriverWait(driver, TIMEOUT) + .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("[class*='dz-clickable']"))); + } + + @AfterClass + public void tearDown() { + driver.quit(); + } + +}