Skip to content

Commit 086ec86

Browse files
committed
Added AssertJ examples
1 parent 6652af2 commit 086ec86

File tree

1 file changed

+21
-345
lines changed

1 file changed

+21
-345
lines changed

src/test/java/com/serenitydojo/playwright/PlaywrightAssertionsTest.java

Lines changed: 21 additions & 345 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.microsoft.playwright.options.AriaRole;
77
import com.microsoft.playwright.options.LoadState;
88
import com.microsoft.playwright.options.SelectOption;
9+
import org.assertj.core.api.Assertions;
910
import org.junit.jupiter.api.*;
1011
import org.junit.jupiter.api.parallel.Execution;
1112
import org.junit.jupiter.api.parallel.ExecutionMode;
@@ -28,6 +29,7 @@ public class PlaywrightAssertionsTest {
2829
@BeforeAll
2930
static void setUpBrowser() {
3031
playwright = Playwright.create();
32+
playwright.selectors().setTestIdAttribute("data-test");
3133
browser = playwright.chromium().launch(
3234
new BrowserType.LaunchOptions().setHeadless(true)
3335
.setArgs(Arrays.asList("--no-sandbox", "--disable-extensions", "--disable-gpu"))
@@ -93,361 +95,35 @@ void dropdownFieldValues() {
9395

9496
assertThat(subjectField).hasValue("warranty");
9597
}
96-
97-
98-
99-
100-
101-
102-
103-
104-
105-
106-
107-
108-
109-
110-
111-
112-
113-
114-
115-
116-
117-
118-
119-
120-
121-
122-
123-
124-
125-
126-
@DisplayName("By CSS class")
127-
@Test
128-
void locateTheSendButtonByCssClass() {
129-
page.locator("#first_name").fill("Sarah-Jane");
130-
page.locator(".btnSubmit").click();
131-
List<String> alertMessages = page.locator(".alert").allTextContents();
132-
Assertions.assertTrue(!alertMessages.isEmpty());
133-
134-
}
135-
136-
@DisplayName("By attribute")
137-
@Test
138-
void locateTheSendButtonByAttribute() {
139-
page.locator("input[placeholder='Your last name *']").fill("Smith");
140-
assertThat(page.locator("#last_name")).hasValue("Smith");
141-
}
14298
}
14399

144-
@DisplayName("Locating elements by text using CSS")
100+
@DisplayName("Making assertions about data values")
145101
@Nested
146-
class LocatingElementsByTextUsingCSS {
147-
148-
@BeforeEach
149-
void openContactPage() {
150-
page.navigate("https://practicesoftwaretesting.com/contact");
151-
}
152-
153-
// :has-text matches any element containing specified text somewhere inside.
154-
@DisplayName("Using :has-text")
155-
@Test
156-
void locateTheSendButtonByText() {
157-
page.locator("#first_name").fill("Sarah-Jane");
158-
page.locator("#last_name").fill("Smith");
159-
page.locator("input:has-text('Send')").click();
160-
}
161-
162-
// :text matches the smallest element containing specified text.
163-
@DisplayName("Using :text")
164-
@Test
165-
void locateAProductItemByText() {
166-
page.locator(".navbar :text('Home')").click();
167-
page.locator(".card :text('Bolt')").click();
168-
assertThat(page.locator("[data-test=product-name]")).hasText("Bolt Cutters");
169-
}
170-
171-
// Exact matches
172-
@DisplayName("Using :text-is")
173-
@Test
174-
void locateAProductItemByTextIs() {
175-
page.locator(".navbar :text('Home')").click();
176-
page.locator(".card :text-is('Bolt Cutters')").click();
177-
assertThat(page.locator("[data-test=product-name]")).hasText("Bolt Cutters");
178-
}
102+
class MakingAssertionsAboutDataValues {
179103

180-
// matching with regular expressions
181-
@DisplayName("Using :text-matches")
182-
@Test
183-
void locateAProductItemByTextMatches() {
184-
page.locator(".navbar :text('Home')").click();
185-
page.locator(".card :text-matches('Bolt \\\\w+')").click();
186-
assertThat(page.locator("[data-test=product-name]")).hasText("Bolt Cutters");
187-
}
188-
}
189-
190-
@DisplayName("Locating visible elements")
191-
@Nested
192-
class LocatingVisibleElements {
193104
@BeforeEach
194-
void openContactPage() {
195-
openPage();
196-
}
197-
198-
@DisplayName("Finding visible and invisible elements")
199-
@Test
200-
void locateVisibleAndInvisibleItems() {
201-
int dropdownItems = page.locator(".dropdown-item").count();
202-
Assertions.assertTrue(dropdownItems > 0);
203-
}
204-
205-
@DisplayName("Finding only visible elements")
206-
@Test
207-
void locateVisibleItems() {
208-
int dropdownItems = page.locator(".dropdown-item:visible").count();
209-
Assertions.assertTrue(dropdownItems == 0);
210-
}
211-
}
212-
213-
@DisplayName("Locating elements by role")
214-
@Nested
215-
class LocatingElementsByRole {
216-
217-
@DisplayName("Using the BUTTON role")
218-
@Test
219-
void byButton() {
220-
page.navigate("https://practicesoftwaretesting.com/contact");
221-
222-
223-
page.getByRole(AriaRole.BUTTON,
224-
new Page.GetByRoleOptions().setName("Send"))
225-
.click();
226-
227-
List<String> errorMessages = page.getByRole(AriaRole.ALERT).allTextContents();
228-
Assertions.assertTrue(!errorMessages.isEmpty());
229-
}
230-
231-
@DisplayName("Using the HEADING role")
232-
@Test
233-
void byHeaderRole() {
234-
openPage();
235-
236-
page.locator("#search-query").fill("Pliers");
237-
238-
page.getByRole(AriaRole.BUTTON,
239-
new Page.GetByRoleOptions().setName("Search"))
240-
.click();
241-
242-
Locator searchHeading = page.getByRole(AriaRole.HEADING,
243-
new Page.GetByRoleOptions().setName(Pattern.compile("Searched for:.*")));
244-
245-
assertThat(searchHeading).isVisible();
246-
assertThat(searchHeading).hasText("Searched for: Pliers");
247-
}
248-
249-
@DisplayName("Using the HEADING role and level")
250-
@Test
251-
void byHeaderRoleLevel() {
252-
openPage();
253-
254-
List<String> level4Headings
255-
= page.getByRole(AriaRole.HEADING,
256-
new Page.GetByRoleOptions()
257-
.setName("Pliers")
258-
.setLevel(5))
259-
.allTextContents();
260-
261-
org.assertj.core.api.Assertions.assertThat(level4Headings).isNotEmpty();
262-
}
263-
264-
@DisplayName("Identifying checkboxes")
265-
@Test
266-
void byCheckboxes() {
267-
playwright.selectors().setTestIdAttribute("data-test");
268-
269-
openPage();
270-
page.getByLabel("Hammer").click();
271-
page.getByLabel("Chisels").click();
272-
page.getByLabel("Wrench").click();
273-
274-
int selectedCount =
275-
page.getByTestId("filters").
276-
getByRole(AriaRole.CHECKBOX,
277-
new Locator.GetByRoleOptions().setChecked(true))
278-
.count();
279-
280-
org.assertj.core.api.Assertions.assertThat(selectedCount).isEqualTo(3);
281-
282-
List<String> selectedOptions =
283-
page.getByTestId("filters").
284-
getByRole(AriaRole.CHECKBOX,
285-
new Locator.GetByRoleOptions().setChecked(true))
286-
.all()
287-
.stream()
288-
.map(Locator::inputValue)
289-
.toList();
290-
291-
org.assertj.core.api.Assertions.assertThat(selectedOptions).hasSize(3);
292-
}
293-
}
294-
295-
@DisplayName("Locating elements by placeholders and labels")
296-
@Nested
297-
class LocatingElementsByPlaceholdersAndLabels {
298-
299-
@DisplayName("Using a label")
300-
@Test
301-
void byLabel() {
302-
page.navigate("https://practicesoftwaretesting.com/contact");
303-
304-
page.getByLabel("First name").fill("Obi-Wan");
305-
page.getByLabel("Last name").fill("Kenobi");
306-
page.getByLabel("Email address").fill("[email protected]");
307-
page.getByLabel("Subject").selectOption(new SelectOption().setLabel("Customer service"));
308-
page.getByLabel("Message *").fill("Hello there");
309-
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Send"));
310-
}
311-
312-
@DisplayName("Using a placeholder text")
313-
@Test
314-
void byPlaceholder() {
315-
page.navigate("https://practicesoftwaretesting.com/contact");
316-
317-
page.getByPlaceholder("Your first name").fill("Obi-Wan");
318-
319-
page.getByPlaceholder("Your last name").fill("Kenobi");
320-
page.getByPlaceholder("Your email").fill("[email protected]");
321-
page.getByLabel("Subject").selectOption(new SelectOption().setLabel("Customer service"));
322-
page.getByLabel("Message *").fill("Hello there");
323-
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Send"));
324-
}
325-
}
326-
327-
@DisplayName("Locating elements by text")
328-
@Nested
329-
class LocatingElementsByText {
330-
331-
@BeforeEach
332-
void openTheCatalogPage() {
333-
openPage();
334-
}
335-
336-
@DisplayName("Locating an element by text contents")
337-
@Test
338-
void byText() {
339-
page.getByText("Bolt Cutters").click();
340-
341-
PlaywrightAssertions.assertThat(page.getByText("MightyCraft Hardware")).isVisible();
342-
}
343-
344-
@DisplayName("Using alt text")
345-
@Test
346-
void byAltText() {
347-
page.getByAltText("Combination Pliers").click();
348-
349-
PlaywrightAssertions.assertThat(page.getByText("ForgeFlex Tools")).isVisible();
350-
}
351-
352-
@DisplayName("Using title")
353-
@Test
354-
void byTitle() {
355-
page.getByAltText("Combination Pliers").click();
356-
357-
page.getByTitle("Practice Software Testing - Toolshop").click();
358-
}
359-
}
360-
361-
@DisplayName("Locating elements by test Id")
362-
@Nested
363-
class LocatingElementsByTestID {
364-
365-
@BeforeAll
366-
static void setTestId() {
367-
playwright.selectors().setTestIdAttribute("data-test");
368-
}
369-
370-
@DisplayName("Using a custom data-test field")
371-
@Test
372-
void byTestId() {
373-
openPage();
374-
375-
playwright.selectors().setTestIdAttribute("data-test");
376-
377-
page.getByTestId("search-query").fill("Pliers");
378-
page.getByTestId("search-submit").click();
379-
}
380-
381-
}
382-
383-
@DisplayName("Nested locators")
384-
@Nested
385-
class NestedLocators {
386-
387-
@BeforeAll
388-
static void setTestId() {
389-
playwright.selectors().setTestIdAttribute("data-test");
390-
}
391-
392-
@DisplayName("Using roles")
393-
@Test
394-
void locatingAMenuItemUsingRoles() {
395-
openPage();
396-
397-
page.getByRole(AriaRole.MENUBAR, new Page.GetByRoleOptions().setName("Main Menu"))
398-
.getByRole(AriaRole.MENUITEM, new Locator.GetByRoleOptions().setName("Home"))
399-
.click();
400-
}
401-
402-
@DisplayName("Using roles with other strategies")
403-
@Test
404-
void locatingAMenuItemUsingRolesAndOtherStrategies() {
405-
openPage();
406-
407-
page.getByRole(AriaRole.MENUBAR, new Page.GetByRoleOptions().setName("Main Menu"))
408-
.getByText("Home")
409-
.click();
410-
}
411-
412-
@DisplayName("filtering locators by text")
413-
@Test
414-
void filteringMenuItems() {
415-
openPage();
416-
417-
page.getByRole(AriaRole.MENUBAR, new Page.GetByRoleOptions().setName("Main Menu"))
418-
.getByText("Categories")
419-
.click();
420-
421-
page.getByRole(AriaRole.MENUBAR, new Page.GetByRoleOptions().setName("Main Menu"))
422-
.getByText("Power Tools")
423-
.click();
424-
105+
void openHomePage() {
106+
page.navigate("https://practicesoftwaretesting.com");
425107
page.waitForCondition(() -> page.getByTestId("product-name").count() > 0);
426-
427-
List<String> allProducts = page.getByTestId("product-name")
428-
.filter(new Locator.FilterOptions().setHasText("Sander"))
429-
.allTextContents();
430-
431-
org.assertj.core.api.Assertions.assertThat(allProducts).allMatch(name -> name.contains("Sander"));
432108
}
433109

434-
@DisplayName("filtering locators by locator")
435110
@Test
436-
void filteringMenuItemsByLocator() {
437-
openPage();;
438-
439-
List<String> allProducts = page.locator(".card")
440-
.filter(new Locator.FilterOptions().setHas(page.getByText("Out of stock")))
441-
.getByTestId("product-name")
442-
.allTextContents();
111+
void allProductPricesShouldBeCorrectValues() {
112+
List<Double> prices = page.getByTestId("product-price")
113+
.allInnerTexts()
114+
.stream()
115+
.map(price -> Double.parseDouble(price.replace("$","")))
116+
.toList();
443117

444-
org.assertj.core.api.Assertions.assertThat(allProducts).hasSize(1)
445-
.allMatch(name -> name.contains("Long Nose Pliers"));
118+
Assertions.assertThat(prices)
119+
.isNotEmpty()
120+
.allMatch(price -> price > 0)
121+
.doesNotContain(0.0)
122+
.allMatch(price -> price < 1000)
123+
.allSatisfy(price ->
124+
Assertions.assertThat(price)
125+
.isGreaterThan(0.0)
126+
.isLessThan(1000.0));
446127
}
447128
}
448-
449-
private void openPage() {
450-
page.navigate("https://practicesoftwaretesting.com");
451-
page.waitForLoadState(LoadState.NETWORKIDLE);
452-
}
453129
}

0 commit comments

Comments
 (0)