-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
all test file refactor with custom fixture
- Loading branch information
Showing
10 changed files
with
150 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
|
@@ -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"); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ({ | ||
|
@@ -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 } }); | ||
}); | ||
|
||
|
@@ -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, | ||
|
@@ -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: { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.