WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 214d06e

Browse files
authored
Merge pull request #7 from auto-qa-hub/create-api-tests-for-pet
Create api tests for pet
2 parents cd489ca + 4fc9865 commit 214d06e

File tree

4 files changed

+227
-31
lines changed

4 files changed

+227
-31
lines changed

README.md

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -55,32 +55,16 @@ Generate and view test reports:
5555

5656
Playwright tests are located in the tests/ directory. Example structure:
5757

58-
/tests
59-
├── storeTests.spec.ts
60-
├── userTests.spec.ts
61-
├── petTests.spec.js
58+
**/tests**
59+
- storeTests.spec.ts
60+
- userTests.spec.ts
61+
- petTests.spec.js
6262

6363
Refer to the Playwright documentation - https://playwright.dev/docs/intro for writing and structuring tests.
6464

65-
## CI/CD Integration
66-
67-
To integrate Playwright with CI/CD pipelines, configure .github/workflows/playwright.yml or relevant CI scripts. Example:
68-
69-
name: Playwright Tests
70-
on: [push, pull_request]
71-
72-
jobs:
73-
test:
74-
runs-on: ubuntu-latest
75-
steps:
76-
- name: Checkout code
77-
uses: actions/checkout@v3
78-
- name: Install dependencies
79-
run: npm install
80-
- name: Install Playwright Browsers
81-
run: npx playwright install --with-deps
82-
- name: Run tests
83-
run: npx playwright test
65+
## CI Integration
66+
67+
To integrate Playwright with CI/CD pipelines, configure .github/workflows/playwright.yml or relevant CI scripts. More - https://playwright.dev/docs/ci-intro
8468

8569
## Troubleshooting
8670

playwright.config.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ export default defineConfig({
4545
use: { ...devices['Desktop Chrome'] },
4646
},
4747

48-
{
49-
name: 'firefox',
50-
use: { ...devices['Desktop Firefox'] },
51-
},
48+
// {
49+
// name: 'firefox',
50+
// use: { ...devices['Desktop Firefox'] },
51+
// },
5252

53-
{
54-
name: 'webkit',
55-
use: { ...devices['Desktop Safari'] },
56-
},
53+
// {
54+
// name: 'webkit',
55+
// use: { ...devices['Desktop Safari'] },
56+
// },
5757

5858
/* Test against mobile viewports. */
5959
// {

tests/image.jpeg

981 KB
Loading

tests/petTests.spec.ts

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
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

Comments
 (0)