Skip to content

Commit

Permalink
cleanup data after and add env files
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonaZmole committed Nov 1, 2023
1 parent 7e961ba commit 4adb0ce
Show file tree
Hide file tree
Showing 20 changed files with 118 additions and 76 deletions.
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
URL="https://automationintesting.online/"
ADMIN_URL="https://automationintesting.online/#/admin"
ADMIN_USERNAME="admin"
ADMIN_PASSWORD="password"
4 changes: 4 additions & 0 deletions .env.qa
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
URL="https://automationintesting.online/"
ADMIN_URL="https://automationintesting.online/#/admin"
ADMIN_USERNAME="admin"
ADMIN_PASSWORD="password"
4 changes: 4 additions & 0 deletions .env.staging
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
URL="https://automationintesting.online/"
ADMIN_URL="https://automationintesting.online/#/admin"
ADMIN_USERNAME="admin"
ADMIN_PASSWORD="password"
7 changes: 2 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,5 @@ node_modules/

megalinter-reports/

/.vs/PlawrightWithTypeScript/FileContentIndex/620e1add-b41d-417d-9db6-26d9b9970d52.vsidx
/.vs/PlawrightWithTypeScript/v17/.wsuo
/.vs/PlawrightWithTypeScript/v17/TestStore/0/000.testlog
/.vs/PlawrightWithTypeScript/v17/TestStore/0/testlog.manifest
/.vs/slnx.sqlite
/.vs/*
.vscode/*
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"files.exclude": {
"**/.vs": true,
"**/.vscode": true,
"**/node_modules": true,
"**/playwright-report": true,
"**/test-results": true
Expand Down
44 changes: 39 additions & 5 deletions helpers/api-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,44 @@
import Env from "./env"
import { request } from "@playwright/test";
import { GetRoomsOutput } from "./models/api/get-room-output";


async function deleteRoom(roomName:string) {
let rooms=getRooms();
async function getRequestContext() {
return await request.newContext({
baseURL: Env.URL,
extraHTTPHeaders: { Accept: 'application/json', },
});
}

async function getLoginToken() {
const response = await (await getRequestContext()).post("auth/login", {
data: {
username: Env.ADMIN_USERNAME,
password: Env.ADMIN_PASSWORD,
},
});

async function getRooms() {

}
const headers = response.headers();
let tokenString = headers["set-cookie"].split(";")[0];
return tokenString.split("=")[1];
}

export async function getRooms(): Promise<GetRoomsOutput> {
const response = await (await getRequestContext()).get("room/", {
headers: {
cookie: `token=${await getLoginToken()}`
},
});
return await response.json() as GetRoomsOutput;
}

export async function deleteRoom(roomName: number) {
let roomsList = await getRooms();
let roomId = roomsList.rooms.filter(x => x.roomName == roomName)[0].roomid;

const response = await (await getRequestContext()).delete(`room/${roomId}`, {
headers: {
cookie: `token=${await getLoginToken()}`
},
});
}
7 changes: 0 additions & 7 deletions helpers/constants.ts

This file was deleted.

6 changes: 6 additions & 0 deletions helpers/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default class Env {
public static readonly URL = process.env.URL;
public static readonly ADMIN_URL = process.env.ADMIN_URL;
public static readonly ADMIN_USERNAME = process.env.ADMIN_USERNAME;
public static readonly ADMIN_PASSWORD = process.env.ADMIN_PASSWORD;
}
15 changes: 15 additions & 0 deletions helpers/models/api/get-room-output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

interface Room {
roomid: number;
roomName: number;
type: string;
accessible: boolean;
image: string;
description: string;
features: string[];
roomPrice: number;
}

export interface GetRoomsOutput {
rooms: Room[];
}
3 changes: 1 addition & 2 deletions helpers/models/enums/room-type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
enum RoomType
{
enum RoomType {
Single,
Twin,
Double,
Expand Down
2 changes: 1 addition & 1 deletion helpers/models/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ export class Room {
accessible: string = "true";
price: string = faker.number.int(999).toString();
roomDetails: string = "TV";
}
}
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@types/node": "^20.8.7"
},
"dependencies": {
"dotenv": "^16.3.1",
"faker": "^6.6.6"
}
}
5 changes: 2 additions & 3 deletions pages/admin-header-page.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Locator, Page, expect } from "@playwright/test";

class AdminHeaderPage {
export default class AdminHeaderPage {

readonly #page: Page

Expand All @@ -15,5 +15,4 @@ class AdminHeaderPage {
async isLogoutButtonDisplayed(): Promise<boolean> {
return await this.#logoutButton.isVisible();
}
}
export default AdminHeaderPage;
}
2 changes: 1 addition & 1 deletion pages/base-page.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type Locator, type Page } from "@playwright/test";

export class BasePage {
export default class BasePage {
readonly #page: Page;

readonly #errorMessages: Locator;
Expand Down
10 changes: 4 additions & 6 deletions pages/login-page.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Page, Locator } from '@playwright/test';
import { ADMIN_USERNAME, ADMIN_PASSWORD } from '../helpers/constants';
import Env from "../helpers/env"

class LoginPage {
export default class LoginPage {
readonly #page: Page;

readonly #usernameInput: Locator;
Expand All @@ -20,8 +20,8 @@ class LoginPage {
async login(username?: string, password?: string) {

if (typeof username == 'undefined' || typeof password == 'undefined') {
await this.#usernameInput.fill(ADMIN_USERNAME);
await this.#passwordInput.fill(ADMIN_PASSWORD);
await this.#usernameInput.fill(Env.ADMIN_USERNAME!);
await this.#passwordInput.fill(Env.ADMIN_PASSWORD!);
}
else {
await this.#usernameInput.fill(username);
Expand All @@ -32,5 +32,3 @@ class LoginPage {
await this.#page.waitForResponse(x => x.url().includes('/auth/login'))
}
}

export default LoginPage;
6 changes: 2 additions & 4 deletions pages/rooms-page.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Locator, Page } from "@playwright/test";
import { BasePage } from "./base-page";
import { Room } from "../helpers/models/room";
import RoomType from "../helpers/models/enums/room-type";
import BasePage from "./base-page";

class RoomsPage extends BasePage {
export default class RoomsPage extends BasePage {
readonly #page: Page;

readonly #createButton: Locator;
Expand Down Expand Up @@ -57,4 +56,3 @@ class RoomsPage extends BasePage {
}

}
export default RoomsPage;
13 changes: 11 additions & 2 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { defineConfig, devices } from '@playwright/test';

// require('dotenv').config();
//require('dotenv').config();

if (process.env.test_env) {
require('dotenv').config({
path: `.env.${process.env.test_env}`,
override: true,
});
} else {
require('dotenv').config();
}

export default defineConfig({
testDir: './tests',
Expand All @@ -9,7 +18,7 @@ export default defineConfig({
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 4 : undefined,
workers: process.env.CI ? 1 : undefined,
reporter: 'html',
use: {
screenshot: 'only-on-failure',
Expand Down
47 changes: 11 additions & 36 deletions tests/admin/create-room.spec.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
import { APIRequestContext, expect, test } from '@playwright/test';
import { ADMIN_URL } from '../../helpers/constants';
import { expect, test } from '@playwright/test';
import LoginPage from '../../pages/login-page';
import RoomsPage from '../../pages/rooms-page';
import { Room } from '../../helpers/models/room';
import RoomType from '../../helpers/models/enums/room-type';
import { URL } from '../../helpers/constants';
import { deleteRoom } from '../../helpers/api-helpers'
import Env from "../../helpers/env"

let loginPage: LoginPage;
let roomsPage: RoomsPage;
let room: Room;

let apiContext: APIRequestContext;

//test.describe.configure({ mode: 'serial' });

test.beforeEach(async ({ page, playwright }) => {

let apiContext: APIRequestContext;

apiContext = await playwright.request.newContext({
baseURL: URL,
extraHTTPHeaders: {
// Authorization: `Basic ${Buffer.from(`${userName}:${password}`).toString('base64')}`,
Accept: 'application/json',
},
});

var t= await apiContext["post"]("/auth/login", { data: { username: "admin", password: "password" } });
test.describe.configure({ mode: 'serial' });

test.beforeEach(async ({ page }) => {
loginPage = new LoginPage(page);
roomsPage = new RoomsPage(page)
});
Expand All @@ -41,7 +27,7 @@ const testCases = [

for (const { roomType } of testCases)
test(`${RoomType[roomType]} room can be created`, async ({ page }) => {
await page.goto(ADMIN_URL);
await page.goto(Env.ADMIN_URL!);
await loginPage.login();

await roomsPage.createRoom();
Expand All @@ -50,26 +36,15 @@ for (const { roomType } of testCases)
let errorMessages = await roomsPage.getErrorMessages();
expect(errorMessages).toContain("Room name must be set")
expect(errorMessages).toContain("must be greater than or equal to 1")
let room = new Room();
room = new Room();
room.type = RoomType[roomType];

await roomsPage.insertRoomDetails(room);
await roomsPage.createRoom();
expect(await roomsPage.getLastRoomDetails()).toEqual(room);
});

test.afterEach(async ({ page, playwright }) => {
test.afterEach(async ({ page }) => {
await page.close();

let apiContext: APIRequestContext;

apiContext = await playwright.request.newContext({
baseURL: URL,
extraHTTPHeaders: {
// Authorization: `Basic ${Buffer.from(`${userName}:${password}`).toString('base64')}`,
Accept: 'application/json',
},
});

var t= await apiContext["post"]("/auth/login", { data: { username: "admin", password: "password" } });
});
await deleteRoom(Number(room.roomName));
});
7 changes: 3 additions & 4 deletions tests/admin/login.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { expect, test } from '@playwright/test';
import { ADMIN_USERNAME, ADMIN_PASSWORD } from '../../helpers/constants';
import { ADMIN_URL } from '../../helpers/constants';
import LoginPage from '../../pages/login-page';
import AdminHeaderPage from '../../pages/admin-header-page'
import Env from '../../helpers/env';

let loginPage: LoginPage;
let adminHeaderPage: AdminHeaderPage;
Expand All @@ -13,13 +12,13 @@ test.beforeEach(async ({ page }) => {
});

const testCases = [
{ username: ADMIN_USERNAME, password: ADMIN_PASSWORD, isLoggedIn: true },
{ username: Env.ADMIN_USERNAME, password: Env.ADMIN_PASSWORD, isLoggedIn: true },
{ username: 'invalidUser', password: 'invalidPassword', isLoggedIn: false },
];

for (const { username, password, isLoggedIn } of testCases)
test(`Login as user ${username}`, async ({ page }) => {
await page.goto(ADMIN_URL);
await page.goto(Env.ADMIN_URL!);
await loginPage.login(username, password);
await expect(await adminHeaderPage.isLogoutButtonDisplayed()).toBe(isLoggedIn);
});
Expand Down

0 comments on commit 4adb0ce

Please sign in to comment.