Skip to content

Commit bd2834b

Browse files
committed
Playwright with TypeScript Tutorial Full Course
1 parent 11805ea commit bd2834b

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

src/utils/APIHelper.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export async function formatAPIRequest(template: string, values: any[]): Promise<string> {
2+
return template.replace(/{(\d+)}/g, (match, p1) => {
3+
const index = parseInt(p1, 10);
4+
return index < values.length ? String(values[index]) : match;
5+
});
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"firstname": "{0}",
3+
"lastname": "{1}",
4+
"totalprice": "{2}",
5+
"depositpaid": true,
6+
"bookingdates": {
7+
"checkin": "2025-01-15",
8+
"checkout": "2025-01-17"
9+
},
10+
"additionalneeds": "breakfast"
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Import playwright module
2+
import { test, expect } from '@playwright/test';
3+
4+
import { formatAPIRequest } from '../../src/utils/APIHelper';
5+
6+
import path from 'path';
7+
import fs from 'fs';
8+
9+
test.use({
10+
baseURL: process.env.BASE_API_URL,
11+
})
12+
13+
/**
14+
* Author Testers Talk
15+
*/
16+
test('Create POST API Request using dynamic api request body in playwright & typescript', async ({ request }) => {
17+
18+
// Reading json file
19+
const filePath = path.join(__dirname, '../../test-data/api_requests/Dynamic_POST_API_Request.json');
20+
const jsonTemplate = fs.readFileSync(filePath, 'utf-8');
21+
22+
const values = ['cypress by testers talk', 'javascript by testers talk', 1000];
23+
24+
// Updating POST API request body
25+
const postAPIRequest = await formatAPIRequest(jsonTemplate, values);
26+
27+
// Create POST API Request
28+
const postAPIResponse = await request.post(`/booking`, { data: JSON.parse(postAPIRequest) });
29+
30+
// Print JSON API response
31+
const jsonPOSTAPIResponse = await postAPIResponse.json();
32+
console.log('POST API Response : ' + JSON.stringify(jsonPOSTAPIResponse, null, 2));
33+
34+
// Validating api response
35+
expect(postAPIResponse.status()).toBe(200);
36+
expect(postAPIResponse.statusText()).toBe('OK');
37+
expect(postAPIResponse.headers()['content-type']).toContain('application/json');
38+
39+
// Validate propert/key names
40+
expect(jsonPOSTAPIResponse.booking).toHaveProperty('firstname');
41+
expect(jsonPOSTAPIResponse.booking).toHaveProperty('lastname');
42+
43+
expect(jsonPOSTAPIResponse.booking.bookingdates).toHaveProperty('checkin');
44+
expect(jsonPOSTAPIResponse.booking.bookingdates).toHaveProperty('checkout');
45+
46+
// Validate API response body
47+
expect(jsonPOSTAPIResponse.bookingid).toBeGreaterThan(0);
48+
expect(jsonPOSTAPIResponse.booking.firstname).toBe('cypress by testers talk');
49+
expect(jsonPOSTAPIResponse.booking.lastname).toBe('javascript by testers talk');
50+
51+
expect(jsonPOSTAPIResponse.booking.bookingdates.checkin).toBe('2025-01-15');
52+
expect(jsonPOSTAPIResponse.booking.bookingdates.checkout).toBe('2025-01-17');
53+
});

0 commit comments

Comments
 (0)