-
-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Huyen Nguyen <[email protected]>
- Loading branch information
1 parent
fb1b523
commit c0c5839
Showing
13 changed files
with
377 additions
and
6 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,24 @@ | ||
name: Playwright Tests | ||
|
||
on: | ||
workflow_dispatch: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: [main] | ||
|
||
jobs: | ||
test: | ||
timeout-minutes: 60 | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: 20.x | ||
- name: Install dependencies | ||
run: npm install -g pnpm && pnpm install | ||
- name: Install Playwright Browsers | ||
run: pnpm exec playwright install --with-deps | ||
- name: Run Playwright tests | ||
run: pnpm exec playwright 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
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,4 +1,2 @@ | ||
#!/usr/bin/env sh | ||
. "$(dirname -- "$0")/_/husky.sh" | ||
|
||
npx pretty-quick --staged |
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 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,37 @@ | ||
import { test, expect } from "@playwright/test"; | ||
|
||
test.beforeEach(async ({ page }) => { | ||
await page.goto("/#/quizzes"); | ||
|
||
await page.getByRole("button", { name: "HTML" }).click(); | ||
|
||
await page.getByRole("button", { name: "10", exact: true }).click(); | ||
}); | ||
|
||
test("should show the results after the user has answered answer all questions", async ({ | ||
page | ||
}) => { | ||
// loop through all the questions. | ||
for (let i = 1; i <= 10; i++) { | ||
expect(page.url()).toContain(`${i}/of/10`); | ||
|
||
// Select the first option (no matter if it right or worng) | ||
await page.getByRole("button").first().click(); | ||
|
||
await page.getByRole("button", { name: "Submit", exact: true }).click(); | ||
|
||
const dialog = page.getByRole("dialog"); | ||
|
||
await expect(dialog).toBeVisible(); | ||
await expect(dialog.getByText("Points:")).toBeVisible(); | ||
await expect(dialog.getByText("Your answer:")).toBeVisible(); | ||
await expect(dialog.getByText("Answer:", { exact: true })).toBeVisible(); | ||
await dialog.getByRole("button", { name: "Next Question" }).click(); | ||
} | ||
|
||
// After the 10th question, the results page shows up | ||
await page.waitForURL("/#/quizzes/HTML/results"); | ||
|
||
await expect(page.getByRole("heading", { name: "Results" })).toBeVisible(); | ||
await expect(page.getByRole("button", { name: "Play again?" })).toBeVisible(); | ||
}); |
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,78 @@ | ||
import { test, expect } from "@playwright/test"; | ||
import { CATEGORIES, QUESTION_NUMS } from "../src/constants"; | ||
|
||
test.beforeEach(async ({ page }) => { | ||
await page.goto("/#/quizzes"); | ||
}); | ||
|
||
test("should display a list of categories", async ({ page }) => { | ||
await expect( | ||
page.getByRole("heading", { name: "Choose a Category" }) | ||
).toBeVisible(); | ||
|
||
for (let i = 0; i < CATEGORIES.length; i++) { | ||
await expect( | ||
page.getByRole("button", { name: CATEGORIES[0], exact: true }) | ||
).toBeVisible(); | ||
} | ||
}); | ||
|
||
test("should allow selecting the number of questions", async ({ page }) => { | ||
await page.getByRole("button", { name: "HTML" }).click(); | ||
|
||
await page.waitForURL("#/quizzes/HTML/questionsTotal"); | ||
|
||
await expect( | ||
page.getByRole("heading", { name: "Choose a length for the Quiz" }) | ||
).toBeVisible(); | ||
|
||
for (let i = 0; i < QUESTION_NUMS.length; i++) { | ||
await expect( | ||
page.getByRole("button", { | ||
name: QUESTION_NUMS[i].toString(), | ||
exact: true | ||
}) | ||
).toBeVisible(); | ||
} | ||
|
||
await expect(page.getByRole("button", { name: /All/ })).toBeVisible(); | ||
}); | ||
|
||
test("should start the first question after the user has selected the number of questions", async ({ | ||
page | ||
}) => { | ||
await page.getByRole("button", { name: "HTML" }).click(); | ||
|
||
await page.getByRole("button", { name: "10", exact: true }).click(); | ||
|
||
await page.waitForURL("/#/quizzes/HTML/questions/1/of/10"); | ||
|
||
await expect(page.getByRole("heading", { name: "Question 1" })).toBeVisible(); | ||
}); | ||
|
||
test("question page should contain 4 options and `submit` button", async ({ | ||
page | ||
}) => { | ||
await page.getByRole("button", { name: "HTML" }).click(); | ||
|
||
await page.getByRole("button", { name: "10", exact: true }).click(); | ||
|
||
const options = page.getByRole("list"); | ||
await expect(options.getByRole("button")).toHaveCount(4); | ||
|
||
await page.getByRole("button", { name: "Submit", exact: true }).click(); | ||
}); | ||
|
||
test("should show a modal after selecting one option and click the `submit` button", async ({ | ||
page | ||
}) => { | ||
await page.getByRole("button", { name: "HTML" }).click(); | ||
|
||
await page.getByRole("button", { name: "10", exact: true }).click(); | ||
|
||
// Select the first option (no matter if it's right or worng) | ||
await page.getByRole("button").first().click(); | ||
|
||
await page.getByRole("button", { name: "Submit", exact: true }).click(); | ||
await expect(page.getByRole("dialog")).toBeVisible(); | ||
}); |
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,15 @@ | ||
import { test, expect } from "@playwright/test"; | ||
|
||
test.beforeEach(async ({ page }) => { | ||
await page.goto("/"); | ||
}); | ||
|
||
test("should render the page correctly", async ({ page }) => { | ||
await expect(page).toHaveTitle(/Developer Quiz/); | ||
|
||
const startButton = page.getByRole("link", { | ||
name: "Get started (it's free)" | ||
}); | ||
await expect(startButton).toBeVisible(); | ||
await expect(startButton).toHaveAttribute("href", "#/quizzes"); | ||
}); |
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,80 @@ | ||
import { test, expect } from "@playwright/test"; | ||
import htmlQuizQuestions from "../src/data/html-quiz"; | ||
import { | ||
correctModalResponses, | ||
incorrectModalResponses | ||
} from "../src/data/modal-responses"; | ||
|
||
test.beforeEach(async ({ page }) => { | ||
await page.goto("/#/quizzes"); | ||
}); | ||
|
||
test("should show 'success' modal after selecting the correct option", async ({ | ||
page | ||
}) => { | ||
await page.getByRole("button", { name: "HTML" }).click(); | ||
|
||
await page.getByRole("button", { name: "10", exact: true }).click(); | ||
|
||
await expect(page.getByText("Points: 0")).toBeVisible(); | ||
|
||
const question = await page.locator("legend").textContent(); | ||
// Find the question inside questions of the category | ||
const questionData = htmlQuizQuestions.find(({ Question }) => | ||
question.includes(Question) | ||
); | ||
|
||
if (!questionData) { | ||
console.log("Question not found."); | ||
} | ||
|
||
const answer = questionData.Answer; | ||
|
||
await page.getByRole("button", { name: answer }).click(); | ||
await page.getByRole("button", { name: "Submit", exact: true }).click(); | ||
|
||
const dialog = page.getByRole("dialog"); | ||
const message = await dialog.getByRole("heading", { level: 2 }).textContent(); | ||
const isMessageInExpectedSet = correctModalResponses.some(response => | ||
message.includes(response) | ||
); | ||
|
||
await expect(dialog).toBeVisible(); | ||
expect(isMessageInExpectedSet).toEqual(true); | ||
await expect(dialog.getByText("Points: 1")).toBeVisible(); | ||
}); | ||
|
||
test("should show 'failure' modal after selecting the wrong option", async ({ | ||
page | ||
}) => { | ||
await page.getByRole("button", { name: "HTML" }).click(); | ||
|
||
await page.getByRole("button", { name: "10", exact: true }).click(); | ||
|
||
await expect(page.getByText("Points: 0")).toBeVisible(); | ||
|
||
const question = await page.locator("legend").textContent(); | ||
// Find the question inside questions of the category | ||
const questionData = htmlQuizQuestions.find(({ Question }) => | ||
question.includes(Question) | ||
); | ||
|
||
if (!questionData) { | ||
console.log("Question not found."); | ||
} | ||
|
||
const distractor = questionData.Distractor1; | ||
|
||
await page.getByRole("button", { name: distractor }).click(); | ||
await page.getByRole("button", { name: "Submit", exact: true }).click(); | ||
|
||
const dialog = page.getByRole("dialog"); | ||
const message = await dialog.getByRole("heading", { level: 2 }).textContent(); | ||
const isMessageInExpectedSet = incorrectModalResponses.some(response => | ||
message.includes(response) | ||
); | ||
|
||
await expect(dialog).toBeVisible(); | ||
expect(isMessageInExpectedSet).toEqual(true); | ||
await expect(dialog.getByText("Points: 0")).toBeVisible(); | ||
}); |
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 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,71 @@ | ||
import { defineConfig, devices } from "@playwright/test"; | ||
|
||
/** | ||
* See https://playwright.dev/docs/test-configuration. | ||
*/ | ||
export default defineConfig({ | ||
testDir: "./e2e", | ||
/* Run tests in files in parallel */ | ||
fullyParallel: true, | ||
/* Fail the build on CI if you accidentally left test.only in the source code. */ | ||
forbidOnly: !!process.env.CI, | ||
/* Retry on CI only */ | ||
retries: process.env.CI ? 2 : 0, | ||
/* Opt out of parallel tests on CI. */ | ||
workers: process.env.CI ? 1 : undefined, | ||
/* Reporter to use. See https://playwright.dev/docs/test-reporters */ | ||
reporter: "html", | ||
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */ | ||
use: { | ||
/* Base URL to use in actions like `await page.goto('/')`. */ | ||
baseURL: "http://127.0.0.1:3000", | ||
|
||
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */ | ||
trace: "on-first-retry" | ||
}, | ||
|
||
/* Configure projects for major browsers */ | ||
projects: [ | ||
{ | ||
name: "chromium", | ||
use: { ...devices["Desktop Chrome"] } | ||
}, | ||
|
||
{ | ||
name: "firefox", | ||
use: { ...devices["Desktop Firefox"] } | ||
}, | ||
|
||
{ | ||
name: "webkit", | ||
use: { ...devices["Desktop Safari"] } | ||
}, | ||
|
||
/* Test against mobile viewports. */ | ||
{ | ||
name: "Mobile Chrome", | ||
use: { ...devices["Pixel 5"] } | ||
} | ||
// { | ||
// name: 'Mobile Safari', | ||
// use: { ...devices['iPhone 12'] }, | ||
// }, | ||
|
||
/* Test against branded browsers. */ | ||
// { | ||
// name: 'Microsoft Edge', | ||
// use: { ...devices['Desktop Edge'], channel: 'msedge' }, | ||
// }, | ||
// { | ||
// name: 'Google Chrome', | ||
// use: { ...devices['Desktop Chrome'], channel: 'chrome' }, | ||
// }, | ||
], | ||
|
||
/* Run your local dev server before starting the tests */ | ||
webServer: { | ||
command: "pnpm run start", | ||
url: "http://127.0.0.1:3000", | ||
reuseExistingServer: !process.env.CI | ||
} | ||
}); |
Oops, something went wrong.