1+ import { test , expect , request } from "@playwright/test" ;
2+ import * as fs from "fs" ;
3+ import * as path from "path" ;
4+
5+ const backendURL : string = "https://petstore.swagger.io/v2/pet" ;
6+
7+ test . describe ( "Testing uploading image for pet and pets creation/updating" , ( ) => {
8+ test ( "Upload an image via API" , async ( ) => {
9+ const apiRequestContext = await request . newContext ( ) ;
10+ const petId = 1 ;
11+ const additionalMetadata = "Sample metadata" ;
12+ const filePath = path . resolve ( __dirname , "../tests/image.jpeg" ) ;
13+
14+ const response = await apiRequestContext . post (
15+ `${ backendURL } /${ petId } /uploadImage` ,
16+ {
17+ multipart : {
18+ additionalMetadata : additionalMetadata ,
19+ file : {
20+ name : "image.jpg" ,
21+ mimeType : "image/jpeg" ,
22+ buffer : fs . readFileSync ( filePath ) ,
23+ } ,
24+ } ,
25+ }
26+ ) ;
27+
28+ expect ( response . status ( ) ) . toBe ( 200 ) ;
29+
30+ const responseBody = await response . json ( ) ;
31+ console . log ( responseBody ) ;
32+ expect ( responseBody ) . toHaveProperty ( "code" ) ;
33+ expect ( responseBody ) . toHaveProperty ( "type" ) ;
34+ expect ( responseBody ) . toHaveProperty ( "message" ) ;
35+ } ) ;
36+
37+ test ( "Add a new pet to the store" , async ( ) => {
38+ const context = await request . newContext ( ) ;
39+ const response = await context . post ( `${ backendURL } ` , {
40+ data : {
41+ id : 2 ,
42+ category : {
43+ id : 0 ,
44+ name : "string" ,
45+ } ,
46+ name : "doggie" ,
47+ photoUrls : [ "string" ] ,
48+ tags : [
49+ {
50+ id : 0 ,
51+ name : "string" ,
52+ } ,
53+ ] ,
54+ status : "available" ,
55+ } ,
56+ } ) ;
57+ expect ( response . status ( ) ) . toBe ( 200 ) ;
58+ const responseBody = await response . json ( ) ;
59+ console . log ( responseBody ) ;
60+ } ) ;
61+
62+ test ( "Update an existing pet" , async ( ) => {
63+ const context = await request . newContext ( ) ;
64+ const response = await context . post ( `${ backendURL } ` , {
65+ data : {
66+ id : 2 ,
67+ category : {
68+ id : 0 ,
69+ name : "string" ,
70+ } ,
71+ name : "kitty" ,
72+ photoUrls : [ "string" ] ,
73+ tags : [
74+ {
75+ id : 0 ,
76+ name : "string" ,
77+ } ,
78+ ] ,
79+ status : "available" ,
80+ } ,
81+ } ) ;
82+ expect ( response . status ( ) ) . toBe ( 200 ) ;
83+ const responseBody = await response . json ( ) ;
84+ console . log ( responseBody ) ;
85+ } ) ;
86+ } ) ;
87+
88+ test . describe ( "Find pet by status: available, pending, sold" , ( ) => {
89+ test ( 'Should return pets with status "pending"' , async ( { request } ) => {
90+ const status = "pending" ;
91+ const response = await request . get ( `${ backendURL } /findByStatus` , {
92+ params : { status } ,
93+ } ) ;
94+
95+ expect ( response . status ( ) ) . toBe ( 200 ) ;
96+
97+ const responseBody = await response . json ( ) ;
98+ expect ( Array . isArray ( responseBody ) ) . toBeTruthy ( ) ;
99+ responseBody . forEach ( ( pet ) => {
100+ expect ( pet . status ) . toBe ( "pending" ) ;
101+ } ) ;
102+ } ) ;
103+ } ) ;
104+
105+ test ( 'Should return pets with status "available"' , async ( { request } ) => {
106+ const status = "available" ;
107+ const response = await request . get ( `${ backendURL } /findByStatus` , {
108+ params : { status } ,
109+ } ) ;
110+
111+ expect ( response . status ( ) ) . toBe ( 200 ) ;
112+
113+ const responseBody = await response . json ( ) ;
114+ expect ( Array . isArray ( responseBody ) ) . toBeTruthy ( ) ;
115+ responseBody . forEach ( ( pet ) => {
116+ expect ( pet . status ) . toBe ( "available" ) ;
117+ } ) ;
118+ } ) ;
119+
120+ test ( 'Should return pets with status "sold"' , async ( { request } ) => {
121+ const status = "sold" ;
122+ const response = await request . get ( `${ backendURL } /findByStatus` , {
123+ params : { status } ,
124+ } ) ;
125+
126+ expect ( response . status ( ) ) . toBe ( 200 ) ;
127+ const responseBody = await response . json ( ) ;
128+ expect ( Array . isArray ( responseBody ) ) . toBeTruthy ( ) ;
129+ responseBody . forEach ( ( pet ) => {
130+ expect ( pet . status ) . toBe ( "sold" ) ;
131+ } ) ;
132+ } ) ;
133+
134+ test . describe ( "Find pet by ID, update pet in the store" , async ( ) => {
135+ test ( "Find pet" , async ( ) => {
136+ const petId = 4 ;
137+ const context = await request . newContext ( ) ;
138+ const response = await context . get ( `${ backendURL } /${ petId } ` ) ;
139+ expect ( response . status ( ) ) . toBe ( 200 ) ;
140+
141+ const responseBody = await response . json ( ) ;
142+ console . log ( responseBody ) ;
143+ } ) ;
144+ } ) ;
145+
146+ test ( "Update pet information" , async ( ) => {
147+ const petId = 2 ;
148+ const updatedName = "Updated Pet Name" ;
149+ const updatedStatus = "sold" ;
150+ const context = await request . newContext ( ) ;
151+ const response = await context . post ( `${ backendURL } /${ petId } ` , {
152+ form : {
153+ name : updatedName ,
154+ status : updatedStatus ,
155+ } ,
156+ } ) ;
157+ expect ( response . status ( ) ) . toBe ( 200 ) ;
158+
159+ const responseBody = await response . json ( ) ;
160+ console . log ( responseBody ) ;
161+ expect ( responseBody . message ) . toBe ( petId . toString ( ) ) ;
162+ } ) ;
163+
164+ test . describe ( "Delete pet by ID" , async ( ) => {
165+ const petId = 6 ;
166+ test . beforeAll ( async ( ) => {
167+ const context = await request . newContext ( ) ;
168+ const response = await context . post ( `${ backendURL } ` , {
169+ data : {
170+ id : petId ,
171+ category : {
172+ id : 0 ,
173+ name : "string" ,
174+ } ,
175+ name : "doggie" ,
176+ photoUrls : [ "string" ] ,
177+ tags : [
178+ {
179+ id : 0 ,
180+ name : "string" ,
181+ } ,
182+ ] ,
183+ status : "available" ,
184+ } ,
185+ } ) ;
186+ expect ( response . status ( ) ) . toBe ( 200 ) ;
187+ const responseBody = await response . json ( ) ;
188+ console . log ( responseBody ) ;
189+ } ) ;
190+
191+ test ( "Delete pet" , async ( ) => {
192+ const context = await request . newContext ( ) ;
193+ const apiKey = "your_actual_api_key" ;
194+ const response = await context . delete ( `${ backendURL } /${ petId } ` , {
195+ headers : {
196+ api_key : apiKey ,
197+ } ,
198+ } ) ;
199+
200+ expect ( response . status ( ) ) . toBe ( 200 ) ;
201+ const responseBody = await response . json ( ) ;
202+ console . log ( responseBody ) ;
203+ } ) ;
204+
205+ test . afterAll ( async ( ) => {
206+ const petId = 6 ;
207+ const context = await request . newContext ( ) ;
208+ const response = await context . delete ( `${ backendURL } /${ petId } ` ) ;
209+
210+ expect ( response . status ( ) ) . toBe ( 404 ) ;
211+ } ) ;
212+ } ) ;
0 commit comments