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