Skip to content

Commit 18fe90b

Browse files
committed
Refactored the contact form assertions
1 parent 08db0f8 commit 18fe90b

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ public void submitForm() {
5858
sendButton.click();
5959
}
6060

61-
public String getAlertMessage() {
62-
return page.getByRole(AriaRole.ALERT).textContent();
61+
public Locator alertMessage() {
62+
return page.getByRole(AriaRole.ALERT);
6363
}
6464

6565
public void clearField(String fieldName) {

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

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

3+
import com.microsoft.playwright.Locator;
34
import com.microsoft.playwright.Page;
45
import com.microsoft.playwright.junit.UsePlaywright;
56
import com.microsoft.playwright.options.AriaRole;
67
import com.serenitydojo.playwright.toolshop.catalog.pageobjects.NavBar;
78
import com.serenitydojo.playwright.toolshop.fixtures.ChromeHeadlessOptions;
8-
import com.serenitydojo.playwright.toolshop.fixtures.ScreenshotManager;
99
import com.serenitydojo.playwright.toolshop.fixtures.TakesFinalScreenshot;
1010
import io.qameta.allure.Feature;
1111
import io.qameta.allure.Story;
@@ -40,7 +40,7 @@ void openContactPage(Page page) {
4040
@Story("Contact form")
4141
@DisplayName("Customers can use the contact form to contact us")
4242
@Test
43-
void completeForm() throws URISyntaxException {
43+
void completeForm(Page page) throws URISyntaxException {
4444
contactForm.setFirstName("Sarah-Jane");
4545
contactForm.setLastName("Smith");
4646
contactForm.setEmail("[email protected]");
@@ -52,8 +52,8 @@ void completeForm() throws URISyntaxException {
5252

5353
contactForm.submitForm();
5454

55-
Assertions.assertThat(contactForm.getAlertMessage())
56-
.contains("Thanks for your message! We will contact you shortly.");
55+
assertThat(contactForm.alertMessage()).isVisible();
56+
assertThat(contactForm.alertMessage()).hasText("Thanks for your message! We will contact you shortly.");
5757
}
5858

5959
@Story("Contact form")
@@ -74,9 +74,8 @@ void mandatoryFields(String fieldName, Page page) {
7474
contactForm.submitForm();
7575

7676
// Check the error message for that field
77-
var errorMessage = page.getByRole(AriaRole.ALERT).getByText(fieldName + " is required");
78-
79-
assertThat(errorMessage).isVisible();
77+
assertThat(contactForm.alertMessage()).isVisible();
78+
assertThat(contactForm.alertMessage()).hasText(fieldName + " is required");
8079
}
8180

8281
@Story("Contact form")
@@ -92,7 +91,8 @@ void messageTooShort(Page page) {
9291

9392
contactForm.submitForm();
9493

95-
assertThat(page.getByRole(AriaRole.ALERT)).hasText("Message must be minimal 50 characters");
94+
assertThat(contactForm.alertMessage()).isVisible();
95+
assertThat(contactForm.alertMessage()).hasText("Message must be minimal 50 characters");
9696
}
9797

9898
@Story("Contact form")
@@ -108,6 +108,7 @@ void invalidEmailField(String invalidEmail, Page page) {
108108

109109
contactForm.submitForm();
110110

111-
assertThat(page.getByRole(AriaRole.ALERT)).hasText("Email format is invalid");
111+
assertThat(contactForm.alertMessage()).isVisible();
112+
assertThat(contactForm.alertMessage()).hasText("Email format is invalid");
112113
}
113114
}

0 commit comments

Comments
 (0)