From f169ad94b4006aebea68f4dfa3acce2dd6df351a Mon Sep 17 00:00:00 2001 From: Abhay Bharti Date: Mon, 12 Feb 2024 20:42:03 +0530 Subject: [PATCH] api example test updated --- playwright.config.ts | 3 +++ src/helper/api/apiHelper.ts | 41 +++++++++++++++++++++---------- src/tests/api/example/api.spec.js | 8 ------ src/tests/api/example/api.spec.ts | 34 +++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 21 deletions(-) delete mode 100644 src/tests/api/example/api.spec.js create mode 100644 src/tests/api/example/api.spec.ts diff --git a/playwright.config.ts b/playwright.config.ts index 08df9f5..6cacafa 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -85,6 +85,9 @@ export default defineConfig({ video: "on", headless: false, viewport: { width: 1920, height: 1080 }, + // baseURL:'https://restful-booker.herokuapp.com', + ignoreHTTPSErrors: true, + acceptDownloads: true, }, }, // { diff --git a/src/helper/api/apiHelper.ts b/src/helper/api/apiHelper.ts index 98ba3da..f6def75 100644 --- a/src/helper/api/apiHelper.ts +++ b/src/helper/api/apiHelper.ts @@ -1,8 +1,9 @@ import { request, expect, APIResponse } from "@playwright/test"; +import { log } from "console"; import exp from "constants"; import { StringLiteral } from "typescript"; -const BASE_URL = "https://jsonplaceholder.typicode.com"; +const BASE_URL = "https://restful-booker.herokuapp.com"; export class ApiHelper { private apiContext: any; @@ -13,12 +14,13 @@ export class ApiHelper { * credentials, request headers, and other configuration settings. */ constructor(apiContext: any) { - this.apiContext = apiContext.newContext({ - extraHTTPHeaders: { - Authorization: `Bearer 12345`, - "Content-Type": `application/json`, - }, - }); + this.apiContext = apiContext; + // this.apiContext = apiContext({ + // extraHTTPHeaders: { + // Authorization: `Bearer 12345`, + // "Content-Type": `application/json`, + // }, + // }); } /** @@ -56,7 +58,6 @@ export class ApiHelper { case "put": await this.invokePutApi(endPoint, payload, statusCode); break; - default: break; } @@ -67,9 +68,13 @@ export class ApiHelper { try { console.log(`Making GET request to endPoint: ${BASE_URL}${endPoint}`); response = await this.apiContext.get(`${BASE_URL}${endPoint}`); - expect(response.status()).toBe(statusCode); + expect( + response.status(), + `API : ${BASE_URL}${endPoint} , Expected status : ${statusCode}, Actual status : ${response.status()}` + ).toBe(statusCode); return await response.json(); } catch (error) { + console.log(error); return error; } } @@ -80,7 +85,10 @@ export class ApiHelper { `Making DELETE request to endPoint: ${BASE_URL}${endPoint}` ); response = await this.apiContext.delete(`${BASE_URL}${endPoint}`); - expect(response.status()).toBe(statusCode); + expect( + response.status(), + `API : ${BASE_URL}${endPoint} , Expected status : ${statusCode}, Actual status : ${response.status()}` + ).toBe(statusCode); return await response.json(); } catch (error) { return error; @@ -105,13 +113,17 @@ export class ApiHelper { ) { let response; try { + let tempPayload = JSON.stringify(payload); console.log( - `Making POST request to endPoint: ${BASE_URL}${endPoint} payload :${payload} ` + `Making POST request to endPoint: ${BASE_URL}${endPoint} payload :${tempPayload} ` ); response = await this.apiContext.post(`${BASE_URL}${endPoint}`, { data: payload, }); - expect(response.status()).toBe(statusCode); + expect( + response.status(), + `API : ${BASE_URL}${endPoint} , Expected status : ${statusCode}, Actual status : ${response.status()}` + ).toBe(statusCode); return await response.json(); } catch (error) { return error; @@ -130,7 +142,10 @@ export class ApiHelper { response = await this.apiContext.put(`${BASE_URL}${endPoint}`, { data: payload, }); - expect(response.status()).toBe(statusCode); + expect( + response.status(), + `API : ${BASE_URL}${endPoint} , Expected status : ${statusCode}, Actual status : ${response.status()}` + ).toBe(statusCode); return await response.json(); } catch (error) { return error; diff --git a/src/tests/api/example/api.spec.js b/src/tests/api/example/api.spec.js deleted file mode 100644 index 8c34612..0000000 --- a/src/tests/api/example/api.spec.js +++ /dev/null @@ -1,8 +0,0 @@ -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("url"); -}); diff --git a/src/tests/api/example/api.spec.ts b/src/tests/api/example/api.spec.ts new file mode 100644 index 0000000..1bcb9f3 --- /dev/null +++ b/src/tests/api/example/api.spec.ts @@ -0,0 +1,34 @@ +import { expect, test } from "@playwright/test"; +import { ApiHelper } from "../../../helper/api/apiHelper"; + +let token: string; +let bookingId: string; + +test.beforeAll(async ({ request }) => { + //1. Hit /Auth Api and provide username/password as body + //2. fetch token value from JSON response + //3. save in token variable + const apiHelper = await new ApiHelper(request); + const responseMsg = await apiHelper.invokePostApi("/auth", { + username: "admin", + password: "password123", + }); + + expect(responseMsg.token); + + token = responseMsg.token; + console.log(token); +}); + +test("Get List of booking and verify response", async ({ request }) => { + /* Test Flow + 1. Hit API endpoint + 2. Verify API status code + 3. Verify JSON Schema + */ + const apiHelper = await new ApiHelper(request); // + const responseMsg = await apiHelper.invokeGetApi("/booking"); + console.log(responseMsg); +}); + +//API used for writing test code - https://restful-booker.herokuapp.com/apidoc/index.html