Skip to content

Commit

Permalink
added new example tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abhaybharti committed Jan 30, 2024
1 parent f81d29c commit e8bbed0
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 27 deletions.
24 changes: 24 additions & 0 deletions src/helper/api/apiHelper.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
8 changes: 8 additions & 0 deletions src/tests/api/example/api.spec.js
Original file line number Diff line number Diff line change
@@ -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();
});
27 changes: 0 additions & 27 deletions src/tests/web/example/example.spec.js

This file was deleted.

31 changes: 31 additions & 0 deletions src/tests/web/example/globalLogin.spec.ts
Original file line number Diff line number Diff line change
@@ -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
});
4 changes: 4 additions & 0 deletions src/tests/web/example/runTestParallel.spect.ts
Original file line number Diff line number Diff line change
@@ -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 }) => {});

Expand Down
4 changes: 4 additions & 0 deletions src/tests/web/example/runTestSerial.spec.ts
Original file line number Diff line number Diff line change
@@ -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 }) => {});

Expand Down
13 changes: 13 additions & 0 deletions src/tests/web/example/saveHarFile.spec.ts
Original file line number Diff line number Diff line change
@@ -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` });
});

0 comments on commit e8bbed0

Please sign in to comment.