Skip to content

Commit 1e34ff0

Browse files
committed
Sample solution
1 parent b11efa5 commit 1e34ff0

File tree

6 files changed

+191
-1
lines changed

6 files changed

+191
-1
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@
9090
<artifactId>allure-cucumber7-jvm</artifactId>
9191
<scope>test</scope>
9292
</dependency>
93+
<dependency>
94+
<groupId>com.github.javafaker</groupId>
95+
<artifactId>javafaker</artifactId>
96+
<version>1.0.2</version>
97+
</dependency>
9398
</dependencies>
9499
<build>
95100
<plugins>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.serenitydojo.playwright.toolshop.domain;
2+
3+
import com.github.javafaker.Faker;
4+
5+
import java.time.LocalDate;
6+
import java.time.ZoneId;
7+
import java.time.format.DateTimeFormatter;
8+
import java.util.Date;
9+
10+
public record User (
11+
String first_name,
12+
String last_name,
13+
String address,
14+
String city,
15+
String state,
16+
String country,
17+
String postcode,
18+
String phone,
19+
String dob,
20+
String password,
21+
String email
22+
) {
23+
public static User randomUser() {
24+
Faker faker = new Faker();
25+
26+
Date birthday = faker.date().birthday();
27+
LocalDate localDate = birthday.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
28+
String formattedDate = localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
29+
return new User(
30+
faker.name().firstName(),
31+
faker.name().lastName(),
32+
faker.address().streetAddress(),
33+
faker.address().city(),
34+
faker.address().state(),
35+
faker.address().country(),
36+
faker.address().zipCode(),
37+
faker.phoneNumber().phoneNumber(),
38+
formattedDate,
39+
"Az123@#$",
40+
faker.internet().emailAddress()
41+
);
42+
}
43+
}
44+
45+
46+
47+
48+
49+
50+
51+
52+

src/test/java/com/serenitydojo/playwright/toolshop/fixtures/PlaywrightTestCase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public abstract class PlaywrightTestCase {
2020
protected static ThreadLocal<Browser> browser = ThreadLocal.withInitial(() ->
2121
playwright.get().chromium().launch(
2222
new BrowserType.LaunchOptions()
23-
.setHeadless(true)
23+
.setHeadless(false)
2424
.setArgs(Arrays.asList("--no-sandbox", "--disable-extensions", "--disable-gpu"))
2525
)
2626
);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.serenitydojo.playwright.toolshop.registration;
2+
3+
import com.microsoft.playwright.Locator;
4+
import com.microsoft.playwright.Page;
5+
import com.microsoft.playwright.options.AriaRole;
6+
import com.serenitydojo.playwright.toolshop.domain.User;
7+
8+
public class LoginPage {
9+
private final Page page;
10+
11+
public LoginPage(Page page) {
12+
this.page = page;
13+
}
14+
15+
public void open() {
16+
page.navigate("https://practicesoftwaretesting.com/auth/login");
17+
}
18+
19+
public void loginAs(User user) {
20+
loginWith(user.email(), user.password());
21+
}
22+
23+
public Locator title() {
24+
return page.getByTestId("page-title");
25+
}
26+
27+
public void loginWith(String email, String password) {
28+
page.getByPlaceholder("Your email").fill(email);
29+
page.getByPlaceholder("Your password").fill(password);
30+
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Login")).click();
31+
}
32+
33+
public Locator alert() {
34+
return page.getByTestId("login-error");
35+
}
36+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.serenitydojo.playwright.toolshop.registration;
2+
3+
import com.serenitydojo.playwright.toolshop.domain.User;
4+
import com.serenitydojo.playwright.toolshop.fixtures.PlaywrightTestCase;
5+
import org.junit.jupiter.api.DisplayName;
6+
import org.junit.jupiter.api.Test;
7+
8+
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
9+
10+
class LoginWithRegisteredUserTest extends PlaywrightTestCase {
11+
// Register a user with the /users/register API and then login with that user
12+
@Test
13+
@DisplayName("Should be able to login with a registered user")
14+
void shouldBeAbleToLoginWithARegisteredUser() {
15+
// Register a user via the API
16+
User user = User.randomUser();
17+
UserAPIClient userAPIClient = new UserAPIClient(page);
18+
userAPIClient.registerUser(user);
19+
20+
// Login via the login page
21+
LoginPage loginPage = new LoginPage(page);
22+
loginPage.open();
23+
loginPage.loginAs(user);
24+
25+
// Check that we are on the account page
26+
assertThat(loginPage.title()).hasText("My account");
27+
}
28+
29+
@Test
30+
@DisplayName("Should not login if invalid credentials are provided")
31+
void shouldNotBeAbleToLoginWithInvalidCredentials() {
32+
// Register a user via the API
33+
User user = User.randomUser();
34+
UserAPIClient userAPIClient = new UserAPIClient(page);
35+
userAPIClient.registerUser(user);
36+
37+
// Login via the login page
38+
LoginPage loginPage = new LoginPage(page);
39+
loginPage.open();
40+
loginPage.loginWith(user.email(), "wrong-password");
41+
42+
// Check that we are on the account page
43+
assertThat(loginPage.alert()).hasText("Invalid email or password");
44+
}
45+
}
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
59+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.serenitydojo.playwright.toolshop.registration;
2+
3+
import com.microsoft.playwright.Page;
4+
import com.microsoft.playwright.options.RequestOptions;
5+
import com.serenitydojo.playwright.toolshop.domain.User;
6+
7+
public class UserAPIClient {
8+
9+
private static final String REGISTER_USER_API = "https://api.practicesoftwaretesting.com/users/register";
10+
private final Page page;
11+
12+
public UserAPIClient(Page page) {
13+
this.page = page;
14+
}
15+
16+
public void registerUser(User user) {
17+
var response = page.request().post(
18+
REGISTER_USER_API,
19+
RequestOptions.create()
20+
.setData(user)
21+
.setHeader("Content-Type", "application/json")
22+
.setHeader("Accept", "application/json")
23+
);
24+
if (response.status() != 201) {
25+
throw new IllegalArgumentException("Could not create user: " + response.text());
26+
}
27+
}
28+
}
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+

0 commit comments

Comments
 (0)