Skip to content

Commit 3f7f73b

Browse files
committed
implement page fixtures
1 parent 2ad0fac commit 3f7f73b

File tree

6 files changed

+111
-2
lines changed

6 files changed

+111
-2
lines changed

fixtures/pageFixtures.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { test as base, Page } from "@playwright/test";
2+
import { LoginPage } from "../pages/LoginPage";
3+
import { RegistrationPage } from "../pages/RegistrationPage";
4+
5+
type MyFixtures = {
6+
loginPage: LoginPage;
7+
registrationPage: RegistrationPage;
8+
//add more types here
9+
};
10+
11+
export const test = base.extend<MyFixtures>({
12+
loginPage: async ({ page }, use) => {
13+
await use(new LoginPage(page));
14+
},
15+
registrationPage: async ({ page }, use) => {
16+
await use(new RegistrationPage(page));
17+
},
18+
//define others similarly
19+
});

pages/BasePage.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//TODO

pages/LoginPage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Page, Locator, expect} from "@playwright/test";
1+
import { Page, Locator, expect } from "@playwright/test";
22
import settings from "../tests/config/settings.json";
33

44
export class LoginPage {
@@ -31,7 +31,7 @@ export class LoginPage {
3131
this.loginButton.waitFor({ state: "visible" });
3232
await this.loginButton.click();
3333
await this.page.waitForTimeout(3000); // wait for login to process
34-
console.log("Current URL after login:", this.page.url());
34+
console.log("Current URL after login:", this.page.url());
3535
}
3636

3737
async verifyDashboard() {

pages/RegistrationPage.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { Page, Locator, expect } from "@playwright/test";
2+
import settings from "../tests/config/settings.json";
3+
import { faker } from "@faker-js/faker/locale/en";
4+
5+
export class RegistrationPage {
6+
readonly page: Page;
7+
readonly firstNameInput: Locator;
8+
readonly lastNameInput: Locator;
9+
readonly dobInput: Locator;
10+
readonly streetInput: Locator;
11+
readonly postalCodeInput: Locator;
12+
readonly cityInput: Locator;
13+
readonly stateInput: Locator;
14+
readonly phoneInput: Locator;
15+
readonly emailInput: Locator;
16+
readonly passwordInput: Locator;
17+
readonly countryDropdown: Locator;
18+
readonly registerButton: Locator;
19+
readonly loginHeader: Locator;
20+
21+
constructor(page: Page) {
22+
this.page = page;
23+
this.firstNameInput = page.locator("#first_name");
24+
this.lastNameInput = page.locator("#last_name");
25+
this.dobInput = page.locator("#dob");
26+
this.streetInput = page.locator("#street");
27+
this.postalCodeInput = page.locator("#postal_code");
28+
this.cityInput = page.locator("#city");
29+
this.stateInput = page.locator("#state");
30+
this.phoneInput = page.locator("#phone");
31+
this.emailInput = page.locator("#email");
32+
this.passwordInput = page.locator("#password");
33+
this.countryDropdown = page.locator("select#country");
34+
this.registerButton = page.locator("//button[@type='submit']");
35+
this.loginHeader = page.locator("//h3[text()='Login']");
36+
}
37+
38+
async navigateToRegistration() {
39+
await this.page.goto(settings.REGISTRATION_URL);
40+
}
41+
42+
async fillRegistrationForm() {
43+
await this.firstNameInput.fill(faker.person.firstName());
44+
await this.lastNameInput.fill(faker.person.lastName());
45+
await this.dobInput.fill("1990-01-01");
46+
await this.streetInput.fill(faker.location.streetAddress());
47+
await this.postalCodeInput.fill(faker.location.zipCode());
48+
await this.cityInput.fill(faker.location.city());
49+
await this.stateInput.fill(faker.location.state());
50+
await this.countryDropdown.selectOption({ label: "Canada" });
51+
const phoneNumber = faker.string.numeric(10);
52+
await this.phoneInput.fill(phoneNumber);
53+
await this.emailInput.fill(faker.internet.email());
54+
await this.passwordInput.fill(settings.PASSWORD);
55+
}
56+
57+
async clickRegisterButton() {
58+
await this.registerButton.click();
59+
}
60+
61+
async verifyRedirectionToLogin() {
62+
await this.loginHeader.waitFor({ state: "visible", timeout: 2000 });
63+
expect(this.page.url()).toBe(settings.LOGIN_URL);
64+
expect(await this.loginHeader.textContent()).toBe("Login");
65+
}
66+
}

tests/hooks/hooks.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Before(async function (this: CustomWorld) {
1313
// Attach to World so step defs can access via this.browser / this.page
1414
this.browser = browser;
1515
this.page = page;
16+
await this.initPageObjects();
1617
});
1718

1819
After(async function (this: CustomWorld) {

tests/support/world.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
11
import { setWorldConstructor, World } from "@cucumber/cucumber";
22
import { Browser, Page } from "playwright";
3+
import { LoginPage } from "../../pages/LoginPage";
4+
import {RegistrationPage} from "../../pages/RegistrationPage";
35

46
export class CustomWorld extends World {
57
browser!: Browser;
68
page!: Page;
9+
loginPage!: LoginPage;
10+
registrationPage!:RegistrationPage;
11+
12+
async initPageObjects() {
13+
this.loginPage = new LoginPage(this.page);
14+
this.registrationPage = new RegistrationPage(this.page);
15+
// Initialize other page objects similarly
16+
}
717
}
18+
819
setWorldConstructor(CustomWorld);
20+
21+
//optional helper function to initialize page objects
22+
export async function initPageObjects(world: CustomWorld): Promise<void> {
23+
if (!world) throw new Error("initPageObjects: world instance is required");
24+
if (typeof world.initPageObjects !== "function") {
25+
throw new Error(
26+
"initPageObjects: provided world does not implement initPageObjects()"
27+
);
28+
}
29+
await world.initPageObjects();
30+
}

0 commit comments

Comments
 (0)