From e8bbed0e151d0a653bba9a4bf170f335229634ca Mon Sep 17 00:00:00 2001 From: Abhay Bharti Date: Tue, 30 Jan 2024 17:57:28 +0530 Subject: [PATCH] added new example tests --- src/helper/api/apiHelper.ts | 24 ++++++++++++++ src/tests/api/example/api.spec.js | 8 +++++ src/tests/web/example/example.spec.js | 27 ---------------- src/tests/web/example/globalLogin.spec.ts | 31 +++++++++++++++++++ .../web/example/runTestParallel.spect.ts | 4 +++ src/tests/web/example/runTestSerial.spec.ts | 4 +++ src/tests/web/example/saveHarFile.spec.ts | 13 ++++++++ 7 files changed, 84 insertions(+), 27 deletions(-) create mode 100644 src/tests/api/example/api.spec.js delete mode 100644 src/tests/web/example/example.spec.js create mode 100644 src/tests/web/example/globalLogin.spec.ts create mode 100644 src/tests/web/example/saveHarFile.spec.ts diff --git a/src/helper/api/apiHelper.ts b/src/helper/api/apiHelper.ts index be54385..cd8022f 100644 --- a/src/helper/api/apiHelper.ts +++ b/src/helper/api/apiHelper.ts @@ -1,12 +1,36 @@ import { request, expect, APIResponse } from "@playwright/test"; import exp from "constants"; import { StringLiteral } from "typescript"; + export class ApiHelper { private apiContext: any; + +/** + * The constructor function initializes a new context for the API. + * @param {any} apiContext - The `apiContext` parameter is an object that represents the context of an + * API. It is used to store and manage information related to the API, such as authentication + * credentials, request headers, and other configuration settings. + */ constructor(apiContext: any) { this.apiContext = apiContext.newContext(); } +/** + * The function `hitApiEndPoint` is an asynchronous function that takes in an operation type, an + * endpoint, a payload, and a status code, and then invokes the corresponding API method based on the + * operation type. + * @param {string} operationType - The `operationType` parameter is a string that specifies the type of + * operation to be performed on the API endpoint. It can have one of the following values: "get", + * "post", "delete", or "put". + * @param {string} endPoint - The `endPoint` parameter is a string that represents the URL or endpoint + * of the API that you want to hit. It specifies the location where the API is hosted and the specific + * resource or action you want to perform. + * @param {object} payload - The `payload` parameter is an object that contains the data to be sent in + * the request body for POST and PUT operations. It can include any relevant information that needs to + * be sent to the API endpoint. + * @param {number} statusCode - The `statusCode` parameter is the expected HTTP status code that the + * API endpoint should return. + */ async hitApiEndPoint( operationType: string, endPoint: string, diff --git a/src/tests/api/example/api.spec.js b/src/tests/api/example/api.spec.js new file mode 100644 index 0000000..109ca77 --- /dev/null +++ b/src/tests/api/example/api.spec.js @@ -0,0 +1,8 @@ +import { test, request } from "@playwright/test"; +import { ApiHelper } from "../../../helper/api/apiHelper"; + +test("sample get requet", async () => { + const apiContext = await request.newContext(); + const apiHelper = new ApiHelper(apiContext); + apiHelper.invokeGetApi(); +}); diff --git a/src/tests/web/example/example.spec.js b/src/tests/web/example/example.spec.js deleted file mode 100644 index 6283d11..0000000 --- a/src/tests/web/example/example.spec.js +++ /dev/null @@ -1,27 +0,0 @@ -// @ts-check - -import { test, expect } from "@playwright/test"; - -test("has title", async ({ page }) => { - await page.goto("https://playwright.dev/"); - - // Expect a title "to contain" a substring. - await expect(page).toHaveTitle(/Playwright/); -}); - -test("get started link", async ({ page }) => { - await page.goto("https://playwright.dev/"); - - // Click the get started link. - await page.getByRole("link", { name: "Get started" }).click(); - - // Expects page to have a heading with the name of Installation. - await expect( - page.getByRole("heading", { name: "Installation" }) - ).toBeVisible(); -}); - -test(`Generate HAR file`, async ({ page: Page }) => { - // To record HAR file, use "update:true", below code will create a directory named har and store all the har related files in it - await Page.routeFromHAR("har/example.har", { update: true }); -}); diff --git a/src/tests/web/example/globalLogin.spec.ts b/src/tests/web/example/globalLogin.spec.ts new file mode 100644 index 0000000..b8f52db --- /dev/null +++ b/src/tests/web/example/globalLogin.spec.ts @@ -0,0 +1,31 @@ +import { test, BrowserContext } from "@playwright/test"; +import { WebHelper } from "../../../helper/web/webHelper.js"; + +let webContext: any; + +/* The `test.beforeAll()` function is a hook provided by the Playwright test framework. It is used to +run a setup function before all the tests in a test file. In this hook, we will login into application and save login details in state.json file*/ +test.beforeAll( + "Login into web app through browser and save login detail in JSON", + async ({ browser }) => { + const browserContext = await browser.newContext(); + const webPage = await browserContext.newPage(); + const webHelper = new WebHelper(webPage, browserContext); + webHelper.navigateToUrl("www.gmail.com"); + + //write code to login in gmail + + await browserContext.storageState({ path: "state.json" }); + webContext = await browserContext.newContext({ + storageState: "state.json", + }); + await webPage.close(); + } +); +/* The code you provided is a test case that logs into a web application using the saved login state. */ +test("Login into web app using saved login state", async () => { + const webPage = await webContext.newPage(); + + const webHelper = new WebHelper(webPage, webContext); + webHelper.navigateToUrl("www.gmail.com"); // Browser will open page using login details saved in test.beforeAll() step +}); diff --git a/src/tests/web/example/runTestParallel.spect.ts b/src/tests/web/example/runTestParallel.spect.ts index dd0b977..cde8987 100644 --- a/src/tests/web/example/runTestParallel.spect.ts +++ b/src/tests/web/example/runTestParallel.spect.ts @@ -1,5 +1,9 @@ import test from "@playwright/test"; +//1st way to set run tests in parallel +test.describe.configure({ mode: "parallel" }); + +//2nd way to set run tests in parallel test.describe.parallel("Run All Tests in Parallel", async () => { test("TestOne", async ({ page }) => {}); diff --git a/src/tests/web/example/runTestSerial.spec.ts b/src/tests/web/example/runTestSerial.spec.ts index 85710dd..cc09b1d 100644 --- a/src/tests/web/example/runTestSerial.spec.ts +++ b/src/tests/web/example/runTestSerial.spec.ts @@ -1,5 +1,9 @@ import test from "@playwright/test"; +//1st way to define test suite to run tests in sequence +test.describe.configure({ mode: "serial" }); + +//2nd way to define test suite to run tests in sequence test.describe.serial("Run all test in serial", async () => { test("TestOne", async ({ page }) => {}); diff --git a/src/tests/web/example/saveHarFile.spec.ts b/src/tests/web/example/saveHarFile.spec.ts new file mode 100644 index 0000000..4b14953 --- /dev/null +++ b/src/tests/web/example/saveHarFile.spec.ts @@ -0,0 +1,13 @@ +import test from "@playwright/test"; + +test(`Generate HAR file`, async ({ page: Page }, testInfo) => { + // To record HAR file, use "update:true", below code will create a directory named har and + //store all the har related files in it + await Page.routeFromHAR("har/example.har", { update: true }); + + /* The `await testInfo.attach(`HAR FILE`, { path: `../../../../har/example.har` });` line is + attaching the generated HAR file to the test report. It allows the HAR file to be easily + accessible and viewable alongside the test results. The `path` parameter specifies the location of + the HAR file. */ + await testInfo.attach(`HAR FILE`, { path: `../../../../har/example.har` }); +});