Skip to content

Commit ab225d0

Browse files
committed
Sample code
1 parent 1e34ff0 commit ab225d0

File tree

7 files changed

+128
-136
lines changed

7 files changed

+128
-136
lines changed

README.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,24 @@ The branch structure of this repository follows the convention `sample-code/<mod
3232

3333
The table below maps each Git branch in this repository to its corresponding Udemy course module. Click the links to access the relevant lecture directly.
3434

35-
| **Module** | **Git Branch** | **Udemy Lecture Link** |
36-
|------------|--------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------|
37-
| Start Here | `sample-code/start-here` | [Start Here](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46063725#overview) |
38-
| Module 3 | `sample-code/module-3-my-first-playwright-test` | [My First Playwright Test](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46077299#overview) |
39-
| Module 4 | `sample-code/module-4-interacting-with-elements` | [Interacting With Field Elements](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46649323#overview) |
40-
| Module 5 | `sample-code/module-5-refactoring` | [Refactoring](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46077309#overview) |
41-
| Module 6 | `sample-code/module-6-browser-options` | [Browser Options](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46110387#overview) |
42-
| Module 7 | `sample-code/module-7-browser-contexts` | [Browser Contexts](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46145035#overview) |
43-
| Module 8 | `sample-code/module-8-locators` | [Locators](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46178143#overview) |
44-
| Module 9 | `sample-code/module-9-forms` | [Forms](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46649323#overview) |
45-
| Module 10 | `sample-code/module-10-assertions` | [Assertions](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46280267#overview) |
46-
| Module 11 | `sample-code/module-11-waits` | [Waits](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46680609#overview) |
47-
| Module 12 | `sample-code/module-12-mocking-api-calls` | [Mocking API Calls](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46728327#overview) |
48-
| Module 13 | `sample-code/module-13-page-objects` | [Page Objects](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46723143#overview) |
49-
| Module 14 | `sample-code/module-14-organizing-your-tests` | [Organizing Your Tests](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46833089#overview) |
50-
| Module 15 | `sample-code/module-15-parallel-execution` | [Parallel Execution](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46835495#overview) |
51-
| Module 16 | `sample-code/module-16-allure-reporting` | [Allure Reporting](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46849301#overview) |
52-
| Module 17 | `sample-code/module-17-cucumber` | [Cucumber](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46906341#overview) |
35+
| **Module** | **Git Branch** | **Udemy Lecture Link** |
36+
|--------------------|--------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------|
37+
| Start Here | `sample-code/start-here` | [Start Here](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46063725#overview) |
38+
| Module 3 | `sample-code/module-3-my-first-playwright-test` | [My First Playwright Test](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46077299#overview) |
39+
| Module 4 | `sample-code/module-4-interacting-with-elements` | [Interacting With Field Elements](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46649323#overview) |
40+
| Module 5 | `sample-code/module-5-refactoring` | [Refactoring](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46077309#overview) |
41+
| Module 6 | `sample-code/module-6-browser-options` | [Browser Options](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46110387#overview) |
42+
| Module 7 | `sample-code/module-7-browser-contexts` | [Browser Contexts](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46145035#overview) |
43+
| Module 8 | `sample-code/module-8-locators` | [Locators](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46178143#overview) |
44+
| Module 9 | `sample-code/module-9-forms` | [Forms](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46649323#overview) |
45+
| Module 10 | `sample-code/module-10-assertions` | [Assertions](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46280267#overview) |
46+
| Module 11 | `sample-code/module-11-waits` | [Waits](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46680609#overview) |
47+
| Module 12 | `sample-code/module-12-mocking-api-calls` | [Mocking API Calls](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46728327#overview) |
48+
| Module 13 | `sample-code/module-13-page-objects` | [Page Objects](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46723143#overview) |
49+
| Module 14 | `sample-code/module-14-organizing-your-tests` | [Organizing Your Tests](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46833089#overview) |
50+
| Module 15 | `sample-code/module-15-parallel-execution` | [Parallel Execution](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46835495#overview) |
51+
| Module 16 | `sample-code/module-16-allure-reporting` | [Allure Reporting](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46849301#overview) |
52+
| Module 17 | `sample-code/module-17-cucumber` | [Cucumber](https://www.udemy.com/course/mastering-modern-test-automation-with-playwright-in-java/learn/lecture/46906341#overview) |
5353

5454
---
5555

src/test/java/com/serenitydojo/playwright/toolshop/domain/User.java

Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,73 @@
33
import com.github.javafaker.Faker;
44

55
import java.time.LocalDate;
6-
import java.time.ZoneId;
76
import java.time.format.DateTimeFormatter;
87
import java.util.Date;
98

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
9+
/**
10+
* {
11+
* "first_name": "John",
12+
* "last_name": "Doe",
13+
* "address": "Street 1",
14+
* "city": "City",
15+
* "state": "State",
16+
* "country": "Country",
17+
* "postcode": "1234AA",
18+
* "phone": "0987654321",
19+
* "dob": "1970-01-01",
20+
* "password": "S1!uper-secret",
21+
* "email": "[email protected]"
22+
* }
23+
*/
24+
public record User(
25+
String first_name,
26+
String last_name,
27+
String address,
28+
String city,
29+
String state,
30+
String country,
31+
String postcode,
32+
String phone,
33+
String dob,
34+
String password,
35+
String email
2236
) {
2337
public static User randomUser() {
24-
Faker faker = new Faker();
38+
Faker fake = new Faker();
39+
int year = fake.number().numberBetween(1970,2000);
40+
int month = fake.number().numberBetween(1,12);
41+
int day = fake.number().numberBetween(1,28);
42+
LocalDate date = LocalDate.of(year,month,day);
43+
String formattedDate = date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
2544

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"));
2945
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(),
46+
fake.name().firstName(),
47+
fake.name().lastName(),
48+
fake.address().streetAddress(),
49+
fake.address().city(),
50+
fake.address().state(),
51+
fake.address().country(),
52+
fake.address().zipCode(),
53+
fake.phoneNumber().phoneNumber(),
3854
formattedDate,
39-
"Az123@#$",
40-
faker.internet().emailAddress()
55+
"Az1234£!3",
56+
fake.internet().emailAddress()
4157
);
4258
}
43-
}
44-
45-
46-
47-
48-
49-
50-
51-
5259

60+
public User withPassword(String password) {
61+
return new User(
62+
this.first_name,
63+
this.last_name,
64+
this.address,
65+
this.city,
66+
this.state,
67+
this.country,
68+
this.postcode,
69+
this.phone,
70+
this.dob,
71+
password,
72+
this.email
73+
);
74+
}
75+
}

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(false)
23+
.setHeadless(true)
2424
.setArgs(Arrays.asList("--no-sandbox", "--disable-extensions", "--disable-gpu"))
2525
)
2626
);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.serenitydojo.playwright.toolshop.login;
2+
3+
import com.microsoft.playwright.Page;
4+
import com.microsoft.playwright.options.AriaRole;
5+
import com.serenitydojo.playwright.toolshop.domain.User;
6+
7+
public class LoginPage {
8+
private final Page page;
9+
10+
public LoginPage(Page page) {
11+
this.page = page;
12+
}
13+
14+
public void open() {
15+
page.navigate("https://practicesoftwaretesting.com/auth/login");
16+
}
17+
18+
public void loginAs(User user) {
19+
page.getByPlaceholder("Your email").fill(user.email());
20+
page.getByPlaceholder("Your password").fill(user.password());
21+
page.getByRole(AriaRole.BUTTON,
22+
new Page.GetByRoleOptions().setName("Login")).click();
23+
24+
}
25+
26+
public String title() {
27+
return page.getByTestId("page-title").textContent();
28+
}
29+
30+
public String loginErrorMessage() {
31+
return page.getByTestId("login-error").textContent();
32+
}
33+
}

src/test/java/com/serenitydojo/playwright/toolshop/registration/LoginWithRegisteredUserTest.java renamed to src/test/java/com/serenitydojo/playwright/toolshop/login/LoginWithRegisteredUserTest.java

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
package com.serenitydojo.playwright.toolshop.registration;
1+
package com.serenitydojo.playwright.toolshop.login;
22

33
import com.serenitydojo.playwright.toolshop.domain.User;
44
import com.serenitydojo.playwright.toolshop.fixtures.PlaywrightTestCase;
55
import org.junit.jupiter.api.DisplayName;
66
import org.junit.jupiter.api.Test;
77

8-
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
public class LoginWithRegisteredUserTest extends PlaywrightTestCase {
911

10-
class LoginWithRegisteredUserTest extends PlaywrightTestCase {
11-
// Register a user with the /users/register API and then login with that user
1212
@Test
1313
@DisplayName("Should be able to login with a registered user")
14-
void shouldBeAbleToLoginWithARegisteredUser() {
14+
void should_login_with_registered_user() {
1515
// Register a user via the API
1616
User user = User.randomUser();
1717
UserAPIClient userAPIClient = new UserAPIClient(page);
@@ -22,38 +22,21 @@ void shouldBeAbleToLoginWithARegisteredUser() {
2222
loginPage.open();
2323
loginPage.loginAs(user);
2424

25-
// Check that we are on the account page
26-
assertThat(loginPage.title()).hasText("My account");
25+
// Check that we are on the right account page
26+
assertThat(loginPage.title()).isEqualTo("My account");
2727
}
2828

2929
@Test
30-
@DisplayName("Should not login if invalid credentials are provided")
31-
void shouldNotBeAbleToLoginWithInvalidCredentials() {
32-
// Register a user via the API
30+
@DisplayName("Should reject a user if they provide a wrong password")
31+
void should_reject_user_with_invalid_password() {
3332
User user = User.randomUser();
3433
UserAPIClient userAPIClient = new UserAPIClient(page);
3534
userAPIClient.registerUser(user);
3635

37-
// Login via the login page
3836
LoginPage loginPage = new LoginPage(page);
3937
loginPage.open();
40-
loginPage.loginWith(user.email(), "wrong-password");
38+
loginPage.loginAs(user.withPassword("wrong-password"));
4139

42-
// Check that we are on the account page
43-
assertThat(loginPage.alert()).hasText("Invalid email or password");
40+
assertThat(loginPage.loginErrorMessage()).isEqualTo("Invalid email or password");
4441
}
4542
}
46-
47-
48-
49-
50-
51-
52-
53-
54-
55-
56-
57-
58-
59-

src/test/java/com/serenitydojo/playwright/toolshop/registration/UserAPIClient.java renamed to src/test/java/com/serenitydojo/playwright/toolshop/login/UserAPIClient.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,27 @@
1-
package com.serenitydojo.playwright.toolshop.registration;
1+
package com.serenitydojo.playwright.toolshop.login;
22

33
import com.microsoft.playwright.Page;
44
import com.microsoft.playwright.options.RequestOptions;
55
import com.serenitydojo.playwright.toolshop.domain.User;
66

77
public class UserAPIClient {
8-
9-
private static final String REGISTER_USER_API = "https://api.practicesoftwaretesting.com/users/register";
108
private final Page page;
119

10+
private static final String REGISTER_USER = "https://api.practicesoftwaretesting.com/users/register";
11+
1212
public UserAPIClient(Page page) {
1313
this.page = page;
1414
}
1515

1616
public void registerUser(User user) {
1717
var response = page.request().post(
18-
REGISTER_USER_API,
18+
REGISTER_USER,
1919
RequestOptions.create()
2020
.setData(user)
2121
.setHeader("Content-Type", "application/json")
22-
.setHeader("Accept", "application/json")
23-
);
22+
.setHeader("Accept", "application/json"));
2423
if (response.status() != 201) {
25-
throw new IllegalArgumentException("Could not create user: " + response.text());
24+
throw new IllegalStateException("Could not create user: " + response.text());
2625
}
2726
}
2827
}
29-
30-
31-
32-
33-
34-
35-
36-
37-
38-

src/test/java/com/serenitydojo/playwright/toolshop/registration/LoginPage.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)