From 87acd8a1c1d7c6b23a8961f3359f4f5f3a985604 Mon Sep 17 00:00:00 2001 From: Abhay Bharti Date: Wed, 14 Feb 2024 22:17:28 +0530 Subject: [PATCH] added generic helper --- .../generalPurpose.ts => helper/Helper.ts} | 2 +- src/helper/api/apiHelper.ts | 4 ++- src/helper/web/webHelper.ts | 6 ++-- src/tests/api/example/api.spec.ts | 9 +++--- src/utils/reader/jsonReader.ts | 31 +++++++++++++++++++ 5 files changed, 43 insertions(+), 9 deletions(-) rename src/{utils/generalPurpose.ts => helper/Helper.ts} (81%) create mode 100644 src/utils/reader/jsonReader.ts diff --git a/src/utils/generalPurpose.ts b/src/helper/Helper.ts similarity index 81% rename from src/utils/generalPurpose.ts rename to src/helper/Helper.ts index 903db38..906a5f4 100644 --- a/src/utils/generalPurpose.ts +++ b/src/helper/Helper.ts @@ -1,4 +1,4 @@ -export class GeneralPurpose { +export class Helper { async isValidDate(date: string) { if (Date.parse(date)) { return true; diff --git a/src/helper/api/apiHelper.ts b/src/helper/api/apiHelper.ts index 4eb7de5..24cb01a 100644 --- a/src/helper/api/apiHelper.ts +++ b/src/helper/api/apiHelper.ts @@ -1,10 +1,11 @@ import { request, expect, APIResponse } from "@playwright/test"; import { log } from "console"; import exp from "constants"; +import { Helper } from "helper/Helper"; import { StringLiteral } from "typescript"; const BASE_URL = "https://restful-booker.herokuapp.com"; -export class ApiHelper { +export class ApiHelper extends Helper { private apiContext: any; /** @@ -14,6 +15,7 @@ export class ApiHelper { * credentials, request headers, and other configuration settings. */ constructor(apiContext: any) { + super(); this.apiContext = apiContext; // this.apiContext = apiContext({ // extraHTTPHeaders: { diff --git a/src/helper/web/webHelper.ts b/src/helper/web/webHelper.ts index 1bec9e6..f98604f 100644 --- a/src/helper/web/webHelper.ts +++ b/src/helper/web/webHelper.ts @@ -1,11 +1,13 @@ import { BrowserContext, Page, expect } from "@playwright/test"; import fs from "fs"; +import { Helper } from "helper/Helper"; -export class WebHelper { +export class WebHelper extends Helper { readonly webPage: Page; readonly browserContext: BrowserContext; constructor(webPage: Page, browserContext: BrowserContext) { + super(); this.webPage = webPage; this.browserContext = browserContext; } @@ -406,6 +408,4 @@ export class WebHelper { }); }); } - - } diff --git a/src/tests/api/example/api.spec.ts b/src/tests/api/example/api.spec.ts index 1fc6bca..b4c8ffa 100644 --- a/src/tests/api/example/api.spec.ts +++ b/src/tests/api/example/api.spec.ts @@ -1,7 +1,6 @@ import { expect, test, APIRequestContext } from "@playwright/test"; import { ApiHelper } from "../../../helper/api/apiHelper"; -import { GeneralPurpose } from "../../../utils/generalPurpose"; let token: string; let bookingId: string; @@ -61,7 +60,7 @@ test("Get Booking Details using BookingID --> 1914", async ({ request }) => { description: "This will make GET call to https://restful-booker.herokuapp.com/booking/:id and verify keys/values in response", }); - const gs = await new GeneralPurpose(); + const apiHelper = await new ApiHelper(request); // const bookingDetails = await apiHelper.invokeGetApi("/booking/1914"); console.log(JSON.stringify(bookingDetails)); @@ -70,8 +69,10 @@ test("Get Booking Details using BookingID --> 1914", async ({ request }) => { expect(bookingDetails.lastname).toBe("Allen"); expect(bookingDetails.totalprice).toBe(111); expect(bookingDetails.depositpaid).toBeTruthy(); - expect(gs.isValidDate(bookingDetails.bookingdates.checkin)).toBe(true); - expect(gs.isValidDate(bookingDetails.bookingdates.checkout)).toBe(true); + expect(apiHelper.isValidDate(bookingDetails.bookingdates.checkin)).toBe(true); + expect(apiHelper.isValidDate(bookingDetails.bookingdates.checkout)).toBe( + true + ); expect(bookingDetails.additionalneeds).toBe("super bowls"); }); diff --git a/src/utils/reader/jsonReader.ts b/src/utils/reader/jsonReader.ts new file mode 100644 index 0000000..f178cf2 --- /dev/null +++ b/src/utils/reader/jsonReader.ts @@ -0,0 +1,31 @@ +import { promisify } from "util"; +import fs from "fs"; + +export class JsonReader { + private fileName: string; + private jsonData: any = ""; + private readFile: any; + + constructor(fileName: string) { + this.fileName = fileName; + this.readFile = promisify(fs.readFile); + this.jsonData = this.readJson(); + } + + async readJson() { + return await JSON.parse(this.readFile(this.fileName, "utf-8")); + } + + //function to read a given path and return the data + async getParamData(jsonPath: string) {} + + //function to return all element matching keyType & keyVal + async getTabName() {} + // + + async getParamLabels(jsonHierarchy: string) {} + + async getAllParamsInHierarchy(jsonHierarchy: string) {} + + async getEndPointNames() {} +}