Skip to content

Commit

Permalink
1. added methods in webHelper.ts
Browse files Browse the repository at this point in the history
2. added helper method in apiHelper.ts
3. added serial test in example
4. added parallel test in example
  • Loading branch information
abhaybharti committed Jan 29, 2024
1 parent 06b443b commit 23035e8
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 12 deletions.
44 changes: 32 additions & 12 deletions src/helper/api/apiHelper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { request, expect, APIResponse } from "@playwright/test";
import exp from "constants";
import { StringLiteral } from "typescript";
export class ApiHelper {
private apiContext: any;
constructor(apiContext: any) {
Expand All @@ -9,39 +10,50 @@ export class ApiHelper {
async hitApiEndPoint(
operationType: string,
endPoint: string,
payload: object
payload: object,
statusCode: number
) {
switch (operationType.toLowerCase()) {
case "get":
await this.invokeGetApi(endPoint);
await this.invokeGetApi(endPoint, statusCode);
break;
case "post":
await this.invokePostApi(endPoint, payload);
await this.invokePostApi(endPoint, payload, statusCode);
break;
case "delete":
await this.invokeDeleteApi();
await this.invokeDeleteApi(endPoint, statusCode);
break;
case "put":
await this.invokePutApi(endPoint, payload);
await this.invokePutApi(endPoint, payload, statusCode);
break;

default:
break;
}
}

async invokeGetApi(endPoint: string) {
async invokeGetApi(endPoint: string, statusCode: number = 200) {
let response;
try {
console.log(`endPoint: , ${endPoint} `);
response = await this.apiContext.get(endPoint);
expect(response.status()).toBe(200);
expect(response.status()).toBe(statusCode);
return await response.json();
} catch (error) {
return error;
}
}
async invokeDeleteApi(endPoint: string, statusCode: number = 200) {
let response;
try {
console.log(`endPoint: , ${endPoint} `);
response = await this.apiContext.delete(endPoint);
expect(response.status()).toBe(statusCode);
return await response.json();
} catch (error) {
return error;
}
}
async invokeDeleteApi() {}

/**
* The function `invokePostApi` is an asynchronous function that sends a POST request to an API
Expand All @@ -54,7 +66,11 @@ export class ApiHelper {
* @returns the response data as a JSON object if the response status is 200. If there is an error, it
* will return the error object.
*/
async invokePostApi(endPoint: string, payload: object) {
async invokePostApi(
endPoint: string,
payload: object,
statusCode: number = 200
) {
let response;
try {
console.log(`endPoint: , ${endPoint} payload :${payload} `);
Expand All @@ -64,13 +80,17 @@ export class ApiHelper {
"Content-Type": "application/json",
},
});
expect(response.status()).toBe(200);
expect(response.status()).toBe(statusCode);
return await response.json();
} catch (error) {
return error;
}
}
async invokePutApi(endPoint: string, payload: object) {
async invokePutApi(
endPoint: string,
payload: object,
statusCode: number = 200
) {
let response;
try {
console.log(`endPoint: , ${endPoint} payload :${payload} `);
Expand All @@ -80,7 +100,7 @@ export class ApiHelper {
"Content-Type": "application/json",
},
});
expect(response.status()).toBe(200);
expect(response.status()).toBe(statusCode);
return await response.json();
} catch (error) {
return error;
Expand Down
26 changes: 26 additions & 0 deletions src/helper/web/webHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,30 @@ export class WebHelper {
async verifyNestedFrame(): Promise<void> {
//TBD
}

async assertPageURL(url: string): Promise<void> {
console.log("Assertion for Page URL");
await expect(this.webPage).toHaveURL(url);
}

async assertPageTitle(title: string): Promise<void> {
console.log("Assertion for Page Title");
await expect(this.webPage).toHaveTitle(title);
}
async openNewTab(url: string): Promise<Page> {
const pageOne = await this.browserContext.newPage();
await pageOne.goto(url);
return pageOne;
}
async takeScreenshot(imageName: string = `screenshot.png`): Promise<void> {
await this.webPage.screenshot({ path: `${imageName}`, fullPage: true });
}

async takeScreenshotOfElement(
locator: string,
imageName: string = `screenshot.png`
): Promise<void> {
const el = await this.webPage.locator(locator);
await el.screenshot({ path: `${imageName}` });
}
}
7 changes: 7 additions & 0 deletions src/tests/web/example/runTestParallel.spect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import test from "@playwright/test";

test.describe.parallel("Run All Tests in Parallel", async () => {
test("TestOne", async ({ page }) => {});

test("TestTwo", async ({ page }) => {});
});
9 changes: 9 additions & 0 deletions src/tests/web/example/runTestSerial.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import test from "@playwright/test";

test.describe.serial("Run all test in serial", async () => {
test("TestOne", async ({ page }) => {});

test("TestTwo", async ({ page }) => {});

test("TestThree", async ({ page }) => {});
});

0 comments on commit 23035e8

Please sign in to comment.