Skip to content

Commit

Permalink
api example test updated
Browse files Browse the repository at this point in the history
  • Loading branch information
abhaybharti committed Feb 12, 2024
1 parent 89fb051 commit f169ad9
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 21 deletions.
3 changes: 3 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
// {
Expand Down
41 changes: 28 additions & 13 deletions src/helper/api/apiHelper.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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`,
// },
// });
}

/**
Expand Down Expand Up @@ -56,7 +58,6 @@ export class ApiHelper {
case "put":
await this.invokePutApi(endPoint, payload, statusCode);
break;

default:
break;
}
Expand All @@ -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;
}
}
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
8 changes: 0 additions & 8 deletions src/tests/api/example/api.spec.js

This file was deleted.

34 changes: 34 additions & 0 deletions src/tests/api/example/api.spec.ts
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f169ad9

Please sign in to comment.