Skip to content

Commit 356db5a

Browse files
committed
Sample locators
1 parent f2de3d4 commit 356db5a

File tree

1 file changed

+176
-0
lines changed

1 file changed

+176
-0
lines changed
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
package com.serenitydojo.playwright;
2+
3+
import com.microsoft.playwright.*;
4+
import com.microsoft.playwright.assertions.PlaywrightAssertions;
5+
import com.microsoft.playwright.options.AriaRole;
6+
import org.junit.jupiter.api.*;
7+
8+
import java.util.Arrays;
9+
import java.util.List;
10+
import java.util.regex.Pattern;
11+
12+
public class PlaywrightLocatorsTest {
13+
14+
private static Playwright playwright;
15+
private static Browser browser;
16+
private static BrowserContext browserContext;
17+
18+
Page page;
19+
20+
@BeforeAll
21+
static void setUpBrowser() {
22+
playwright = Playwright.create();
23+
browser = playwright.chromium().launch(
24+
new BrowserType.LaunchOptions().setHeadless(false)
25+
.setArgs(Arrays.asList("--no-sandbox", "--disable-extensions", "--disable-gpu"))
26+
);
27+
browserContext = browser.newContext();
28+
}
29+
30+
@BeforeEach
31+
void setUp() {
32+
page = browserContext.newPage();
33+
}
34+
35+
@AfterAll
36+
static void tearDown() {
37+
browser.close();
38+
playwright.close();
39+
}
40+
41+
@DisplayName("Locating elements using CSS")
42+
@Nested
43+
class LocatingElementsUsingCSS {
44+
45+
@BeforeEach
46+
void openContactPage() {
47+
page.navigate("https://practicesoftwaretesting.com/contact");
48+
}
49+
50+
@DisplayName("By id")
51+
@Test
52+
void locateTheFirstNameFieldByID() {
53+
page.locator("#first_name").fill("Sarah-Jane");
54+
PlaywrightAssertions.assertThat(page.locator("#first_name")).hasValue("Sarah-Jane");
55+
}
56+
57+
@DisplayName("By CSS class")
58+
@Test
59+
void locateTheSendButtonByCssClass() {
60+
page.locator("#first_name").fill("Sarah-Jane");
61+
page.locator(".btnSubmit").click();
62+
List<String> alertMessages = page.locator(".alert").allTextContents();
63+
Assertions.assertTrue(!alertMessages.isEmpty());
64+
65+
}
66+
67+
@DisplayName("By attribute")
68+
@Test
69+
void locateTheSendButtonByAttribute() {
70+
page.locator("input[placeholder='Your last name *']").fill("Smith");
71+
PlaywrightAssertions.assertThat(page.locator("#last_name")).hasValue("Smith");
72+
}
73+
}
74+
75+
@DisplayName("Locating elements by text using CSS")
76+
@Nested
77+
class LocatingElementsByTextUsingCSS {
78+
79+
@BeforeEach
80+
void openContactPage() {
81+
page.navigate("https://practicesoftwaretesting.com/contact");
82+
}
83+
84+
// :has-text matches any element containing specified text somewhere inside.
85+
@DisplayName("Using :has-text")
86+
@Test
87+
void locateTheSendButtonByText() {
88+
page.locator("#first_name").fill("Sarah-Jane");
89+
page.locator("#last_name").fill("Smith");
90+
page.locator("input:has-text('Send')").click();
91+
}
92+
93+
// :text matches the smallest element containing specified text.
94+
@DisplayName("Using :text")
95+
@Test
96+
void locateAProductItemByText() {
97+
page.locator(".navbar :text('Home')").click();
98+
page.locator(".card :text('Bolt')").click();
99+
PlaywrightAssertions.assertThat(page.locator("[data-test=product-name]")).hasText("Bolt Cutters");
100+
}
101+
102+
// Exact matches
103+
@DisplayName("Using :text-is")
104+
@Test
105+
void locateAProductItemByTextIs() {
106+
page.locator(".navbar :text('Home')").click();
107+
page.locator(".card :text-is('Bolt Cutters')").click();
108+
PlaywrightAssertions.assertThat(page.locator("[data-test=product-name]")).hasText("Bolt Cutters");
109+
}
110+
111+
// matching with regular expressions
112+
@DisplayName("Using :text-matches")
113+
@Test
114+
void locateAProductItemByTextMatches() {
115+
page.locator(".navbar :text('Home')").click();
116+
page.locator(".card :text-matches('Bolt \\\\w+')").click();
117+
PlaywrightAssertions.assertThat(page.locator("[data-test=product-name]")).hasText("Bolt Cutters");
118+
}
119+
}
120+
121+
@DisplayName("Locating visible elements")
122+
@Nested
123+
class LocatingVisibleElements {
124+
@BeforeEach
125+
void openContactPage() {
126+
page.navigate("https://practicesoftwaretesting.com");
127+
}
128+
129+
@DisplayName("Finding visible and invisible elements")
130+
@Test
131+
void locateVisibleAndInvisibleItems() {
132+
int dropdownItems = page.locator(".dropdown-item").count();
133+
Assertions.assertTrue(dropdownItems > 0);
134+
}
135+
136+
@DisplayName("Finding only visible elements")
137+
@Test
138+
void locateVisibleItems() {
139+
int dropdownItems = page.locator(".dropdown-item:visible").count();
140+
Assertions.assertTrue(dropdownItems == 0);
141+
}
142+
}
143+
144+
@DisplayName("Locating elements by role")
145+
@Nested
146+
class LocatingElementsByRole {
147+
148+
@DisplayName("Using the BUTTON role")
149+
@Test
150+
void byButton() {
151+
page.navigate("https://practicesoftwaretesting.com/contact");
152+
153+
List<String> errorMessages = page.getByRole(AriaRole.ALERT).allTextContents();
154+
155+
page.getByRole(AriaRole.BUTTON,
156+
new Page.GetByRoleOptions().setName("Sign in"))
157+
.click();
158+
Assertions.assertTrue(!errorMessages.isEmpty());
159+
}
160+
161+
@DisplayName("Using the HEADING role")
162+
@Test
163+
void byHeaderRole() {
164+
page.navigate("https://practicesoftwaretesting.com");
165+
166+
page.locator("#search-query").fill("Pliers");
167+
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Search"))
168+
.click();
169+
Locator searchHeading = page.getByRole(AriaRole.HEADING,
170+
new Page.GetByRoleOptions().setName(Pattern.compile("Searched for:.*")));
171+
172+
PlaywrightAssertions.assertThat(searchHeading).isVisible();
173+
PlaywrightAssertions.assertThat(searchHeading).hasText("Searched for: Pliers");
174+
}
175+
}
176+
}

0 commit comments

Comments
 (0)