This framework is built to show case how quickly we can generate the runnable API test scripts using Swagger.json and swagger-typescript-api with help of Playwright test runner.
- Node.js: Ensure Node.js is installed on your system.
- Playwright: Install Playwright globally or locally in your project.
- Dependencies: Install project dependencies using
npm install.
-
Clone the repository:
git clone <repository-url> cd <repository-folder>
-
Install dependencies:
npm install
-
Add the swagger json file and run the following command
npx swagger-typescript-api generate --path ./<swagger.json> -
Now the
Api.tsclient file get generated -
Add the prompt to the path
.github/prompts/** -
Open github copilot chat and run the prompt
-
Run all tests:
npx playwright test -
Run a specific test file:
npx playwright test tests/api/<test-file>.spec.ts
-
View the HTML report:
npx playwright show-report
- Add debugging logs in test files to capture intermediate values:
console.log('Response:', response);
- Fixtures: The
tests/fixtures.tsfile initializes the API client. - Test Files: Each API endpoint has a corresponding test file in
tests/api. - Assertions: Tests include assertions for response status codes and data validation.
import { test } from '../fixtures';
import { expect } from '@playwright/test';
test('Get pet by ID', async ({ apiClient }) => {
const petId = 1;
const response = await apiClient.pet.getPetById(petId);
console.log('Response:', response);
expect(response.status).toBe(200);
expect(response.data.name).toBe('Doggie');
});- Ensure the
pets.jsonfile is up-to-date with the latest API definitions. - Use the Swagger Petstore base URL (
https://petstore.swagger.io/v2) for API requests.