Skip to content

Commit acd0f51

Browse files
committed
Refactored tests
1 parent 0581a2e commit acd0f51

File tree

5 files changed

+44
-16
lines changed

5 files changed

+44
-16
lines changed
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/toolshop/catalog/AddToCartTest.java

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

3+
import com.microsoft.playwright.Page;
4+
import com.microsoft.playwright.junit.UsePlaywright;
5+
import com.serenitydojo.playwright.HeadlessChromeOptions;
36
import com.serenitydojo.playwright.toolshop.catalog.pageobjects.*;
4-
import com.serenitydojo.playwright.toolshop.fixtures.PlaywrightTestCase;
57
import io.qameta.allure.Feature;
68
import io.qameta.allure.Story;
79
import org.assertj.core.api.Assertions;
@@ -13,7 +15,8 @@
1315

1416
@DisplayName("Shopping Cart")
1517
@Feature("Shopping Cart")
16-
public class AddToCartTest extends PlaywrightTestCase {
18+
@UsePlaywright(HeadlessChromeOptions.class)
19+
public class AddToCartTest {
1720

1821
SearchComponent searchComponent;
1922
ProductList productList;
@@ -22,12 +25,12 @@ public class AddToCartTest extends PlaywrightTestCase {
2225
CheckoutCart checkoutCart;
2326

2427
@BeforeEach
25-
void openHomePage() {
28+
void openHomePage(Page page) {
2629
page.navigate("https://practicesoftwaretesting.com");
2730
}
2831

2932
@BeforeEach
30-
void setUp() {
33+
void setUp(Page page) {
3134
searchComponent = new SearchComponent(page);
3235
productList = new ProductList(page);
3336
productDetails = new ProductDetails(page);

src/test/java/com/serenitydojo/playwright/toolshop/catalog/SearchForProductsTest.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.serenitydojo.playwright.toolshop.catalog;
22

3+
import com.microsoft.playwright.Page;
4+
import com.microsoft.playwright.junit.UsePlaywright;
5+
import com.serenitydojo.playwright.HeadlessChromeOptions;
36
import com.serenitydojo.playwright.toolshop.catalog.pageobjects.ProductList;
47
import com.serenitydojo.playwright.toolshop.catalog.pageobjects.SearchComponent;
5-
import com.serenitydojo.playwright.toolshop.fixtures.PlaywrightTestCase;
68
import io.qameta.allure.Feature;
79
import io.qameta.allure.Story;
810
import org.assertj.core.api.Assertions;
@@ -13,10 +15,11 @@
1315

1416
@DisplayName("Searching for products")
1517
@Feature("Searching for products")
16-
public class SearchForProductsTest extends PlaywrightTestCase {
18+
@UsePlaywright(HeadlessChromeOptions.class)
19+
public class SearchForProductsTest {
1720

1821
@BeforeEach
19-
void openHomePage() {
22+
void openHomePage(Page page) {
2023
page.navigate("https://practicesoftwaretesting.com");
2124
}
2225

@@ -27,7 +30,7 @@ class SearchingByKeyword {
2730

2831
@Test
2932
@DisplayName("When there are matching results")
30-
void whenSearchingByKeyword() {
33+
void whenSearchingByKeyword(Page page) {
3134
SearchComponent searchComponent = new SearchComponent(page);
3235
ProductList productList = new ProductList(page);
3336

@@ -40,7 +43,7 @@ void whenSearchingByKeyword() {
4043

4144
@Test
4245
@DisplayName("When there are no matching results")
43-
void whenThereIsNoMatchingProduct() {
46+
void whenThereIsNoMatchingProduct(Page page) {
4447
SearchComponent searchComponent = new SearchComponent(page);
4548
ProductList productList = new ProductList(page);
4649
searchComponent.searchBy("unknown");
@@ -55,7 +58,7 @@ void whenThereIsNoMatchingProduct() {
5558
@Test
5659
@Story("Clearing the previous search results")
5760
@DisplayName("When the user clears a previous search results")
58-
void clearingTheSearchResults() {
61+
void clearingTheSearchResults(Page page) {
5962
SearchComponent searchComponent = new SearchComponent(page);
6063
ProductList productList = new ProductList(page);
6164
searchComponent.searchBy("saw");

src/test/java/com/serenitydojo/playwright/toolshop/contact/ContactFormTest.java

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

3+
import com.microsoft.playwright.Page;
4+
import com.microsoft.playwright.junit.UsePlaywright;
35
import com.microsoft.playwright.options.AriaRole;
6+
import com.serenitydojo.playwright.HeadlessChromeOptions;
47
import com.serenitydojo.playwright.toolshop.catalog.pageobjects.NavBar;
58
import com.serenitydojo.playwright.toolshop.fixtures.PlaywrightTestCase;
69
import io.qameta.allure.Allure;
@@ -21,13 +24,14 @@
2124

2225
@DisplayName("Contact form")
2326
@Feature("Contact form")
24-
public class ContactFormTest extends PlaywrightTestCase {
27+
@UsePlaywright(HeadlessChromeOptions.class)
28+
public class ContactFormTest {
2529

2630
ContactForm contactForm;
2731
NavBar navigate;
2832

2933
@BeforeEach
30-
void openContactPage() {
34+
void openContactPage(Page page) {
3135
contactForm = new ContactForm(page);
3236
navigate = new NavBar(page);
3337
navigate.toTheContactPage();
@@ -56,7 +60,7 @@ void completeForm() throws URISyntaxException {
5660
@DisplayName("First name, last name, email and message are mandatory")
5761
@ParameterizedTest(name = "{arguments} is a mandatory field")
5862
@ValueSource(strings = {"First name", "Last name", "Email", "Message"})
59-
void mandatoryFields(String fieldName) {
63+
void mandatoryFields(String fieldName, Page page) {
6064
// Fill in the field values
6165
contactForm.setFirstName("Sarah-Jane");
6266
contactForm.setLastName("Smith");
@@ -78,7 +82,7 @@ void mandatoryFields(String fieldName) {
7882
@Story("Submitting a request")
7983
@DisplayName("The message must be at least 50 characters long")
8084
@Test
81-
void messageTooShort() {
85+
void messageTooShort(Page page) {
8286

8387
contactForm.setFirstName("Sarah-Jane");
8488
contactForm.setLastName("Smith");
@@ -95,7 +99,7 @@ void messageTooShort() {
9599
@DisplayName("The email address must be correctly formatted")
96100
@ParameterizedTest(name = "'{arguments}' should be rejected")
97101
@ValueSource(strings = {"not-an-email", "not-an.email.com", "notanemail"})
98-
void invalidEmailField(String invalidEmail) {
102+
void invalidEmailField(String invalidEmail, Page page) {
99103
contactForm.setFirstName("Sarah-Jane");
100104
contactForm.setLastName("Smith");
101105
contactForm.setEmail(invalidEmail);

src/test/resources/junit-platform.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
junit.jupiter.execution.parallel.enabled=true
22

3-
junit.jupiter.execution.parallel.mode.default=concurrent
3+
junit.jupiter.execution.parallel.mode.default=same_thread
44
junit.jupiter.execution.parallel.mode.classes.default=concurrent
55
junit.jupiter.execution.parallel.console.mode=verbose
66

0 commit comments

Comments
 (0)