Skip to content

Commit

Permalink
test: setup e2e tests (#1040)
Browse files Browse the repository at this point in the history
Co-authored-by: Huyen Nguyen <[email protected]>
  • Loading branch information
shootermv and huyenltnguyen authored Aug 12, 2024
1 parent fb1b523 commit c0c5839
Show file tree
Hide file tree
Showing 13 changed files with 377 additions and 6 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/playwright.yml
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
2 changes: 0 additions & 2 deletions .husky/pre-commit
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ Once you are finished making changes, you will need to run the test suite to mak
Here is terminal command for running tests: `pnpm test` (or, even shorter: `pnpm t` )
alternativetly (and also cool!) - you can install [vscode vitest extension](https://github.com/vitest-dev/vscode)

### How to run the e2e tests

This repo uses [Playwright](https://playwright.dev/) for end-to-end testing.

- To run the tests in UI mode, run:
```
pnpm run e2e:ui
```
- To run the tests in headless mode, run:

```
pnpm run e2e:ci
```

Note: e2e tests cannot be run in Gitpod environment.

### How to report bugs

Found a bug while playing?
Expand Down
37 changes: 37 additions & 0 deletions e2e/can-finish-quiz.spec.ts
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();
});
78 changes: 78 additions & 0 deletions e2e/can-run-quiz.spec.ts
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();
});
15 changes: 15 additions & 0 deletions e2e/landing-page.spec.ts
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");
});
80 changes: 80 additions & 0 deletions e2e/score-and-results.spec.ts
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();
});
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
"start:local-docker": "docker container run --rm -it -p 3000:3000 developer-quiz-site",
"build": "vite build",
"test": "vitest",
"e2e:ui": "playwright test --ui",
"e2e:ci": "playwright test",
"lint": "eslint .",
"pretty-quick": "pretty-quick",
"fix-style": "pnpm run lint --fix",
Expand All @@ -50,6 +52,7 @@
]
},
"devDependencies": {
"@playwright/test": "^1.45.3",
"@types/node": "^20.0.0",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.0.4",
Expand Down
71 changes: 71 additions & 0 deletions playwright.config.ts
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
}
});
Loading

0 comments on commit c0c5839

Please sign in to comment.