Skip to content

Commit 3ef10c3

Browse files
committed
Refactored tests using the UsePlaywright annotaiton
1 parent d8ea70a commit 3ef10c3

File tree

4 files changed

+118
-122
lines changed

4 files changed

+118
-122
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.serenitydojo.playwright;
2+
3+
import com.microsoft.playwright.*;
4+
import com.microsoft.playwright.assertions.PlaywrightAssertions;
5+
import com.microsoft.playwright.junit.UsePlaywright;
6+
import com.microsoft.playwright.options.AriaRole;
7+
import com.microsoft.playwright.options.LoadState;
8+
import com.microsoft.playwright.options.SelectOption;
9+
import org.assertj.core.api.Assertions;
10+
import org.junit.jupiter.api.*;
11+
12+
import java.util.Arrays;
13+
import java.util.List;
14+
import java.util.regex.Pattern;
15+
16+
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
17+
18+
@UsePlaywright(HeadlessChromeOptions.class)
19+
public class AddingItemsToTheCartTest {
20+
21+
@DisplayName("Search for pliers")
22+
@Test
23+
void searchForPliers(Page page) {
24+
page.onConsoleMessage(msg -> System.out.println(msg.text()));
25+
26+
page.navigate("https://practicesoftwaretesting.com");
27+
page.getByPlaceholder("Search").fill("Pliers");
28+
page.getByPlaceholder("Search").press("Enter");
29+
30+
page.waitForLoadState();
31+
page.waitForCondition( () -> page.getByTestId("product-name").count() > 0);
32+
33+
List<String> products = page.getByTestId("product-name").allTextContents();
34+
Assertions.assertThat(products.get(0)).containsIgnoringCase("Pliers");
35+
36+
assertThat(page.locator(".card")).hasCount(4);
37+
38+
List<String> productNames = page.getByTestId("product-name").allTextContents();
39+
Assertions.assertThat(productNames).allMatch(name -> name.contains("Pliers"));
40+
41+
Locator outOfStockItem = page.locator(".card")
42+
.filter(new Locator.FilterOptions().setHasText("Out of stock"))
43+
.getByTestId("product-name");
44+
45+
assertThat(outOfStockItem).hasCount(1);
46+
assertThat(outOfStockItem).hasText("Long Nose Pliers");
47+
}
48+
49+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.serenitydojo.playwright;
2+
3+
import com.microsoft.playwright.BrowserType;
4+
import com.microsoft.playwright.junit.Options;
5+
import com.microsoft.playwright.junit.OptionsFactory;
6+
7+
import java.util.Arrays;
8+
9+
public class HeadlessChromeOptions implements OptionsFactory {
10+
@Override
11+
public Options getOptions() {
12+
return new Options().setLaunchOptions(
13+
new BrowserType.LaunchOptions()
14+
.setArgs(Arrays.asList("--no-sandbox", "--disable-extensions", "--disable-gpu"))
15+
).setHeadless(true)
16+
.setTestIdAttribute("data-test");
17+
}
18+
}

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

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.serenitydojo.playwright;
22

33
import com.microsoft.playwright.*;
4+
import com.microsoft.playwright.junit.UsePlaywright;
45
import com.microsoft.playwright.options.AriaRole;
56
import com.microsoft.playwright.options.SelectOption;
67
import org.assertj.core.api.Assertions;
@@ -12,50 +13,22 @@
1213

1314
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
1415

16+
@UsePlaywright(HeadlessChromeOptions.class)
1517
public class PlaywrightCollectionsTest {
1618

17-
protected static Playwright playwright;
18-
protected static Browser browser;
19-
protected static BrowserContext browserContext;
20-
21-
Page page;
22-
23-
@BeforeAll
24-
static void setUpBrowser() {
25-
playwright = Playwright.create();
26-
browser = playwright.chromium().launch(
27-
new BrowserType.LaunchOptions().setHeadless(true)
28-
.setArgs(Arrays.asList("--no-sandbox", "--disable-extensions", "--disable-gpu"))
29-
);
30-
playwright.selectors().setTestIdAttribute("data-test");
31-
}
32-
3319
@BeforeEach
34-
void setUp() {
35-
browserContext = browser.newContext();
36-
page = browserContext.newPage();
37-
openPage();
38-
}
39-
40-
@AfterEach
41-
void closeContext() {
42-
browserContext.close();
43-
}
44-
45-
@AfterAll
46-
static void tearDown() {
47-
browser.close();
48-
playwright.close();
20+
void setUp(Page page) {
21+
openPage(page);
4922
}
5023

51-
private void openPage() {
24+
private void openPage(Page page) {
5225
page.navigate("https://practicesoftwaretesting.com");
5326
page.waitForCondition(() -> page.getByTestId("product-name").count() > 0);
5427
}
5528

5629
@DisplayName("Counting items in a list")
5730
@Test
58-
void countingItemsOnThePage() {
31+
void countingItemsOnThePage(Page page) {
5932

6033
int itemsOnThePage = page.locator(".card").count();
6134

@@ -64,23 +37,23 @@ void countingItemsOnThePage() {
6437

6538
@DisplayName("Finding the first matching item")
6639
@Test
67-
void findingTheFirstMatchingItem() {
40+
void findingTheFirstMatchingItem(Page page) {
6841

6942
page.locator(".card").first().click();
7043

7144
}
7245

7346
@DisplayName("Finding the nth matching item")
7447
@Test
75-
void findingNthMatchingItem() {
48+
void findingNthMatchingItem(Page page) {
7649

7750
page.locator(".card").nth(2).click();
7851

7952
}
8053

8154
@DisplayName("Finding the last matching item")
8255
@Test
83-
void findingLastMatchingItem() {
56+
void findingLastMatchingItem(Page page) {
8457

8558
page.locator(".card").last().click();
8659

@@ -92,7 +65,7 @@ class FindingTheTextInAList {
9265

9366
@DisplayName("and finding all the text values ")
9467
@Test
95-
void withAllTextContents() {
68+
void withAllTextContents(Page page) {
9669

9770
List<String> itemNames = page.getByTestId("product-name").allTextContents();
9871

@@ -110,7 +83,7 @@ void withAllTextContents() {
11083

11184
@DisplayName("and asserting with hasText")
11285
@Test
113-
void withHasText() {
86+
void withHasText(Page page) {
11487
assertThat(page.getByTestId("product-name"))
11588
.hasText(new String[]{
11689
" Combination Pliers ",

0 commit comments

Comments
 (0)