updated link on awaiting submission view allin welsh Related work items: #24389
83 lines
2.5 KiB
JavaScript
83 lines
2.5 KiB
JavaScript
import { test, expect } from "@playwright/test";
|
|
|
|
// TO DO - build PEDW API playwright harness
|
|
|
|
// Define a base URL for the mock API
|
|
const BASE_URL = process.env.BASE_URL;
|
|
|
|
test.describe("User API Validation Suite", () => {
|
|
// 1. GET Request Example
|
|
test("should fetch a list of users and validate headers", async ({
|
|
request,
|
|
}) => {
|
|
const response = await request.get(`${BASE_URL}/users/1`);
|
|
|
|
// Assert the HTTP status code is 200 OK
|
|
expect(response.ok()).toBeTruthy();
|
|
expect(response.status()).toBe(200);
|
|
|
|
// Parse the JSON response body
|
|
const responseBody = await response.json();
|
|
|
|
// Assert specific data fields inside the JSON
|
|
expect(responseBody.id).toBe(1);
|
|
expect(responseBody.name).toBe("Leanne Graham");
|
|
expect(responseBody.email).toBe("Sincere@april.biz");
|
|
|
|
// Assert response headers
|
|
expect(response.headers()["content-type"]).toContain("application/json");
|
|
});
|
|
|
|
// 2. POST Request Example
|
|
test("should successfully create a new post", async ({ request }) => {
|
|
const response = await request.post(`${BASE_URL}/posts`, {
|
|
headers: {
|
|
"Content-type": "application/json; charset=UTF-8",
|
|
// 'Authorization': `Bearer ${process.env.API_TOKEN}` <-- Example for CI/CD tokens
|
|
},
|
|
data: {
|
|
title: "Automation Test",
|
|
body: "Testing APIs with Playwright in DevOps pipelines.",
|
|
userId: 99,
|
|
},
|
|
});
|
|
|
|
// Assert creation status code (201 Created)
|
|
expect(response.status()).toBe(201);
|
|
|
|
const responseBody = await response.json();
|
|
|
|
// Assert the API returned our sent data with a new generated ID
|
|
expect(responseBody).toMatchObject({
|
|
title: "Automation Test",
|
|
body: "Testing APIs with Playwright in DevOps pipelines.",
|
|
userId: 99,
|
|
id: 101, // Mock database increments ID to 101
|
|
});
|
|
});
|
|
|
|
// 3. PUT Request Example (Update)
|
|
test("should update an existing post", async ({ request }) => {
|
|
const response = await request.put(`${BASE_URL}/posts/1`, {
|
|
data: {
|
|
id: 1,
|
|
title: "Updated Title",
|
|
body: "Updated body content",
|
|
userId: 1,
|
|
},
|
|
});
|
|
|
|
expect(response.ok()).toBeTruthy();
|
|
const responseBody = await response.json();
|
|
expect(responseBody.title).toBe("Updated Title");
|
|
});
|
|
|
|
// 4. DELETE Request Example
|
|
test("should delete a post", async ({ request }) => {
|
|
const response = await request.delete(`${BASE_URL}/posts/1`);
|
|
|
|
// Assert successful deletion response status
|
|
expect(response.status()).toBe(200);
|
|
});
|
|
});
|