Skip to content

Commit eedcce2

Browse files
committed
feat(registerPage.ts): intro the registerpage page object pattern
1 parent df35001 commit eedcce2

File tree

5 files changed

+72
-5
lines changed

5 files changed

+72
-5
lines changed

src/pages/Register.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const Register = () => {
4242
fullWidth
4343
id="name"
4444
label="Name"
45+
data-testid="name"
4546
autoFocus
4647
value={name}
4748
onChange={(e) => setName(e.target.value)}
@@ -54,6 +55,7 @@ const Register = () => {
5455
fullWidth
5556
id="email"
5657
label="Email Address"
58+
data-testid="email"
5759
name="email"
5860
value={email}
5961
onChange={(e) => setEmail(e.target.value)}
@@ -65,7 +67,8 @@ const Register = () => {
6567
fullWidth
6668
name="password"
6769
label="Password"
68-
type="password"
70+
type="textbox"
71+
data-testid="password"
6972
id="password"
7073
value={password}
7174
onChange={(e) => setPassword(e.target.value)}
@@ -75,6 +78,7 @@ const Register = () => {
7578

7679
<Button
7780
component={Link}
81+
data-testid="registerButton"
7882
fullWidth
7983
to="/home"
8084
variant="contained"
@@ -85,7 +89,7 @@ const Register = () => {
8589

8690
<Grid container justifyContent="flex-end">
8791
<Grid item>
88-
<Link to="/login">Already have an account? Login</Link>
92+
<Link to="/login" data-testid="loginLink">Already have an account? Login</Link>
8993
</Grid>
9094
</Grid>
9195
</Box>

tests/indexPage.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { WelcomePage } from './pages/welcomePage';
44

55
test.describe('Login Page', () => {
66
test('has title', async ({ page }) => {
7-
const playwrightDev = new IndexPage(page);
8-
await playwrightDev.goto();
7+
const indexPage = new IndexPage(page);
8+
await indexPage.goto();
99

1010
// Expect a title "to contain" a substring.
1111
await expect(page).toHaveTitle(/Jersey's Login Page/);

tests/pages/indexPage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ export class IndexPage {
2525
}
2626

2727
async fillPassword() {
28+
// TODO: use the secret service.
2829
await this.password.fill('Dev0psd@y2024');
2930
await expect(this.password).toBeVisible();
3031
}
3132

32-
3333
async loginAction() {
3434
await this.fillEmail();
3535
await this.fillPassword();

tests/pages/registerPage.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { expect, type Locator, type Page } from '@playwright/test';
2+
3+
export class RegisterPage {
4+
readonly page: Page;
5+
readonly name: Locator;
6+
readonly email: Locator;
7+
readonly password: Locator;
8+
readonly registerButton: Locator;
9+
readonly loginLink: Locator;
10+
11+
constructor(page: Page) {
12+
this.page = page;
13+
this.name = page.locator('div[data-testid="name"] input');
14+
this.email = page.locator('div[data-testid="email"] input');
15+
this.password = page.locator('div[data-testid="password"] input');
16+
this.registerButton = page.locator('a[data-testid="registerButton"]');
17+
this.loginLink = page.locator('a[data-testid="loginLink"]');
18+
}
19+
20+
async goto() {
21+
await this.page.goto('/react-playwright-demo/register');
22+
}
23+
24+
async fillName() {
25+
await this.name.fill('JerseyAwesome');
26+
await expect(this.name).toBeVisible();
27+
}
28+
29+
async fillEmail() {
30+
await this.email.fill('[email protected]');
31+
await expect(this.email).toBeVisible();
32+
}
33+
34+
async fillPassword() {
35+
// TODO: use the secret service.
36+
await this.password.fill('Dev0psd@y2024');
37+
await expect(this.password).toBeVisible();
38+
}
39+
40+
async registerAction() {
41+
await this.fillName();
42+
await this.fillEmail();
43+
await this.fillPassword();
44+
await this.registerButton.click();
45+
}
46+
}

tests/registerPage.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { test, expect } from '@playwright/test';
2+
import { RegisterPage } from './pages/registerPage';
3+
import { WelcomePage } from './pages/welcomePage';
4+
5+
test.describe('Register Page', () => {
6+
test('do register', async ({ page }) => {
7+
const registerPage = new RegisterPage(page);
8+
await registerPage.goto();
9+
await registerPage.registerAction();
10+
11+
const welcomePage = new WelcomePage(page);
12+
const welcomeMsgText = await welcomePage.getWelcomeMsgText();
13+
14+
// Expect a welcome message LoginSuccess
15+
await expect(welcomeMsgText).toContain('LoginSuccess');
16+
});
17+
});

0 commit comments

Comments
 (0)