Skip to content

Commit 77549cd

Browse files
added locator code file for java for locator strategies Section and added content in diff languages files (#2583)
added locator file for java and added content in diff languages files for website
1 parent de91efd commit 77549cd

File tree

6 files changed

+132
-124
lines changed

6 files changed

+132
-124
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import org.junit.jupiter.api.AfterEach;
2+
import org.junit.jupiter.api.Assertions;
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
import org.openqa.selenium.By;
6+
import org.openqa.selenium.WebDriver;
7+
import org.openqa.selenium.WebElement;
8+
import org.openqa.selenium.chrome.ChromeDriver;
9+
10+
public class LocatorTest {
11+
12+
private WebDriver driver;
13+
14+
@BeforeEach
15+
public void setUp() {
16+
// Initialize the driver before each test
17+
driver = new ChromeDriver();
18+
driver.get("https://www.selenium.dev/selenium/web/locators_tests/locators.html");
19+
}
20+
21+
@AfterEach
22+
public void tearDown() {
23+
// Close the browser after each test
24+
if (driver != null) {
25+
driver.quit();
26+
}
27+
}
28+
29+
@Test
30+
public void testClassName() {
31+
WebElement element = driver.findElement(By.className("information"));
32+
Assertions.assertNotNull(element);
33+
Assertions.assertEquals("input", element.getTagName());
34+
}
35+
36+
@Test
37+
public void testCssSelector() {
38+
WebElement element = driver.findElement(By.cssSelector("#fname"));
39+
Assertions.assertNotNull(element);
40+
Assertions.assertEquals("Jane", element.getAttribute("value"));
41+
}
42+
43+
@Test
44+
public void testId() {
45+
WebElement element = driver.findElement(By.id("lname"));
46+
Assertions.assertNotNull(element);
47+
Assertions.assertEquals("Doe", element.getAttribute("value"));
48+
}
49+
50+
@Test
51+
public void testName() {
52+
WebElement element = driver.findElement(By.name("newsletter"));
53+
Assertions.assertNotNull(element);
54+
Assertions.assertEquals("input", element.getTagName());
55+
}
56+
57+
@Test
58+
public void testLinkText() {
59+
WebElement element = driver.findElement(By.linkText("Selenium Official Page"));
60+
Assertions.assertNotNull(element);
61+
Assertions.assertEquals("https://www.selenium.dev/", element.getAttribute("href"));
62+
}
63+
64+
@Test
65+
public void testPartialLinkText() {
66+
WebElement element = driver.findElement(By.partialLinkText("Official Page"));
67+
Assertions.assertNotNull(element);
68+
Assertions.assertEquals("https://www.selenium.dev/", element.getAttribute("href"));
69+
}
70+
71+
@Test
72+
public void testTagName() {
73+
WebElement element = driver.findElement(By.tagName("a"));
74+
Assertions.assertNotNull(element);
75+
Assertions.assertEquals("https://www.selenium.dev/", element.getAttribute("href"));
76+
}
77+
78+
@Test
79+
public void testXpath() {
80+
WebElement element = driver.findElement(By.xpath("//input[@value='f']"));
81+
Assertions.assertNotNull(element);
82+
Assertions.assertEquals("radio", element.getAttribute("type"));
83+
}
84+
}

examples/java/src/test/java/dev/selenium/elements/LocatorsTest.java

Lines changed: 0 additions & 44 deletions
This file was deleted.

website_and_docs/content/documentation/webdriver/elements/locators.en.md

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ available in Selenium.
7777
{{< tabpane langEqualsHeader=true >}}
7878
{{< badge-examples >}}
7979
{{< tab header="Java" >}}
80-
WebDriver driver = new ChromeDriver();
81-
driver.findElement(By.className("information"));
80+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L31" >}}
8281
{{< /tab >}}
8382
{{< tab header="Python" text=true >}}
8483
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L7-L9" >}}
@@ -109,9 +108,8 @@ textbox, using css.
109108

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

141139
{{< tabpane langEqualsHeader=true >}}
142140
{{< badge-examples >}}
143-
{{< tab header="Java" >}}
144-
WebDriver driver = new ChromeDriver();
145-
driver.findElement(By.id("lname"));
141+
{{< tab header="Java" >}}
142+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L45" >}}
146143
{{< /tab >}}
147144
{{< tab header="Python" text=true >}}
148145
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L27-L29" >}}
@@ -173,8 +170,7 @@ We will identify the Newsletter checkbox using it.
173170
{{< tabpane langEqualsHeader=true >}}
174171
{{< badge-examples >}}
175172
{{< tab header="Java" >}}
176-
WebDriver driver = new ChromeDriver();
177-
driver.findElement(By.name("newsletter"));
173+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L52" >}}
178174
{{< /tab >}}
179175
{{< tab header="Python" text=true >}}
180176
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L37-L39" >}}
@@ -203,8 +199,7 @@ In the HTML snippet shared, we have a link available, let's see how will we loca
203199
{{< tabpane langEqualsHeader=true >}}
204200
{{< badge-examples >}}
205201
{{< tab header="Java" >}}
206-
WebDriver driver = new ChromeDriver();
207-
driver.findElement(By.linkText("Selenium Official Page"));
202+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L59" >}}
208203
{{< /tab >}}
209204
{{< tab header="Python" text=true >}}
210205
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L47-L49" >}}
@@ -234,8 +229,7 @@ In the HTML snippet shared, we have a link available, lets see how will we locat
234229
{{< tabpane langEqualsHeader=true >}}
235230
{{< badge-examples >}}
236231
{{< tab header="Java" >}}
237-
WebDriver driver = new ChromeDriver();
238-
driver.findElement(By.partialLinkText("Official Page"));
232+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L66" >}}
239233
{{< /tab >}}
240234
{{< tab header="Python" text=true >}}
241235
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L57-L59" >}}
@@ -262,9 +256,8 @@ We can use the HTML TAG itself as a locator to identify the web element on the p
262256
From the above HTML snippet shared, lets identify the link, using its html tag "a".
263257
{{< tabpane langEqualsHeader=true >}}
264258
{{< badge-examples >}}
265-
{{< tab header="Java" >}}
266-
WebDriver driver = new ChromeDriver();
267-
driver.findElement(By.tagName("a"));
259+
{{< tab header="Java" >}}
260+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L73" >}}
268261
{{< /tab >}}
269262
{{< tab header="Python" text=true >}}
270263
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L67-L69" >}}
@@ -297,9 +290,8 @@ first name text box. Let us create locator for female radio button using xpath.
297290

298291
{{< tabpane langEqualsHeader=true >}}
299292
{{< badge-examples >}}
300-
{{< tab header="Java" >}}
301-
WebDriver driver = new ChromeDriver();
302-
driver.findElement(By.xpath("//input[@value='f']"));
293+
{{< tab header="Java" >}}
294+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L80" >}}
303295
{{< /tab >}}
304296
{{< tab header="Python" text=true >}}
305297
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L77-L79" >}}

website_and_docs/content/documentation/webdriver/elements/locators.ja.md

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ above shown HTML snippet. We can identify these elements using the class name lo
7575
available in Selenium.
7676
{{< tabpane langEqualsHeader=true >}}
7777
{{< tab header="Java" >}}
78-
WebDriver driver = new ChromeDriver();
79-
driver.findElement(By.className("information"));
78+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L31" >}}
8079
{{< /tab >}}
8180
{{< tab header="Python" text=true >}}
8281
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L7-L9" >}}
@@ -106,9 +105,8 @@ Let us see an example from above HTML snippet. We will create locator for First
106105
textbox, using css.
107106

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

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

169166
{{< tabpane langEqualsHeader=true >}}
170167
{{< tab header="Java" >}}
171-
WebDriver driver = new ChromeDriver();
172-
driver.findElement(By.name("newsletter"));
168+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L52" >}}
173169
{{< /tab >}}
174170
{{< tab header="Python" text=true >}}
175171
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L37-L39" >}}
@@ -196,9 +192,8 @@ If the element we want to locate is a link, we can use the link text locator
196192
to identify it on the web page. The link text is the text displayed of the link.
197193
In the HTML snippet shared, we have a link available, lets see how will we locate it.
198194
{{< tabpane langEqualsHeader=true >}}
199-
{{< tab header="Java" >}}
200-
WebDriver driver = new ChromeDriver();
201-
driver.findElement(By.linkText("Selenium Official Page"));
195+
{{< tab header="Java" >}}
196+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L59" >}}
202197
{{< /tab >}}
203198
{{< tab header="Python" text=true >}}
204199
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L47-L49" >}}
@@ -227,8 +222,7 @@ We can pass partial text as value.
227222
In the HTML snippet shared, we have a link available, lets see how will we locate it.
228223
{{< tabpane langEqualsHeader=true >}}
229224
{{< tab header="Java" >}}
230-
WebDriver driver = new ChromeDriver();
231-
driver.findElement(By.partialLinkText("Official Page"));
225+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L66" >}}
232226
{{< /tab >}}
233227
{{< tab header="Python" text=true >}}
234228
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L57-L59" >}}
@@ -255,8 +249,7 @@ We can use the HTML TAG itself as a locator to identify the web element on the p
255249
From the above HTML snippet shared, lets identify the link, using its html tag "a".
256250
{{< tabpane langEqualsHeader=true >}}
257251
{{< tab header="Java" >}}
258-
WebDriver driver = new ChromeDriver();
259-
driver.findElement(By.tagName("a"));
252+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L73" >}}
260253
{{< /tab >}}
261254
{{< tab header="Python" text=true >}}
262255
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L67-L69" >}}
@@ -288,9 +281,8 @@ Or the xpath could be relative. Example- //input[@name='fname']. This will retur
288281
first name text box. Let us create locator for female radio button using xpath.
289282

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

website_and_docs/content/documentation/webdriver/elements/locators.pt-br.md

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ above shown HTML snippet. We can identify these elements using the class name lo
7878
available in Selenium.
7979
{{< tabpane langEqualsHeader=true >}}
8080
{{< tab header="Java" >}}
81-
WebDriver driver = new ChromeDriver();
82-
driver.findElement(By.className("information"));
81+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L31" >}}
8382
{{< /tab >}}
8483
{{< tab header="Python" text=true >}}
8584
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L7-L9" >}}
@@ -109,9 +108,8 @@ Let us see an example from above HTML snippet. We will create locator for First
109108
textbox, using css.
110109

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

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

172169
{{< tabpane langEqualsHeader=true >}}
173170
{{< tab header="Java" >}}
174-
WebDriver driver = new ChromeDriver();
175-
driver.findElement(By.name("newsletter"));
171+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L52" >}}
176172
{{< /tab >}}
177173
{{< tab header="Python" text=true >}}
178174
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L37-L39" >}}
@@ -199,9 +195,8 @@ If the element we want to locate is a link, we can use the link text locator
199195
to identify it on the web page. The link text is the text displayed of the link.
200196
In the HTML snippet shared, we have a link available, lets see how will we locate it.
201197
{{< tabpane langEqualsHeader=true >}}
202-
{{< tab header="Java" >}}
203-
WebDriver driver = new ChromeDriver();
204-
driver.findElement(By.linkText("Selenium Official Page"));
198+
{{< tab header="Java" >}}
199+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L59" >}}
205200
{{< /tab >}}
206201
{{< tab header="Python" text=true >}}
207202
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L47-L49" >}}
@@ -230,8 +225,7 @@ We can pass partial text as value.
230225
In the HTML snippet shared, we have a link available, lets see how will we locate it.
231226
{{< tabpane langEqualsHeader=true >}}
232227
{{< tab header="Java" >}}
233-
WebDriver driver = new ChromeDriver();
234-
driver.findElement(By.partialLinkText("Official Page"));
228+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L66" >}}
235229
{{< /tab >}}
236230
{{< tab header="Python" text=true >}}
237231
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L57-L59" >}}
@@ -257,9 +251,8 @@ In the HTML snippet shared, we have a link available, lets see how will we locat
257251
We can use the HTML TAG itself as a locator to identify the web element on the page.
258252
From the above HTML snippet shared, lets identify the link, using its html tag "a".
259253
{{< tabpane langEqualsHeader=true >}}
260-
{{< tab header="Java" >}}
261-
WebDriver driver = new ChromeDriver();
262-
driver.findElement(By.tagName("a"));
254+
{{< tab header="Java" >}}
255+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/elements/LocatorTest.java#L73" >}}
263256
{{< /tab >}}
264257
{{< tab header="Python" text=true >}}
265258
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L67-L69" >}}
@@ -291,9 +284,8 @@ Or the xpath could be relative. Example- //input[@name='fname']. This will retur
291284
first name text box. Let us create locator for female radio button using xpath.
292285

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

0 commit comments

Comments
 (0)