Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
darvinpatel committed Sep 17, 2024
0 parents commit 1a20145
Show file tree
Hide file tree
Showing 20 changed files with 1,202 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
USERNAME_ADMIN=admin
USER_ADMIN_PASSWORD=password123
TOKEN=
BOOKING_ID_1=
BOOKING_ID_2=
BOOKING_ID_3=
BOOKING_ID_4=
BOOKING_ID_5=
BOOKING_ID_6=
BOOKING_ID_7=
27 changes: 27 additions & 0 deletions .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Playwright Tests
on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]
jobs:
test:
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
- name: Install dependencies
run: npm ci
- name: Install Playwright Browsers
run: npx playwright install --with-deps
- name: Run Playwright tests
run: npx playwright test
- uses: actions/upload-artifact@v3
if: always()
with:
name: playwright-report
path: playwright-report/
retention-days: 30
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
/test-results/
/playwright-report/
/playwright/.cache/
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<h1>
Practice api test automation with <a href="https://playwright.dev/"> <img width="140" alt="Playwright Logo" src="https://raw.githubusercontent.com/github/explore/60cd2530141f67f07a947fa2d310c482e287e387/topics/playwright/playwright.png" /> </a> on <a href="https://restful-booker.herokuapp.com/apidoc/index.html#api-Booking-PartialUpdateBooking">Restful-booker</a>
</h1>

> **Note**
>
> + **<a href="https://restful-booker.herokuapp.com/apidoc/index.html">Restful-booker</a>** is a public REST API that you can use to learn more about API Testing or try out API testing tools against. Restful-booker is a Create Read Update Delete Web API that comes with authentication features and loaded with a bunch of bugs for you to explore.
>
## PLaywright features
API testing using:

- Playwright https://playwright.dev/
- TypeScript https://www.typescriptlang.org/

This tests are purely for Playwright features practice.


## Getting Started
### Prerequisites
You need to have Node.js installed on your machine.

## Useful Commands

### Run all tests in Playwright

```shell
npm run test
```
### Run all tests and show test report
```shell
npm run report
```
### Run smoke tests
```shell
npm run smoke-tests
```
### Run api tests for get operation
```shell
npm run test-get
```
### Run api tests for post operation
```shell
npm run test-post
```
### Run api tests for put operation
```shell
npm run test-put
```
### Run api tests for patch operation
```shell
npm run test-patch
```
### Run api tests for delete operation
```shell
npm run test-delete
```
31 changes: 31 additions & 0 deletions apitest/createBooking/restful-booker.createBooking.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { expect, test } from "playwright/test";
import bookingData = require("../../test-data/post-booking.json");

test("Create Booking", async ({ request, baseURL }) => {
const response = await request.post(`${baseURL}/booking`, {
data: bookingData,
});

const responseBody = await response.json();
process.env.BOOKING_ID_1 = responseBody.bookingid;

expect(response.status()).toBe(200);
expect(response.ok()).toBeTruthy();
expect(responseBody.booking).toHaveProperty(
"firstname",
bookingData.firstname
);
expect(responseBody.booking).toHaveProperty("lastname", bookingData.lastname);
expect(responseBody.booking).toHaveProperty(
"totalprice",
bookingData.totalprice
);
expect(responseBody.booking).toHaveProperty(
"depositpaid",
bookingData.depositpaid
);
expect(responseBody.booking).toHaveProperty(
"additionalneeds",
bookingData.additionalneeds
);
});
36 changes: 36 additions & 0 deletions apitest/deleteBooking/restful-booker.deleteBooking.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { expect, test } from "playwright/test";
import bookingData = require("../../test-data/booking.json");

test.beforeEach("Create Booking", async ({ request, baseURL }) => {
const response = await request.post(`${baseURL}/booking`, {
data: bookingData,
});
expect(response.status()).toBe(200);
expect(response.ok()).toBeTruthy();

const responseBody = await response.json();
process.env.BOOKING_ID_2 = responseBody.bookingid;
});

test("Delete Booking @delete", async ({ request, baseURL }) => {
let ID = process.env.BOOKING_ID_2;
const url = `${baseURL}/booking/`;
const response2 = await request.get(url + ID, {});
expect(response2.status()).toBe(200);

const response = await request.delete(url + ID, {
headers: {
Cookie: `token=${process.env.TOKEN}`,
Accept: "application/json",
},
});

expect(response.ok()).toBeTruthy();
expect(response.status()).toBe(201);
expect(response.statusText()).toBe("Created");

const getUrl = `${baseURL}/booking/`;
const getResponse = await request.get(getUrl + ID, {});
expect(getResponse.status()).toBe(404);
expect(getResponse.statusText()).toBe("Not Found");
});
135 changes: 135 additions & 0 deletions apitest/getBooking/restful-booker.getBooking.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import { expect, test } from "playwright/test";
import bookingData = require("../../test-data/booking.json");

test.beforeEach("Create Booking", async ({ request, baseURL }) => {
const response = await request.post(`${baseURL}/booking`, {
data: bookingData,
});
expect(response.status()).toBe(200);
expect(response.ok()).toBeTruthy();

const responseBody = await response.json();
process.env.BOOKING_ID_3 = responseBody.bookingid;
});

test("Get Booking By Lastname @get", async ({ request, baseURL }) => {
const response = await request.get(`${baseURL}/booking`, {
params: {
lastname: "Brown",
},
});
expect(response.ok()).toBeTruthy();
expect(response.status()).toBe(200);

let responseAsString = await response.body().then((b) => {
let data = JSON.parse(b.toString());
return data.filter(
(d: { bookingid: string | undefined }) =>
d.bookingid == process.env.BOOKING_ID_3
);
});

let ID = process.env.BOOKING_ID_3;
const a = ID;

expect(responseAsString[0] !== undefined).toBe(true);

if (a) {
expect(responseAsString[0]).toHaveProperty("bookingid", +a);
}
});

test("Get Booking By Firstname @get", async ({ request, baseURL }) => {
const response = await request.get(`${baseURL}/booking`, {
params: {
firstname: "Sally",
},
});
expect(response.ok()).toBeTruthy();
expect(response.status()).toBe(200);

let responseAsString = await response.body().then((b) => {
let data = JSON.parse(b.toString());
return data.filter(
(d: { bookingid: string | undefined }) =>
d.bookingid == process.env.BOOKING_ID_3
);
});

let ID = process.env.BOOKING_ID_3;
const a = ID;

expect(responseAsString[0] !== undefined).toBe(true);

if (a) {
expect(responseAsString[0]).toHaveProperty("bookingid", +a);
}
});

test.skip("Get Booking By Checkin Date @get", async ({ request, baseURL }) => {
const response = await request.get(`${baseURL}/booking`, {
params: {
checkin: "2013-02-25",
},
});
expect(response.ok()).toBeTruthy();
expect(response.status()).toBe(200);

let responseAsString = await response.body().then((b) => {
let data = JSON.parse(b.toString());
return data.filter(
(d: { bookingid: string | undefined }) =>
d.bookingid == process.env.BOOKING_ID_3
);
});

let ID = process.env.BOOKING_ID_3;
const a = ID;

expect(responseAsString[0] !== undefined).toBe(true);

if (responseAsString[0] !== undefined && a) {
expect(responseAsString[0]).toHaveProperty("bookingid", +a);
}
});

test("Get Booking By Checkout Date @get", async ({ request, baseURL }) => {
const response = await request.get(`${baseURL}/booking`, {
params: {
checkout: "2014-10-23",
},
});
expect(response.ok()).toBeTruthy();
expect(response.status()).toBe(200);

let responseAsString = await response.body().then((b) => {
let data = JSON.parse(b.toString());
return data.filter(
(d: { bookingid: string | undefined }) =>
d.bookingid == process.env.BOOKING_ID_3
);
});

let ID = process.env.BOOKING_ID_3;
const a = ID;

expect(responseAsString[0] !== undefined).toBe(true);

if (a) {
expect(responseAsString[0]).toHaveProperty("bookingid", +a);
}
});

test("Get Booking By ID @get", async ({ request, baseURL }) => {
let ID = process.env.BOOKING_ID_3;
const url = `${baseURL}/booking/`;
const response = await request.get(url + ID, {});
const responseBody = await response.json();

expect(response.status()).toBe(200);
expect(response.ok()).toBeTruthy();
expect(responseBody).toHaveProperty("firstname", bookingData.firstname);
expect(responseBody).toHaveProperty("lastname", bookingData.lastname);
expect(responseBody).toHaveProperty("totalprice", bookingData.totalprice);
expect(responseBody).toHaveProperty("depositpaid", bookingData.depositpaid);
});
33 changes: 33 additions & 0 deletions apitest/getBooking/restful-booker.getBookings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { expect, test } from "playwright/test";
import bookingData = require("../../test-data/booking.json");

test.beforeEach("Create Booking", async ({ request, baseURL }) => {
const response = await request.post(`${baseURL}/booking`, {
data: bookingData,
});
expect(response.status()).toBe(200);
expect(response.ok()).toBeTruthy();

const responseBody = await response.json();
process.env.BOOKING_ID_4 = responseBody.bookingid;
});

test("Get BookingIds", async ({ request, baseURL }) => {
const response = await request.get(`${baseURL}/booking`);
expect(response.ok()).toBeTruthy();
expect(response.status()).toBe(200);

let responseAsString = await response.body().then((b) => {
let data = JSON.parse(b.toString());
return data.filter(
(d: { bookingid: string | undefined }) =>
d.bookingid == process.env.BOOKING_ID_4
);
});

let ID = process.env.BOOKING_ID_4;
const a = ID;
if (a) {
expect(responseAsString[0]).toHaveProperty("bookingid", +a);
}
});
7 changes: 7 additions & 0 deletions apitest/smokeTests/restful-booker.healthcheck.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { expect, test } from "playwright/test";

test("@smoke - Service HealthCheck", async ({ request, baseURL }) => {
const response = await request.get(`${baseURL}/ping`);
expect(response.status()).toBe(201);
expect(response.ok()).toBeTruthy();
});
44 changes: 44 additions & 0 deletions apitest/updateBooking/restful-booker.partialUpdateBooking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { expect, test } from "playwright/test";
import bookingData = require("../../test-data/update-booking.json");

test.beforeEach("Create Booking", async ({ request, baseURL }) => {
const response = await request.post(`${baseURL}/booking`, {
data: bookingData,
});
expect(response.status()).toBe(200);
expect(response.ok()).toBeTruthy();

const responseBody = await response.json();
process.env.BOOKING_ID_5 = responseBody.bookingid;
});

test("Update Booking Partially - Udpate firstname and lastname @patch", async ({ request, baseURL }) => {
let ID = process.env.BOOKING_ID_5;
const url = `${baseURL}/booking/`;
const response2 = await request.get(url + ID, {});
expect(response2.status()).toBe(200);

const response = await request.put(url + ID, {
headers: {
Cookie: `token=${process.env.TOKEN}`,
Accept: "application/json",
},
data: {
firstname: "James",
lastname: "Brown",
},
});
expect(response.status()).toBe(200);
expect(response.ok()).toBeTruthy();
const responseBody = await response.json();

console.log(responseBody);
expect(responseBody).toHaveProperty("firstname", "James");
expect(responseBody).toHaveProperty("lastname", "Brown");
expect(responseBody).toHaveProperty("totalprice", bookingData.totalprice);
expect(responseBody).toHaveProperty("depositpaid", bookingData.depositpaid);
expect(responseBody).toHaveProperty(
"additionalneeds",
bookingData.additionalneeds
);
});
Loading

0 comments on commit 1a20145

Please sign in to comment.