|
1 | 1 | package dev.selenium.elements;
|
2 |
| -import org.openqa.selenium.By; |
3 |
| -import org.openqa.selenium.support.pagefactory.ByAll; |
4 |
| -import org.openqa.selenium.support.pagefactory.ByChained; |
5 |
| -import dev.selenium.BaseTest; |
6 |
| -import java.util.List; |
7 | 2 |
|
| 3 | +import dev.selenium.BaseTest; |
| 4 | +import org.openqa.selenium.By; |
8 | 5 | import org.openqa.selenium.WebDriver;
|
9 | 6 | import org.openqa.selenium.WebElement;
|
10 | 7 | import org.openqa.selenium.chrome.ChromeDriver;
|
| 8 | +import org.openqa.selenium.support.pagefactory.ByAll; |
| 9 | +import org.openqa.selenium.support.pagefactory.ByChained; |
| 10 | + |
| 11 | +import java.util.List; |
11 | 12 |
|
12 | 13 | public class LocatorsTest extends BaseTest {
|
13 | 14 |
|
| 15 | + public void findElementByClassName() { |
| 16 | + WebDriver driver = new ChromeDriver(); |
| 17 | + driver.get("https://www.selenium.dev/selenium/web/locators_tests/locators.html"); |
| 18 | + |
| 19 | + // Find element by class name |
| 20 | + WebElement element = driver.findElement(By.className("information")); |
| 21 | + } |
| 22 | + |
| 23 | + public void findElementByCssSelector() { |
| 24 | + WebDriver driver = new ChromeDriver(); |
| 25 | + driver.get("https://www.selenium.dev/selenium/web/locators_tests/locators.html"); |
| 26 | + |
| 27 | + // Find element by css selector |
| 28 | + WebElement element = driver.findElement(By.cssSelector("#fname")); |
| 29 | + } |
| 30 | + |
| 31 | + public void findElementById() { |
| 32 | + WebDriver driver = new ChromeDriver(); |
| 33 | + driver.get("https://www.selenium.dev/selenium/web/locators_tests/locators.html"); |
| 34 | + |
| 35 | + // Find element by id |
| 36 | + WebElement element = driver.findElement(By.id("lname")); |
| 37 | + } |
| 38 | + |
| 39 | + public void findElementByName() { |
| 40 | + WebDriver driver = new ChromeDriver(); |
| 41 | + driver.get("https://www.selenium.dev/selenium/web/locators_tests/locators.html"); |
| 42 | + |
| 43 | + // Find element by name |
| 44 | + WebElement element = driver.findElement(By.name("newsletter")); |
| 45 | + } |
| 46 | + |
| 47 | + public void findElementByLinkText() { |
| 48 | + WebDriver driver = new ChromeDriver(); |
| 49 | + driver.get("https://www.selenium.dev/selenium/web/locators_tests/locators.html"); |
| 50 | + |
| 51 | + // Find element by link text |
| 52 | + WebElement element = driver.findElement(By.linkText("Selenium Official Page")); |
| 53 | + } |
| 54 | + |
| 55 | + public void findElementByPartialLinkText() { |
| 56 | + WebDriver driver = new ChromeDriver(); |
| 57 | + driver.get("https://www.selenium.dev/selenium/web/locators_tests/locators.html"); |
| 58 | + |
| 59 | + // Find element by partial link text |
| 60 | + WebElement element = driver.findElement(By.linkText("Official Page")); |
| 61 | + } |
| 62 | + |
| 63 | + public void findElementByTagName() { |
| 64 | + WebDriver driver = new ChromeDriver(); |
| 65 | + driver.get("https://www.selenium.dev/selenium/web/locators_tests/locators.html"); |
| 66 | + |
| 67 | + // Find element by tag name |
| 68 | + WebElement element = driver.findElement(By.tagName("a")); |
| 69 | + } |
| 70 | + |
| 71 | + public void findElementByXpath() { |
| 72 | + WebDriver driver = new ChromeDriver(); |
| 73 | + driver.get("https://www.selenium.dev/selenium/web/locators_tests/locators.html"); |
| 74 | + |
| 75 | + // Find element by xpath |
| 76 | + WebElement element = driver.findElement(By.xpath("//input[@value='f']")); |
| 77 | + } |
| 78 | + |
| 79 | + public void findElementUsingBy() { |
| 80 | + WebDriver driver = new ChromeDriver(); |
| 81 | + driver.get("https://www.selenium.dev/selenium/web/locators_tests/locators.html"); |
| 82 | + |
| 83 | + // Define locators using different strategies shared above |
| 84 | + By informationLocator = By.className("information"); |
| 85 | + By firstNameLocator = By.cssSelector("#fname"); |
| 86 | + By lastNameLocator = By.id("lname"); |
| 87 | + By newsletterLocator = By.name("newsletter"); |
| 88 | + By linkTextLocator = By.linkText("Selenium Official Page"); |
| 89 | + By partialLinkTextLocator = By.partialLinkText("Official Page"); |
| 90 | + By tagNameLocator = By.tagName("a"); |
| 91 | + By xpathLocator = By.xpath("//input[@value='f']"); |
| 92 | + |
| 93 | + // Now we can directly use them in driver.findElement() |
| 94 | + WebElement informationElement = driver.findElement(informationLocator); |
| 95 | + WebElement firstNameElement = driver.findElement(firstNameLocator); |
| 96 | + WebElement lastNameElement = driver.findElement(lastNameLocator); |
| 97 | + WebElement newsletterElement = driver.findElement(newsletterLocator); |
| 98 | + WebElement linkTextElement = driver.findElement(linkTextLocator); |
| 99 | + WebElement partialLinkTextElement = driver.findElement(partialLinkTextLocator); |
| 100 | + WebElement tagNameElement = driver.findElement(tagNameLocator); |
| 101 | + WebElement xpathElement = driver.findElement(xpathLocator); |
| 102 | + } |
14 | 103 |
|
15 | 104 | public void ByAllTest() {
|
16 | 105 | // Create instance of ChromeDriver
|
|
0 commit comments