Skip to content

Commit 536693f

Browse files
committed
Merged updates from the allure reporting module
1 parent 3149aac commit 536693f

File tree

6 files changed

+68
-72
lines changed

6 files changed

+68
-72
lines changed

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import com.microsoft.playwright.Page;
44
import com.microsoft.playwright.junit.UsePlaywright;
5-
import com.serenitydojo.playwright.HeadlessChromeOptions;
6-
import com.serenitydojo.playwright.toolshop.fixtures.RecordsAllureScreenshots;
75
import com.serenitydojo.playwright.toolshop.catalog.pageobjects.*;
6+
import com.serenitydojo.playwright.toolshop.fixtures.ChromeHeadlessOptions;
7+
import com.serenitydojo.playwright.toolshop.fixtures.TakesFinalScreenshot;
88
import io.qameta.allure.Feature;
99
import io.qameta.allure.Story;
1010
import org.assertj.core.api.Assertions;
@@ -16,8 +16,8 @@
1616

1717
@DisplayName("Shopping Cart")
1818
@Feature("Shopping Cart")
19-
@UsePlaywright(HeadlessChromeOptions.class)
20-
public class AddToCartTest implements RecordsAllureScreenshots {
19+
@UsePlaywright(ChromeHeadlessOptions.class)
20+
public class AddToCartTest implements TakesFinalScreenshot {
2121

2222
SearchComponent searchComponent;
2323
ProductList productList;
@@ -26,8 +26,8 @@ public class AddToCartTest implements RecordsAllureScreenshots {
2626
CheckoutCart checkoutCart;
2727

2828
@BeforeEach
29-
void openHomePage(Page page) {
30-
page.navigate("https://practicesoftwaretesting.com");
29+
void openHomePage() {
30+
navBar.openHomePage();
3131
}
3232

3333
@BeforeEach
@@ -39,8 +39,9 @@ void setUp(Page page) {
3939
checkoutCart = new CheckoutCart(page);
4040
}
4141

42+
4243
@Test
43-
@Story("Check out")
44+
@Story("Checking out a product")
4445
@DisplayName("Checking out a single item")
4546
void whenCheckingOutASingleItem() {
4647
searchComponent.searchBy("pliers");
@@ -64,7 +65,7 @@ void whenCheckingOutASingleItem() {
6465
}
6566

6667
@Test
67-
@Story("Check out")
68+
@Story("Checking out a product")
6869
@DisplayName("Checking out multiple items")
6970
void whenCheckingOutMultipleItems() {
7071
navBar.openHomePage();

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

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import com.microsoft.playwright.Page;
44
import com.microsoft.playwright.junit.UsePlaywright;
5-
import com.serenitydojo.playwright.HeadlessChromeOptions;
6-
import com.serenitydojo.playwright.toolshop.fixtures.RecordsAllureScreenshots;
75
import com.serenitydojo.playwright.toolshop.catalog.pageobjects.ProductList;
86
import com.serenitydojo.playwright.toolshop.catalog.pageobjects.SearchComponent;
7+
import com.serenitydojo.playwright.toolshop.fixtures.ChromeHeadlessOptions;
8+
import com.serenitydojo.playwright.toolshop.fixtures.TakesFinalScreenshot;
99
import io.qameta.allure.Feature;
1010
import io.qameta.allure.Story;
1111
import org.assertj.core.api.Assertions;
@@ -15,9 +15,9 @@
1515
import org.junit.jupiter.api.Test;
1616

1717
@DisplayName("Searching for products")
18-
@Feature("Searching for products")
19-
@UsePlaywright(HeadlessChromeOptions.class)
20-
public class SearchForProductsTest implements RecordsAllureScreenshots {
18+
@Feature("Product Catalog")
19+
@UsePlaywright(ChromeHeadlessOptions.class)
20+
public class SearchForProductsTest implements TakesFinalScreenshot {
2121

2222
@BeforeEach
2323
void openHomePage(Page page) {
@@ -54,22 +54,21 @@ void whenThereIsNoMatchingProduct(Page page) {
5454
Assertions.assertThat(matchingProducts).isEmpty();
5555
Assertions.assertThat(productList.getSearchCompletedMessage()).contains("There are no products found.");
5656
}
57-
}
5857

59-
@Test
60-
@Story("Clearing the previous search results")
61-
@DisplayName("When the user clears a previous search results")
62-
void clearingTheSearchResults(Page page) {
63-
SearchComponent searchComponent = new SearchComponent(page);
64-
ProductList productList = new ProductList(page);
65-
searchComponent.searchBy("saw");
58+
@Test
59+
@DisplayName("When the user clears a previous search results")
60+
void clearingTheSearchResults(Page page) {
61+
SearchComponent searchComponent = new SearchComponent(page);
62+
ProductList productList = new ProductList(page);
63+
searchComponent.searchBy("saw");
6664

67-
var matchingFilteredProducts = productList.getProductNames();
68-
Assertions.assertThat(matchingFilteredProducts).hasSize(2);
65+
var matchingFilteredProducts = productList.getProductNames();
66+
Assertions.assertThat(matchingFilteredProducts).hasSize(2);
6967

70-
searchComponent.clearSearch();
68+
searchComponent.clearSearch();
7169

72-
var matchingProducts = productList.getProductNames();
73-
Assertions.assertThat(matchingProducts).hasSize(9);
70+
var matchingProducts = productList.getProductNames();
71+
Assertions.assertThat(matchingProducts).hasSize(9);
72+
}
7473
}
7574
}

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

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
package com.serenitydojo.playwright.toolshop.contact;
22

3-
import com.microsoft.playwright.BrowserContext;
43
import com.microsoft.playwright.Page;
5-
import com.microsoft.playwright.Tracing;
64
import com.microsoft.playwright.junit.UsePlaywright;
75
import com.microsoft.playwright.options.AriaRole;
8-
import com.serenitydojo.playwright.HeadlessChromeOptions;
9-
import com.serenitydojo.playwright.toolshop.fixtures.RecordsAllureScreenshots;
106
import com.serenitydojo.playwright.toolshop.catalog.pageobjects.NavBar;
7+
import com.serenitydojo.playwright.toolshop.fixtures.ChromeHeadlessOptions;
8+
import com.serenitydojo.playwright.toolshop.fixtures.TakesFinalScreenshot;
119
import io.qameta.allure.Feature;
12-
import io.qameta.allure.Step;
1310
import io.qameta.allure.Story;
1411
import org.assertj.core.api.Assertions;
15-
import org.junit.jupiter.api.*;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.DisplayName;
14+
import org.junit.jupiter.api.Test;
1615
import org.junit.jupiter.params.ParameterizedTest;
1716
import org.junit.jupiter.params.provider.ValueSource;
1817

@@ -23,9 +22,9 @@
2322
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
2423

2524
@DisplayName("Contact form")
26-
@Feature("Contact form")
27-
@UsePlaywright(HeadlessChromeOptions.class)
28-
public class ContactFormTest implements RecordsAllureScreenshots {
25+
@Feature("Contacts")
26+
@UsePlaywright(ChromeHeadlessOptions.class)
27+
public class ContactFormTest implements TakesFinalScreenshot {
2928

3029
ContactForm contactForm;
3130
NavBar navigate;
@@ -37,31 +36,10 @@ void openContactPage(Page page) {
3736
navigate.toTheContactPage();
3837
}
3938

40-
41-
@BeforeEach
42-
void setupTrace(BrowserContext context) {
43-
context.tracing().start(
44-
new Tracing.StartOptions()
45-
.setScreenshots(true)
46-
.setSnapshots(true)
47-
.setSources(true)
48-
);
49-
}
50-
51-
@AfterEach
52-
void recordTrace(TestInfo testInfo, BrowserContext context) {
53-
String traceName = testInfo.getDisplayName().replace(" ", "-").toLowerCase();
54-
context.tracing().stop(
55-
new Tracing.StopOptions()
56-
.setPath(Paths.get("target/trace-" + traceName + ".zip"))
57-
);
58-
}
59-
60-
61-
@Story("Submitting a request")
39+
@Story("Contact form")
6240
@DisplayName("Customers can use the contact form to contact us")
6341
@Test
64-
void completeForm(Page page) throws URISyntaxException {
42+
void completeForm() throws URISyntaxException {
6543
contactForm.setFirstName("Sarah-Jane");
6644
contactForm.setLastName("Smith");
6745
contactForm.setEmail("[email protected]");
@@ -71,19 +49,13 @@ void completeForm(Page page) throws URISyntaxException {
7149
Path fileToUpload = Paths.get(ClassLoader.getSystemResource("data/sample-data.txt").toURI());
7250
contactForm.setAttachment(fileToUpload);
7351

74-
recordScreenshot(page, "Submit form");
7552
contactForm.submitForm();
7653

7754
Assertions.assertThat(contactForm.getAlertMessage())
7855
.contains("Thanks for your message! We will contact you shortly.");
7956
}
8057

81-
@Step
82-
void submitTheForm(Page page) {
83-
84-
}
85-
86-
@Story("Submitting a request")
58+
@Story("Contact form")
8759
@DisplayName("First name, last name, email and message are mandatory")
8860
@ParameterizedTest(name = "{arguments} is a mandatory field")
8961
@ValueSource(strings = {"First name", "Last name", "Email", "Message"})
@@ -98,7 +70,6 @@ void mandatoryFields(String fieldName, Page page) {
9870
// Clear one of the fields
9971
contactForm.clearField(fieldName);
10072

101-
recordScreenshot(page, "Submit form");
10273
contactForm.submitForm();
10374

10475
// Check the error message for that field
@@ -107,7 +78,7 @@ void mandatoryFields(String fieldName, Page page) {
10778
assertThat(errorMessage).isVisible();
10879
}
10980

110-
@Story("Submitting a request")
81+
@Story("Contact form")
11182
@DisplayName("The message must be at least 50 characters long")
11283
@Test
11384
void messageTooShort(Page page) {
@@ -118,13 +89,12 @@ void messageTooShort(Page page) {
11889
contactForm.setMessage("A short long message.");
11990
contactForm.selectSubject("Warranty");
12091

121-
recordScreenshot(page, "Submit form");
12292
contactForm.submitForm();
12393

12494
assertThat(page.getByRole(AriaRole.ALERT)).hasText("Message must be minimal 50 characters");
12595
}
12696

127-
@Story("Submitting a request")
97+
@Story("Contact form")
12898
@DisplayName("The email address must be correctly formatted")
12999
@ParameterizedTest(name = "'{arguments}' should be rejected")
130100
@ValueSource(strings = {"not-an-email", "not-an.email.com", "notanemail"})
@@ -135,7 +105,6 @@ void invalidEmailField(String invalidEmail, Page page) {
135105
contactForm.setMessage("A very long message to the warranty service about a warranty on a product!");
136106
contactForm.selectSubject("Warranty");
137107

138-
recordScreenshot(page, "Submit form");
139108
contactForm.submitForm();
140109

141110
assertThat(page.getByRole(AriaRole.ALERT)).hasText("Email format is invalid");

src/test/java/com/serenitydojo/playwright/HeadlessChromeOptions.java renamed to src/test/java/com/serenitydojo/playwright/toolshop/fixtures/ChromeHeadlessOptions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
package com.serenitydojo.playwright;
1+
package com.serenitydojo.playwright.toolshop.fixtures;
22

33
import com.microsoft.playwright.BrowserType;
44
import com.microsoft.playwright.junit.Options;
55
import com.microsoft.playwright.junit.OptionsFactory;
66

77
import java.util.Arrays;
88

9-
public class HeadlessChromeOptions implements OptionsFactory {
9+
public class ChromeHeadlessOptions implements OptionsFactory {
1010
@Override
1111
public Options getOptions() {
1212
return new Options().setLaunchOptions(
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.serenitydojo.playwright.toolshop.fixtures;
2+
3+
import com.microsoft.playwright.Page;
4+
import io.qameta.allure.Allure;
5+
6+
import java.io.ByteArrayInputStream;
7+
8+
public class ScreenshotManager {
9+
public static void takeScreenshot(Page page, String name) {
10+
var screenshot = page.screenshot(
11+
new Page.ScreenshotOptions().setFullPage(true)
12+
);
13+
Allure.addAttachment(name, new ByteArrayInputStream(screenshot));
14+
}
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.serenitydojo.playwright.toolshop.fixtures;
2+
3+
import com.microsoft.playwright.Page;
4+
import org.junit.jupiter.api.AfterEach;
5+
6+
public interface TakesFinalScreenshot {
7+
8+
@AfterEach
9+
default void takeScreenshot(Page page) {
10+
ScreenshotManager.takeScreenshot(page, "Final screenshot");
11+
}
12+
}

0 commit comments

Comments
 (0)