Skip to content

Commit 296f035

Browse files
committed
Playwright TypeScript Framework
1 parent a7c166a commit 296f035

File tree

10 files changed

+71
-65
lines changed

10 files changed

+71
-65
lines changed

Playwright-TypeScript/.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
ENV=qa
2-
URL=https://parabank.parasoft.com/parabank/index.htm
2+
STAGING_URL=https://www.google.com
3+
QA_URL=https://www.google.com
34
USER_NAME=1234DF
45
PASSWORD=DFGHJK67
56

Playwright-TypeScript/package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Playwright-TypeScript/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"@eslint/js": "^9.10.0",
1616
"@playwright/test": "^1.47.1",
1717
"@types/debug": "^4.1.12",
18+
"@types/dotenv": "^8.2.0",
1819
"@types/node": "^22.5.5",
1920
"@types/winston": "^2.4.4",
2021
"@types/xlsx": "^0.0.36",

Playwright-TypeScript/src/fixtures/testfixture.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ import { writeTestStatusToExcelFile } from '../utils/excelhandler';
55
import { HomePage } from '../pages/homepage';
66
import { PlaylistPage } from '../pages/playlistpage';
77
import { ResultPage } from '../pages/resultpage';
8-
import BaseTest from '../utils/basetest';
98

9+
import { loadTestData } from '../utils/jsonhandler';
10+
11+
import { TestData } from '../interfaces/testdata.interface';
1012

1113
export const test = base.extend<{
1214
saveLogs: void;
1315
homePage: HomePage;
1416
playlistPage: PlaylistPage;
1517
resultPage: ResultPage;
16-
baseTest: BaseTest;
18+
testData: TestData;
1719
}>({
1820
saveLogs: [async ({ }, use, testInfo) => {
1921
// Collecting logs during the test.
@@ -42,15 +44,14 @@ export const test = base.extend<{
4244
const playlistPage = new PlaylistPage(page);
4345
await use(playlistPage);
4446
},
45-
resultPage: async ({ page }, use) => {
46-
const resultPage = new ResultPage(page);
47+
resultPage: async ({ page, testData }, use) => {
48+
const resultPage = new ResultPage(page, testData);
4749
await use(resultPage);
4850
},
49-
baseTest: async ({ page }, use) => {
50-
const baseTest = new BaseTest(page);
51-
await use(baseTest);
51+
testData: async ({ }, use) => {
52+
const data = await loadTestData();
53+
await use(data);
5254
}
53-
5455
});
5556

5657
export { expect } from '@playwright/test';
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export interface Module1Data {
2+
"skill1": string;
3+
"skill2": string;
4+
"skill3": string;
5+
"skill4": string;
6+
"skill5": string;
7+
"skill6": string;
8+
"skill7": string;
9+
"skill8": string;
10+
}
11+
12+
export interface TestData {
13+
module1?: Module1Data;
14+
}

Playwright-TypeScript/src/pages/homepage.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Inlcude playwright module
22
import { expect, type Locator, type Page } from '@playwright/test';
3+
import dotenv from 'dotenv';
4+
dotenv.config();
35

46
// create class
57
export class HomePage {
@@ -15,9 +17,13 @@ export class HomePage {
1517
this.searchTextbox = page.locator('#APjFqb');
1618
}
1719

18-
async goto(){
19-
await this.page.setViewportSize({width:1366, height:728})
20-
await this.page.goto('https://www.google.com');
20+
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));
26+
}
2127
}
2228

2329
async searchKeywords(param1:string){
Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
11
// Inlcude playwright module
22
import { expect, type Locator, type Page } from '@playwright/test';
33

4-
import BaseTest from '../utils/basetest';
5-
64
// create class
75
export class ResultPage {
86

97
readonly page: Page;
108
readonly playlistlink: Locator;
119

12-
constructor(page: Page) {
13-
const baseTest = new BaseTest(page);
14-
10+
constructor(page: Page, testData: any) {
1511
// Init page object
1612
this.page = page;
17-
18-
console.log(String(baseTest.testData.module1.skill1));
1913
// Elements
20-
this.playlistlink = page.getByRole('link',{name: String(baseTest.testData.module1.skill1) });
14+
this.playlistlink = page.getByRole('link',{name: String(testData.module1.skill1) });
2115
}
2216

2317
async clickOnPlaylist(){
2418
await expect(this.playlistlink.first()).toBeEnabled();
2519
await this.playlistlink.first().click();
2620
}
27-
2821
}

Playwright-TypeScript/src/utils/basetest.ts

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
import { TestData } from '../interfaces/testdata.interface';
4+
5+
export async function loadTestData() {
6+
const environment = process.env.ENV || 'qa';
7+
const directoryPath = path.join(__dirname.replace('utils',''), `/test-data`, environment);
8+
const jsonData: TestData = {};
9+
10+
fs.readdirSync(directoryPath).forEach(file => {
11+
if (path.extname(file) === '.json') {
12+
const filePath = path.join(directoryPath, file);
13+
const fileContent: TestData = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
14+
Object.assign(jsonData, fileContent); // Merge the content into a single object
15+
}
16+
});
17+
18+
return jsonData;
19+
}

Playwright-TypeScript/tests/01_ui_test.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ dotenv.config();
1010
/**
1111
* Bakkappa N
1212
*/
13-
test('[2] UI automation test using playwright', { tag: '@UITest' }, async ({ page, baseTest, homePage, resultPage, playlistPage }) => {
13+
test('[2] UI automation test using playwright', { tag: '@UITest' }, async ({ page, testData, homePage, resultPage, playlistPage }) => {
1414

1515
await test.step('Go to URL', async () => {
16-
await baseTest.goto();
16+
await homePage.goto();
1717
});
1818

1919
await test.step('Search with keywords', async () => {
20-
await homePage.searchKeywords(String(baseTest.testData.module1.skill1));
20+
await homePage.searchKeywords(String(testData.module1?.skill1));
2121
});
2222

2323
await test.step('Click on playlist', async () => {
@@ -34,7 +34,7 @@ test('[2] UI automation test using playwright', { tag: '@UITest' }, async ({ pag
3434
/**
3535
* Bakkappa N
3636
*/
37-
test('[9, 12, 14] Verify excel data using playwright', { tag: '@ValidateExcel' }, async ({ page }) => {
37+
test('[9, 12, 14] Verify excel data using playwright', { tag: '@ValidateExcel' }, async ({ page, testData }) => {
3838
const filePath = path.resolve(__dirname, String(process.env.DOWNLOAD_PATH)).toString();
3939
const workbook = xlsx.readFile(filePath);
4040
const worksheet = workbook.Sheets[process.env.Sheet1 as string];

0 commit comments

Comments
 (0)