Skip to content

Commit 21352cf

Browse files
committed
adding files to repo
1 parent 6330c03 commit 21352cf

16 files changed

Lines changed: 517 additions & 0 deletions

.github/workflows/playwright.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Playwright Tests
2+
on:
3+
push:
4+
branches: [ main, master ]
5+
pull_request:
6+
branches: [ main, master ]
7+
jobs:
8+
test:
9+
timeout-minutes: 60
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
- uses: actions/setup-node@v4
14+
with:
15+
node-version: lts/*
16+
- name: Install dependencies
17+
run: npm ci
18+
- name: Install Playwright Browsers
19+
run: npx playwright install --with-deps
20+
- name: Run Playwright tests
21+
run: npx playwright test
22+
- uses: actions/upload-artifact@v4
23+
if: ${{ !cancelled() }}
24+
with:
25+
name: playwright-report
26+
path: playwright-report/
27+
retention-days: 30

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
# Playwright
3+
node_modules/
4+
/test-results/
5+
/playwright-report/
6+
/blob-report/
7+
/playwright/.cache/
8+
/playwright/.auth/

package-lock.json

Lines changed: 97 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "playwright_api_testing",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {},
7+
"keywords": [],
8+
"author": "",
9+
"license": "ISC",
10+
"type": "commonjs",
11+
"devDependencies": {
12+
"@playwright/test": "^1.58.2",
13+
"@types/node": "^25.3.1"
14+
}
15+
}

playwright.config.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// @ts-check
2+
import { defineConfig, devices } from '@playwright/test';
3+
4+
/**
5+
* Read environment variables from file.
6+
* https://github.com/motdotla/dotenv
7+
*/
8+
// import dotenv from 'dotenv';
9+
// import path from 'path';
10+
// dotenv.config({ path: path.resolve(__dirname, '.env') });
11+
12+
/**
13+
* @see https://playwright.dev/docs/test-configuration
14+
*/
15+
export default defineConfig({
16+
testDir: './tests',
17+
/* Run tests in files in parallel */
18+
fullyParallel: true,
19+
/* Fail the build on CI if you accidentally left test.only in the source code. */
20+
forbidOnly: !!process.env.CI,
21+
/* Retry on CI only */
22+
retries: process.env.CI ? 2 : 0,
23+
/* Opt out of parallel tests on CI. */
24+
workers: process.env.CI ? 1 : undefined,
25+
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
26+
reporter: 'html',
27+
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
28+
use: {
29+
/* Base URL to use in actions like `await page.goto('')`. */
30+
// baseURL: 'http://localhost:3000',
31+
32+
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
33+
trace: 'on-first-retry',
34+
},
35+
36+
/* Configure projects for major browsers */
37+
projects: [
38+
{
39+
name: 'chromium',
40+
use: { ...devices['Desktop Chrome'] },
41+
},
42+
43+
{
44+
name: 'firefox',
45+
use: { ...devices['Desktop Firefox'] },
46+
},
47+
48+
{
49+
name: 'webkit',
50+
use: { ...devices['Desktop Safari'] },
51+
},
52+
53+
/* Test against mobile viewports. */
54+
// {
55+
// name: 'Mobile Chrome',
56+
// use: { ...devices['Pixel 5'] },
57+
// },
58+
// {
59+
// name: 'Mobile Safari',
60+
// use: { ...devices['iPhone 12'] },
61+
// },
62+
63+
/* Test against branded browsers. */
64+
// {
65+
// name: 'Microsoft Edge',
66+
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
67+
// },
68+
// {
69+
// name: 'Google Chrome',
70+
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
71+
// },
72+
],
73+
74+
/* Run your local dev server before starting the tests */
75+
// webServer: {
76+
// command: 'npm run start',
77+
// url: 'http://localhost:3000',
78+
// reuseExistingServer: !process.env.CI,
79+
// },
80+
});
81+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"firstname": "{0}",
3+
"lastname": "{1}",
4+
"totalprice": 1000,
5+
"depositpaid": true,
6+
"bookingdates": {
7+
"checkin": "2021-01-01",
8+
"checkout": "2022-01-01"
9+
},
10+
"additionalneeds": "{2}"
11+
}

test-data/post_request_body.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"firstname": "Mandy",
3+
"lastname": "Rose",
4+
"totalprice": 1000,
5+
"depositpaid": true,
6+
"bookingdates": {
7+
"checkin": "2021-01-01",
8+
"checkout": "2022-01-01"
9+
},
10+
"additionalneeds": "super bowls"
11+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const {test, expect} = require('@playwright/test')
2+
3+
test('Create POST API request using static request body' , async({request}) => { //request object is used to make API calls: GET, POST ..
4+
//API POST resquest
5+
const postAPIResponse= await request.post('https://restful-booker.herokuapp.com/booking', {
6+
data: {
7+
"firstname": "Mandy",
8+
"lastname": "Rose",
9+
"totalprice": 1000,
10+
"depositpaid": true,
11+
"bookingdates": {
12+
"checkin": "2021-01-01",
13+
"checkout": "2022-01-01"
14+
},
15+
"additionalneeds": "super bowls"
16+
}
17+
18+
})
19+
const postAPIResponseBody = await postAPIResponse.json();
20+
21+
//verify response status code is 200 and ok
22+
expect(postAPIResponse.status()).toBe(200);
23+
expect(postAPIResponse.ok()).toBeTruthy();
24+
25+
//verify response contains some text
26+
const text = await postAPIResponse.text();
27+
expect(text).toContain('Mandy');
28+
29+
//validate JSON API response
30+
expect(postAPIResponseBody.booking).toHaveProperty("lastname","Rose");
31+
32+
//write response on the console
33+
console.log(postAPIResponseBody);
34+
35+
//validate nested JSON objects
36+
expect(postAPIResponseBody.booking.bookingdates).toHaveProperty("checkin","2021-01-01");
37+
38+
39+
})
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const {test, expect} = require('@playwright/test')
2+
3+
//create a variable to read the JSON file
4+
const bookingAPIRequestBody = require('../test-data/post_request_body.json');
5+
6+
test('Create POST API request using static JSON file' , async({request}) => { //request object is used to make API calls: GET, POST ..
7+
//API POST resquest
8+
const postAPIResponse= await request.post('https://restful-booker.herokuapp.com/booking', {
9+
10+
data: bookingAPIRequestBody //pass the variable to post request variable
11+
12+
})
13+
const postAPIResponseBody = await postAPIResponse.json();
14+
15+
//verify response status code is 200 and ok
16+
expect(postAPIResponse.status()).toBe(200);
17+
expect(postAPIResponse.ok()).toBeTruthy();
18+
19+
//verify response contains some text
20+
const text = await postAPIResponse.text();
21+
expect(text).toContain('Mandy');
22+
23+
//validate JSON API response
24+
expect(postAPIResponseBody.booking).toHaveProperty("lastname","Rose");
25+
26+
//write response on the console
27+
console.log(postAPIResponseBody);
28+
29+
//validate nested JSON objects
30+
expect(postAPIResponseBody.booking.bookingdates).toHaveProperty("checkin","2021-01-01");
31+
32+
33+
})
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {test, expect } from '@playwright/test';
2+
3+
test('Create API GET Request for Booking IDs', async ({request}) => {
4+
//send a GET request & store response in a variable
5+
const response = await request.get('https://restful-booker.herokuapp.com/booking');
6+
7+
//verify the status code of the response is 200
8+
expect(response.status()).toBe(200);
9+
expect(response.ok).toBeTruthy();
10+
11+
//check response contains some text
12+
const value = await response.text();
13+
expect(value).toContain('bookingid');
14+
15+
//write response on the console
16+
console.log(response.json());
17+
})

0 commit comments

Comments
 (0)