From 23035e8359d4362ae9a8281a604588f8a7ab6162 Mon Sep 17 00:00:00 2001 From: Abhay Bharti Date: Mon, 29 Jan 2024 21:24:58 +0530 Subject: [PATCH] 1. added methods in webHelper.ts 2. added helper method in apiHelper.ts 3. added serial test in example 4. added parallel test in example --- src/helper/api/apiHelper.ts | 44 ++++++++++++++----- src/helper/web/webHelper.ts | 26 +++++++++++ .../web/example/runTestParallel.spect.ts | 7 +++ src/tests/web/example/runTestSerial.spec.ts | 9 ++++ 4 files changed, 74 insertions(+), 12 deletions(-) create mode 100644 src/tests/web/example/runTestParallel.spect.ts create mode 100644 src/tests/web/example/runTestSerial.spec.ts diff --git a/src/helper/api/apiHelper.ts b/src/helper/api/apiHelper.ts index f4c8ce0..be54385 100644 --- a/src/helper/api/apiHelper.ts +++ b/src/helper/api/apiHelper.ts @@ -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) { @@ -9,20 +10,21 @@ 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: @@ -30,18 +32,28 @@ export class ApiHelper { } } - 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 @@ -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} `); @@ -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} `); @@ -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; diff --git a/src/helper/web/webHelper.ts b/src/helper/web/webHelper.ts index e40fcc7..527f446 100644 --- a/src/helper/web/webHelper.ts +++ b/src/helper/web/webHelper.ts @@ -83,4 +83,30 @@ export class WebHelper { async verifyNestedFrame(): Promise { //TBD } + + async assertPageURL(url: string): Promise { + console.log("Assertion for Page URL"); + await expect(this.webPage).toHaveURL(url); + } + + async assertPageTitle(title: string): Promise { + console.log("Assertion for Page Title"); + await expect(this.webPage).toHaveTitle(title); + } + async openNewTab(url: string): Promise { + const pageOne = await this.browserContext.newPage(); + await pageOne.goto(url); + return pageOne; + } + async takeScreenshot(imageName: string = `screenshot.png`): Promise { + await this.webPage.screenshot({ path: `${imageName}`, fullPage: true }); + } + + async takeScreenshotOfElement( + locator: string, + imageName: string = `screenshot.png` + ): Promise { + const el = await this.webPage.locator(locator); + await el.screenshot({ path: `${imageName}` }); + } } diff --git a/src/tests/web/example/runTestParallel.spect.ts b/src/tests/web/example/runTestParallel.spect.ts new file mode 100644 index 0000000..dd0b977 --- /dev/null +++ b/src/tests/web/example/runTestParallel.spect.ts @@ -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 }) => {}); +}); diff --git a/src/tests/web/example/runTestSerial.spec.ts b/src/tests/web/example/runTestSerial.spec.ts new file mode 100644 index 0000000..85710dd --- /dev/null +++ b/src/tests/web/example/runTestSerial.spec.ts @@ -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 }) => {}); +});