Skip to content

Commit cb82a53

Browse files
committed
Playwright TypeScript Framework
1 parent e52f86e commit cb82a53

File tree

4 files changed

+75
-32
lines changed

4 files changed

+75
-32
lines changed

Diff for: Playwright-TypeScript/src/pages/basepage.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Locator, Page } from 'playwright';
2+
import { expect } from '@playwright/test';
3+
4+
export class BasePage {
5+
protected page: Page;
6+
7+
constructor(page: Page) {
8+
this.page = page;
9+
}
10+
11+
async navigateTo(url: string): Promise<void> {
12+
await this.page.goto(url);
13+
}
14+
15+
async clickOnElement(locator: Locator): Promise<void> {
16+
await locator.waitFor({ state: 'visible' });
17+
await locator.click();
18+
}
19+
20+
async doubleClickOnElement(locator: Locator): Promise<void> {
21+
await locator.waitFor({ state: 'visible' });
22+
await locator.dblclick();
23+
}
24+
25+
async enterText(locator: Locator, value: string): Promise<void> {
26+
await locator.click();
27+
await locator.fill('');
28+
await locator.fill(value);
29+
}
30+
31+
async isElementVisible(locator: Locator): Promise<boolean> {
32+
return await locator.isVisible();
33+
}
34+
35+
async getText(locator: Locator): Promise<string | null> {
36+
return await locator.textContent();
37+
}
38+
39+
async pressKeyboardKey(locator: Locator, key: string): Promise<void> {
40+
await locator.press(key);
41+
}
42+
43+
async isElementEnabled(locator: Locator): Promise<void> {
44+
await expect(locator).toBeEnabled();
45+
}
46+
}

Diff for: Playwright-TypeScript/src/pages/homepage.ts

+14-16
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
11
// Inlcude playwright module
22
import { expect, type Locator, type Page } from '@playwright/test';
3+
import { BasePage } from './basepage';
4+
35
import dotenv from 'dotenv';
46
dotenv.config();
57

68
// create class
7-
export class HomePage {
9+
export class HomePage extends BasePage {
810

9-
readonly page: Page;
1011
readonly searchTextbox: Locator;
1112

1213
constructor(page: Page) {
13-
// Init page object
14-
this.page = page;
15-
14+
super(page);
15+
1616
// Elements
1717
this.searchTextbox = page.locator('#APjFqb');
1818
}
1919

2020
async goto() {
21-
await this.page.setViewportSize({ width: 1366, height: 728 })
22-
if (String(process.env.ENV).toUpperCase() == 'QA') {
23-
await this.page.goto(String(process.env.QA_URL));
24-
} else if (String(process.env.ENV).toUpperCase() == 'STAGE') {
25-
await this.page.goto(String(process.env.STAGING_URL));
21+
await this.page.setViewportSize({ width: 1366, height: 728 });
22+
if (String(process.env.ENV).toUpperCase() === 'QA') {
23+
await this.navigateTo(String(process.env.QA_URL));
24+
} else if (String(process.env.ENV).toUpperCase() === 'STAGE') {
25+
await this.navigateTo(String(process.env.STAGING_URL));
2626
}
2727
}
2828

29-
async searchKeywords(param1:string){
30-
await expect(this.searchTextbox).toBeEnabled();
31-
await this.searchTextbox.click();
32-
await this.searchTextbox.fill(param1);
33-
await this.searchTextbox.press('Enter');
29+
async searchKeywords(param1: string) {
30+
await this.clickOnElement(this.searchTextbox);
31+
await this.enterText(this.searchTextbox, param1);
32+
await this.pressKeyboardKey(this.searchTextbox, 'Enter');
3433
}
35-
3634
}

Diff for: Playwright-TypeScript/src/pages/playlistpage.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
// Inlcude playwright module
22
import { expect, type Locator, type Page } from '@playwright/test';
3-
import { setTimeout } from 'timers';
3+
import { BasePage } from './basepage';
44

55
// create class
6-
export class PlaylistPage {
7-
readonly page: Page;
6+
export class PlaylistPage extends BasePage {
7+
88
readonly videoLink: Locator;
99

1010
constructor(page: Page) {
11-
// Init page object
12-
this.page = page;
11+
super(page);
1312

1413
// Elements
1514
this.videoLink = page.locator('#container > #thumbnail');
1615
}
1716

1817
async clickOnVideo(){
1918
await this.page.waitForTimeout(3000);
20-
await expect(this.videoLink.first()).toBeEnabled(),{setTimeout:60000};
21-
await this.videoLink.first().click();
19+
await this.isElementEnabled(this.videoLink.first());
20+
await this.clickOnElement(this.videoLink.first());
2221
}
23-
2422
}

Diff for: Playwright-TypeScript/src/pages/resultpage.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
// Inlcude playwright module
22
import { expect, type Locator, type Page } from '@playwright/test';
3+
import { BasePage } from './basepage';
34

45
// create class
5-
export class ResultPage {
6+
export class ResultPage extends BasePage {
67

7-
readonly page: Page;
88
readonly playlistlink: Locator;
99

1010
constructor(page: Page, testData: any) {
11-
// Init page object
12-
this.page = page;
11+
super(page);
12+
1313
// Elements
1414
this.playlistlink = page.getByRole('link',{name: String(testData.module1.skill1) });
1515
}
1616

1717
async clickOnPlaylist(){
18-
await expect(this.playlistlink.first()).toBeEnabled();
19-
await this.playlistlink.first().click();
18+
await this.isElementEnabled(this.playlistlink.first());
19+
await this.clickOnElement( this.playlistlink.first());
2020
}
2121

2222
async clickOnPlaylistLink(playlist:string) {
23-
await expect(this.page.getByRole('link', { name: playlist }).first()).toBeEnabled();
24-
await this.page.getByRole('link', { name: playlist }).first().click();
23+
const link = this.page.getByRole('link', { name: playlist }).first();
24+
await expect(link).toBeEnabled();
25+
await this.clickOnElement(link);
2526
}
2627
}

0 commit comments

Comments
 (0)