Skip to content

Commit fbe0935

Browse files
committed
Added login tests
1 parent ff430d2 commit fbe0935

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed
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+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.serenitydojo.playwright.toolshop.login;
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 org.assertj.core.api.Assertions.assertThat;
9+
10+
public class LoginWithRegisteredUserTest extends PlaywrightTestCase {
11+
12+
@Test
13+
@DisplayName("Should be able to login with a registered user")
14+
void should_login_with_registered_user() {
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 right account page
26+
assertThat(loginPage.title()).isEqualTo("My account");
27+
}
28+
29+
@Test
30+
@DisplayName("Should reject a user if they provide a wrong password")
31+
void should_reject_user_with_invalid_password() {
32+
User user = User.randomUser();
33+
UserAPIClient userAPIClient = new UserAPIClient(page);
34+
userAPIClient.registerUser(user);
35+
36+
LoginPage loginPage = new LoginPage(page);
37+
loginPage.open();
38+
loginPage.loginAs(user.withPassword("wrong-password"));
39+
40+
assertThat(loginPage.loginErrorMessage()).isEqualTo("Invalid email or password");
41+
}
42+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.serenitydojo.playwright.toolshop.login;
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+
private final Page page;
9+
10+
private static final String REGISTER_USER = "https://api.practicesoftwaretesting.com/users/register";
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,
19+
RequestOptions.create()
20+
.setData(user)
21+
.setHeader("Content-Type", "application/json")
22+
.setHeader("Accept", "application/json"));
23+
if (response.status() != 201) {
24+
throw new IllegalStateException("Could not create user: " + response.text());
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)