Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions examples/java/src/test/java/dev/selenium/elements/LocatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class LocatorTest {

private WebDriver driver;

@BeforeEach
public void setUp() {
// Initialize the driver before each test
driver = new ChromeDriver();
driver.get("https://www.selenium.dev/selenium/web/locators_tests/locators.html");
}

@AfterEach
public void tearDown() {
// Close the browser after each test
if (driver != null) {
driver.quit();
}
}

@Test
public void testClassName() {
WebElement element = driver.findElement(By.className("information"));
Assertions.assertNotNull(element);
Assertions.assertEquals("input", element.getTagName());
}

@Test
public void testCssSelector() {
WebElement element = driver.findElement(By.cssSelector("#fname"));
Assertions.assertNotNull(element);
Assertions.assertEquals("Jane", element.getAttribute("value"));
}

@Test
public void testId() {
WebElement element = driver.findElement(By.id("lname"));
Assertions.assertNotNull(element);
Assertions.assertEquals("Doe", element.getAttribute("value"));
}

@Test
public void testName() {
WebElement element = driver.findElement(By.name("newsletter"));
Assertions.assertNotNull(element);
Assertions.assertEquals("input", element.getTagName());
}

@Test
public void testLinkText() {
WebElement element = driver.findElement(By.linkText("Selenium Official Page"));
Assertions.assertNotNull(element);
Assertions.assertEquals("https://www.selenium.dev/", element.getAttribute("href"));
}

@Test
public void testPartialLinkText() {
WebElement element = driver.findElement(By.partialLinkText("Official Page"));
Assertions.assertNotNull(element);
Assertions.assertEquals("https://www.selenium.dev/", element.getAttribute("href"));
}

@Test
public void testTagName() {
WebElement element = driver.findElement(By.tagName("a"));
Assertions.assertNotNull(element);
Assertions.assertEquals("https://www.selenium.dev/", element.getAttribute("href"));
}

@Test
public void testXpath() {
WebElement element = driver.findElement(By.xpath("//input[@value='f']"));
Assertions.assertNotNull(element);
Assertions.assertEquals("radio", element.getAttribute("type"));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ available in Selenium.
{{< tabpane langEqualsHeader=true >}}
{{< badge-examples >}}
{{< tab header="Java" >}}
WebDriver driver = new ChromeDriver();
driver.findElement(By.className("information"));
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L31" >}}
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L7-L9" >}}
Expand Down Expand Up @@ -109,9 +108,8 @@ textbox, using css.

{{< tabpane langEqualsHeader=true >}}
{{< badge-examples >}}
{{< tab header="Java" >}}
WebDriver driver = new ChromeDriver();
driver.findElement(By.cssSelector("#fname"));
{{< tab header="Java" >}}
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L38" >}}
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L17-L19" >}}
Expand Down Expand Up @@ -140,9 +138,8 @@ We will identify the Last Name field using it.

{{< tabpane langEqualsHeader=true >}}
{{< badge-examples >}}
{{< tab header="Java" >}}
WebDriver driver = new ChromeDriver();
driver.findElement(By.id("lname"));
{{< tab header="Java" >}}
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L45" >}}
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L27-L29" >}}
Expand Down Expand Up @@ -173,8 +170,7 @@ We will identify the Newsletter checkbox using it.
{{< tabpane langEqualsHeader=true >}}
{{< badge-examples >}}
{{< tab header="Java" >}}
WebDriver driver = new ChromeDriver();
driver.findElement(By.name("newsletter"));
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L52" >}}
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L37-L39" >}}
Expand Down Expand Up @@ -203,8 +199,7 @@ In the HTML snippet shared, we have a link available, let's see how will we loca
{{< tabpane langEqualsHeader=true >}}
{{< badge-examples >}}
{{< tab header="Java" >}}
WebDriver driver = new ChromeDriver();
driver.findElement(By.linkText("Selenium Official Page"));
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L59" >}}
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L47-L49" >}}
Expand Down Expand Up @@ -234,8 +229,7 @@ In the HTML snippet shared, we have a link available, lets see how will we locat
{{< tabpane langEqualsHeader=true >}}
{{< badge-examples >}}
{{< tab header="Java" >}}
WebDriver driver = new ChromeDriver();
driver.findElement(By.partialLinkText("Official Page"));
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L66" >}}
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L57-L59" >}}
Expand All @@ -262,9 +256,8 @@ We can use the HTML TAG itself as a locator to identify the web element on the p
From the above HTML snippet shared, lets identify the link, using its html tag "a".
{{< tabpane langEqualsHeader=true >}}
{{< badge-examples >}}
{{< tab header="Java" >}}
WebDriver driver = new ChromeDriver();
driver.findElement(By.tagName("a"));
{{< tab header="Java" >}}
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L73" >}}
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L67-L69" >}}
Expand Down Expand Up @@ -297,9 +290,8 @@ first name text box. Let us create locator for female radio button using xpath.

{{< tabpane langEqualsHeader=true >}}
{{< badge-examples >}}
{{< tab header="Java" >}}
WebDriver driver = new ChromeDriver();
driver.findElement(By.xpath("//input[@value='f']"));
{{< tab header="Java" >}}
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L80" >}}
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L77-L79" >}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ above shown HTML snippet. We can identify these elements using the class name lo
available in Selenium.
{{< tabpane langEqualsHeader=true >}}
{{< tab header="Java" >}}
WebDriver driver = new ChromeDriver();
driver.findElement(By.className("information"));
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L31" >}}
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L7-L9" >}}
Expand Down Expand Up @@ -106,9 +105,8 @@ Let us see an example from above HTML snippet. We will create locator for First
textbox, using css.

{{< tabpane langEqualsHeader=true >}}
{{< tab header="Java" >}}
WebDriver driver = new ChromeDriver();
driver.findElement(By.cssSelector("#fname"));
{{< tab header="Java" >}}
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L38" >}}
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L17-L19" >}}
Expand Down Expand Up @@ -136,9 +134,8 @@ Generally the ID property should be unique for a element on the web page.
We will identify the Last Name field using it.

{{< tabpane langEqualsHeader=true >}}
{{< tab header="Java" >}}
WebDriver driver = new ChromeDriver();
driver.findElement(By.id("lname"));
{{< tab header="Java" >}}
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L45" >}}
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L27-L29" >}}
Expand Down Expand Up @@ -168,8 +165,7 @@ We will identify the Newsletter checkbox using it.

{{< tabpane langEqualsHeader=true >}}
{{< tab header="Java" >}}
WebDriver driver = new ChromeDriver();
driver.findElement(By.name("newsletter"));
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L52" >}}
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L37-L39" >}}
Expand All @@ -196,9 +192,8 @@ If the element we want to locate is a link, we can use the link text locator
to identify it on the web page. The link text is the text displayed of the link.
In the HTML snippet shared, we have a link available, lets see how will we locate it.
{{< tabpane langEqualsHeader=true >}}
{{< tab header="Java" >}}
WebDriver driver = new ChromeDriver();
driver.findElement(By.linkText("Selenium Official Page"));
{{< tab header="Java" >}}
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L59" >}}
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L47-L49" >}}
Expand Down Expand Up @@ -227,8 +222,7 @@ We can pass partial text as value.
In the HTML snippet shared, we have a link available, lets see how will we locate it.
{{< tabpane langEqualsHeader=true >}}
{{< tab header="Java" >}}
WebDriver driver = new ChromeDriver();
driver.findElement(By.partialLinkText("Official Page"));
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L66" >}}
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L57-L59" >}}
Expand All @@ -255,8 +249,7 @@ We can use the HTML TAG itself as a locator to identify the web element on the p
From the above HTML snippet shared, lets identify the link, using its html tag "a".
{{< tabpane langEqualsHeader=true >}}
{{< tab header="Java" >}}
WebDriver driver = new ChromeDriver();
driver.findElement(By.tagName("a"));
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L73" >}}
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L67-L69" >}}
Expand Down Expand Up @@ -288,9 +281,8 @@ Or the xpath could be relative. Example- //input[@name='fname']. This will retur
first name text box. Let us create locator for female radio button using xpath.

{{< tabpane langEqualsHeader=true >}}
{{< tab header="Java" >}}
WebDriver driver = new ChromeDriver();
driver.findElement(By.xpath("//input[@value='f']"));
{{< tab header="Java" >}}
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L80" >}}
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L77-L79" >}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ above shown HTML snippet. We can identify these elements using the class name lo
available in Selenium.
{{< tabpane langEqualsHeader=true >}}
{{< tab header="Java" >}}
WebDriver driver = new ChromeDriver();
driver.findElement(By.className("information"));
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L31" >}}
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L7-L9" >}}
Expand Down Expand Up @@ -109,9 +108,8 @@ Let us see an example from above HTML snippet. We will create locator for First
textbox, using css.

{{< tabpane langEqualsHeader=true >}}
{{< tab header="Java" >}}
WebDriver driver = new ChromeDriver();
driver.findElement(By.cssSelector("#fname"));
{{< tab header="Java" >}}
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L38" >}}
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L17-L19" >}}
Expand Down Expand Up @@ -140,8 +138,7 @@ We will identify the Last Name field using it.

{{< tabpane langEqualsHeader=true >}}
{{< tab header="Java" >}}
WebDriver driver = new ChromeDriver();
driver.findElement(By.id("lname"));
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L45" >}}
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L27-L29" >}}
Expand Down Expand Up @@ -171,8 +168,7 @@ We will identify the Newsletter checkbox using it.

{{< tabpane langEqualsHeader=true >}}
{{< tab header="Java" >}}
WebDriver driver = new ChromeDriver();
driver.findElement(By.name("newsletter"));
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L52" >}}
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L37-L39" >}}
Expand All @@ -199,9 +195,8 @@ If the element we want to locate is a link, we can use the link text locator
to identify it on the web page. The link text is the text displayed of the link.
In the HTML snippet shared, we have a link available, lets see how will we locate it.
{{< tabpane langEqualsHeader=true >}}
{{< tab header="Java" >}}
WebDriver driver = new ChromeDriver();
driver.findElement(By.linkText("Selenium Official Page"));
{{< tab header="Java" >}}
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L59" >}}
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L47-L49" >}}
Expand Down Expand Up @@ -230,8 +225,7 @@ We can pass partial text as value.
In the HTML snippet shared, we have a link available, lets see how will we locate it.
{{< tabpane langEqualsHeader=true >}}
{{< tab header="Java" >}}
WebDriver driver = new ChromeDriver();
driver.findElement(By.partialLinkText("Official Page"));
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L66" >}}
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L57-L59" >}}
Expand All @@ -257,9 +251,8 @@ In the HTML snippet shared, we have a link available, lets see how will we locat
We can use the HTML TAG itself as a locator to identify the web element on the page.
From the above HTML snippet shared, lets identify the link, using its html tag "a".
{{< tabpane langEqualsHeader=true >}}
{{< tab header="Java" >}}
WebDriver driver = new ChromeDriver();
driver.findElement(By.tagName("a"));
{{< tab header="Java" >}}
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L73" >}}
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L67-L69" >}}
Expand Down Expand Up @@ -291,9 +284,8 @@ Or the xpath could be relative. Example- //input[@name='fname']. This will retur
first name text box. Let us create locator for female radio button using xpath.

{{< tabpane langEqualsHeader=true >}}
{{< tab header="Java" >}}
WebDriver driver = new ChromeDriver();
driver.findElement(By.xpath("//input[@value='f']"));
{{< tab header="Java" >}}
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L80" >}}
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L77-L79" >}}
Expand Down
Loading
Loading