Skip to content

Commit

Permalink
all test file refactor with custom fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
hasan-py committed Oct 31, 2024
1 parent c2d2e57 commit 6ee4a1b
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 227 deletions.
59 changes: 59 additions & 0 deletions client/e2e/fixtures/game.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import type { Page } from "@playwright/test";
import { test as base, expect } from "@playwright/test";
import { gameListResponse } from "../mockResponses";
import { API_ROUTES } from "../support/constants";

type GameForm = {
gameName: string;
gameDescription: string;
gameImage: string;
};

export class Game {
constructor(public readonly page: Page) {}

async gotoGameList(list?: typeof gameListResponse) {
await this.page.route(API_ROUTES.GAME_LIST, async (route) => {
await route.fulfill({ json: list || gameListResponse });
});

await this.page.goto("/");
}

async gameFormValidation() {
await expect(
this.page.locator("text=Game Name must be provided")
).toBeVisible();
await expect(
this.page.locator("text=Game Image must be a valid URL")
).toBeVisible();
await expect(
this.page.locator("text=Description must be provided")
).toBeVisible();
}

async fillGameForm({ gameName, gameDescription, gameImage }: GameForm) {
const nameInput = this.page.locator('input[placeholder="Game Name"]');
const descriptionInput = this.page.locator(
'textarea[placeholder="Game description..."]'
);
const imageInput = this.page.locator(
'input[placeholder="Game Image (URL Only)"]'
);

await nameInput.fill(gameName);
await descriptionInput.fill(gameDescription);
await imageInput.fill(gameImage);
}

gameReviewUrl(id: string) {
return `${API_ROUTES.GAME_DETAILS + "/" + id}`;
}
}

export const gameTest = base.extend<{ game: Game }>({
game: async ({ page }, use) => {
const game = new Game(page);
await use(game);
},
});
5 changes: 3 additions & 2 deletions client/e2e/fixtures/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect, mergeTests } from "@playwright/test";
import { gameTest } from "./game";
import { loginTest } from "./login";

const test = mergeTests(loginTest);
const test = mergeTests(loginTest, gameTest);

export { test, expect };
export { expect, test };
30 changes: 27 additions & 3 deletions client/e2e/fixtures/login.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { test as base } from "@playwright/test";
import type { Page, Locator } from "@playwright/test";
import { API_ROUTES } from "../support/constants";
import { gameListResponse, moderatorExistsResponse } from "../mockResponses";
import {
createModeratorResponse,
gameListResponse,
moderatorExistsResponse,
moderatorNotExistsResponse,
} from "../mockResponses";

export class Login {
private readonly inputName: Locator;
private readonly inputEmail: Locator;
private readonly inputPassword: Locator;
private readonly submitButton: Locator;

constructor(public readonly page: Page) {
this.inputName = this.page.locator('input[placeholder="Name"]');
this.inputEmail = this.page.locator('input[placeholder="Email"]');
this.inputPassword = this.page.locator('input[placeholder="Password"]');
this.submitButton = this.page.locator("button");
Expand All @@ -27,9 +34,26 @@ export class Login {
await this.submitButton.click();
}

async gotoLoginPage() {
async submitCreateForm() {
await this.inputName.fill("John Doe");
await this.inputEmail.fill("[email protected]");
await this.inputPassword.fill("password123");

await this.page.route(API_ROUTES.CREATE_MODERATOR, async (route) => {
await route.fulfill({ json: createModeratorResponse });
});

await this.submitButton.click();
}

// This function will work as login and create page based on the argument
async gotoLoginPage(isCreatePage?: boolean) {
await this.page.route(API_ROUTES.CHECK_MODERATOR, async (route) => {
await route.fulfill({ json: moderatorExistsResponse });
await route.fulfill({
json: isCreatePage
? moderatorNotExistsResponse
: moderatorExistsResponse,
});
});
await this.page.goto("/login");
}
Expand Down
64 changes: 23 additions & 41 deletions client/e2e/tests/addEditGame.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,39 @@ test.describe("Games: Add, Edit from Admin panel", () => {
await login.gotoGamesPage();
});

test("Add Games validations check", async ({ page }) => {
test("Add Games validations check", async ({ page, game }) => {
await page.click("#add-game-button");
await page.click("#save-button-add-game");

await expect(page.locator("text=Game Name must be provided")).toBeVisible();
await expect(
page.locator("text=Game Image must be a valid URL")
).toBeVisible();
await expect(
page.locator("text=Description must be provided")
).toBeVisible();

await game.gameFormValidation();
await page.click("#cancel-button-add-game");
});

test("Edit games validations check", async ({ page }) => {
test("Edit games validations check", async ({ page, game }) => {
const editButton = page.locator('[data-button="edit-game"]').first();
await editButton.click();

await page.fill('input[placeholder="Game Name"]', "");
await page.fill('input[placeholder="Game Image (URL Only)"]', "");
await page.fill('textarea[placeholder="Game description..."]', "");
await game.fillGameForm({
gameName: "",
gameDescription: "",
gameImage: "",
});

await page.click("#save-button-add-game");

await expect(page.locator("text=Game Name must be provided")).toBeVisible();
await expect(
page.locator("text=Game Image must be a valid URL")
).toBeVisible();
await expect(
page.locator("text=Description must be provided")
).toBeVisible();
await game.gameFormValidation();

await page.click("#cancel-button-add-game");
});

test("Check add new games", async ({ page }) => {
test("Check add new games", async ({ page, game }) => {
await page.click("#add-game-button");

await page.fill('input[placeholder="Game Name"]', "Lost Ark 2");
await page.fill(
'input[placeholder="Game Image (URL Only)"]',
"https://www.freetogame.com/g/517/thumbnail.jpg"
);
await page.fill(
'textarea[placeholder="Game description..."]',
"Free-to-play multiplayer massive adventure filled with lands waiting to be explored"
);
await game.fillGameForm({
gameName: "Lost Ark 2",
gameDescription:
"Free-to-play multiplayer massive adventure filled with lands waiting to be explored",
gameImage: "https://www.freetogame.com/g/517/thumbnail.jpg",
});

await page.route(API_ROUTES.CREATE_NEW_GAME, async (route) => {
await route.fulfill({
Expand All @@ -68,19 +53,16 @@ test.describe("Games: Add, Edit from Admin panel", () => {
await expect(page.locator("text=Game created successfully")).toBeVisible();
});

test("Check edit games", async ({ page }) => {
test("Check edit games", async ({ page, game }) => {
const editButton = page.locator('[data-button="edit-game"]').first();
await editButton.click();

await page.fill('input[placeholder="Game Name"]', "Lost Ark 2 Edited");
await page.fill(
'input[placeholder="Game Image (URL Only)"]',
"https://www.freetogame.com/g/517/thumbnail.jpg"
);
await page.fill(
'textarea[placeholder="Game description..."]',
"Edited Free-to-play multiplayer massive adventure filled with lands waiting to be explored"
);
await game.fillGameForm({
gameName: "Lost Ark 2 Edited",
gameDescription:
"Edited Free-to-play multiplayer massive adventure filled with lands waiting to be explored",
gameImage: "https://www.freetogame.com/g/517/thumbnail.jpg",
});

const updateApiURL = API_ROUTES.UPDATE_GAME + "/" + currentGame._id;
await page.route(updateApiURL, async (route) => {
Expand Down
16 changes: 0 additions & 16 deletions client/e2e/tests/example.spec.ts

This file was deleted.

76 changes: 13 additions & 63 deletions client/e2e/tests/gameListAndReview.spec.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,12 @@
import { test, expect } from "@playwright/test";
import { expect, test } from "../fixtures";
import { API_ROUTES } from "../support/constants";
import { gameListResponse, moderatorExistsResponse } from "../mockResponses";
import { gameListResponse } from "../mockResponses";

test.describe("Game List", () => {
test.beforeEach(async ({ page }) => {
await page.route(API_ROUTES.CHECK_MODERATOR, async (route) => {
await route.fulfill({ json: moderatorExistsResponse });
});

await page.goto("/login");

await page.route(API_ROUTES.LOGIN, async (route) => {
await route.fulfill({
json: {
token: "mockToken",
},
});
});

await page.fill('input[placeholder="Email"]', "[email protected]");
await page.fill('input[placeholder="Password"]', "password123");

await page.click("button");

await page.route(API_ROUTES.GAME_LIST, async (route) => {
await route.fulfill({ json: gameListResponse });
});

await page.goto("/");
test.beforeEach(async ({ login, game }) => {
await login.gotoLoginPage();
await login.submitLoginForm();
await game.gotoGameList();
});

test("Should display game cards with images, titles, and review counts", async ({
Expand Down Expand Up @@ -67,37 +46,12 @@ test.describe("Game Review", () => {
const currentGame = gameListResponse.data[0];
const mockLatLong = { latitude: 37.7749, longitude: -122.4194 };

test.beforeEach(async ({ page, context }) => {
await page.route(API_ROUTES.CHECK_MODERATOR, async (route) => {
await route.fulfill({ json: moderatorExistsResponse });
});

await page.goto("/login");

await page.route(API_ROUTES.LOGIN, async (route) => {
await route.fulfill({
json: {
token: "mockToken",
},
});
});

await page.fill('input[placeholder="Email"]', "[email protected]");
await page.fill('input[placeholder="Password"]', "password123");

await page.click("button");

await page.route(API_ROUTES.GAME_LIST, async (route) => {
await route.fulfill({ json: gameListResponse });
});
test.beforeEach(async ({ page, context, login, game }) => {
await login.gotoLoginPage();
await login.submitLoginForm();
await game.gotoGameList();

await page.goto("/");

const gameReviewApiUrl = `${
API_ROUTES.GAME_DETAILS + "/" + currentGame._id
}`;

await page.route(gameReviewApiUrl, async (route) => {
await page.route(game.gameReviewUrl(currentGame._id), async (route) => {
await route.fulfill({ json: { data: currentGame } });
});

Expand Down Expand Up @@ -137,7 +91,7 @@ test.describe("Game Review", () => {
);
});

test("Game Review: should submit a review", async ({ page, context }) => {
test("Game Review: should submit a review", async ({ page, game }) => {
const mockUserReview = {
text: "Nice game. Loved it.",
rating: 4,
Expand All @@ -162,12 +116,8 @@ test.describe("Game Review", () => {
});
});

const gameReviewApiUrl = `${
API_ROUTES.GAME_DETAILS + "/" + currentGame._id
}`;

// Adding the new review into the old review list
await page.route(gameReviewApiUrl, async (route) => {
await page.route(game.gameReviewUrl(currentGame._id), async (route) => {
await route.fulfill({
json: {
data: {
Expand Down
27 changes: 7 additions & 20 deletions client/e2e/tests/home.spec.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,23 @@
import { expect, test } from "@playwright/test";
import { gameListResponse } from "../mockResponses";
import { API_ROUTES } from "../support/constants";
import { expect, test } from "../fixtures";

test("Home page: Rendered successfully and show hero text", async ({
page,
game,
}) => {
await page.route(API_ROUTES.GAME_LIST, async (route) => {
await route.fulfill({ json: { data: [] } });
});

await page.goto("/");
await game.gotoGameList({ data: [] });
await expect(
page.locator("text=Find your next captivating gaming moment")
).toBeVisible();
});

test("Game List: No games found", async ({ page }) => {
await page.route(API_ROUTES.GAME_LIST, async (route) => {
await route.fulfill({ json: { data: [] } });
});

await page.goto("/");
test("Game List: No games found", async ({ page, game }) => {
await game.gotoGameList({ data: [] });
await expect(page.locator("text=No game listed")).toBeVisible();
await expect(page.locator("text=Only Moderator can list game")).toBeVisible();
});

test("Game List: Check game count greater than one", async ({ page }) => {
await page.route(API_ROUTES.GAME_LIST, async (route) => {
await route.fulfill({ json: gameListResponse });
});

await page.goto("/");
test("Game List: Check game count greater than one", async ({ page, game }) => {
await game.gotoGameList();

const gameCards = page.locator(".grid .col-span-1");
const cardCount = await gameCards.count(); // Get the number of game cards
Expand Down
Loading

0 comments on commit 6ee4a1b

Please sign in to comment.