Skip to content

Commit

Permalink
Merge pull request #456 from Open-Earth-Foundation/feat/playwright
Browse files Browse the repository at this point in the history
feat(ci): install Playwright for e2e tests and create e2e test for signup
  • Loading branch information
lemilonkh committed Apr 24, 2024
2 parents 3e6d219 + c236ac1 commit 01d2a9a
Show file tree
Hide file tree
Showing 12 changed files with 733 additions and 78 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/web-develop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ jobs:
- name: Run tests and generate coverage file
run: npm run ci:test

- name: Install Playwright Browsers
run: npx playwright install --with-deps
- name: Run Playwright tests
run: npx playwright test
- uses: actions/upload-artifact@v4
if: always()
with:
name: playwright-report
path: playwright-report/
retention-days: 30

- name: Upload coverage reports to Codecov
continue-on-error: true
uses: codecov/codecov-action@v4.0.1
Expand Down
11 changes: 11 additions & 0 deletions .github/workflows/web-tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ jobs:
- name: Run API tests
run: npm run api:test

- name: Install Playwright Browsers
run: npx playwright install --with-deps
- name: Run Playwright tests
run: npx playwright test
- uses: actions/upload-artifact@v4
if: always()
with:
name: playwright-report
path: playwright-report/
retention-days: 30

- name: Shut down database
run: docker stop github_action_postgresql

Expand Down
25 changes: 10 additions & 15 deletions .github/workflows/web-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,19 @@ jobs:
- name: Run NextJS build
run: npm run build

# - name: Run Cypress tests
# run: npm run cy:test

# ... Generate LCOV files or download it from a different job
- name: Run tests and generate coverage file
run: npm run ci:test

# - name: Setup LCOV
# uses: hrishikesh-kadam/setup-lcov@v1
# - name: Report code coverage
# uses: zgosalvez/github-actions-report-lcov@v3
# with:
# coverage-files: ./app/lcov*.info
# minimum-coverage: 40
# artifact-name: code-coverage-report
# github-token: ${{ secrets.GITHUB_TOKEN }}
# working-directory: ./app
# update-comment: true
- name: Install Playwright Browsers
run: npx playwright install --with-deps
- name: Run Playwright tests
run: npx playwright test
- uses: actions/upload-artifact@v4
if: always()
with:
name: playwright-report
path: playwright-report/
retention-days: 30

- name: Upload coverage reports to Codecov
continue-on-error: true
Expand Down
5 changes: 4 additions & 1 deletion app/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts

/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
12 changes: 12 additions & 0 deletions app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,15 @@ Then, you can run the app in a container:
```bash
docker run -p 3000:3000 ghcr.io/open-earth-foundation/citycatalyst
```

### End to end testing

We use Playwright to run automated E2E tests.

Setup: `npx playwright install --with-deps`

Run: `npm run e2e:test`

### API unit tests

Run: `npm run api:test`
58 changes: 0 additions & 58 deletions app/cypress/e2e/signup.cy.ts

This file was deleted.

90 changes: 90 additions & 0 deletions app/e2e/signup.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { test, expect, type Page } from "@playwright/test";
import { randomUUID } from "node:crypto";

async function expectText(page: Page, text: string) {
await expect(page.getByText(text).first()).toBeVisible();
}

test.beforeEach(async ({ page }) => {
await page.goto("/en/auth/signup");
});

test.describe("Signup", () => {
test("should navigate to signup from login", async ({ page }) => {
await page.goto("/");
const link = page.getByText("Sign up");
await expect(link).toBeVisible();
await link.click();
await expect(
page.getByRole("heading", { name: "Sign Up to City Catalyst" }),
).toBeVisible();
});

test("should redirect to dashboard after entering correct data", async ({
page,
}) => {
await expect(
page.getByRole("heading", { name: "Sign Up to City Catalyst" }),
).toBeVisible();

const email = `e2e-test+${randomUUID()}@example.com`;

await page.getByPlaceholder("Your full name").fill("Test User");
await page.getByPlaceholder("e.g. youremail@domain.com").fill(email);
await page.getByLabel("Password", { exact: true }).fill("Test123");
await page.getByLabel("Confirm Password").fill("Test123");
await page.getByPlaceholder("Enter the code you received").fill("123456");
await page
.locator('input[name="acceptTerms"] + .chakra-checkbox__control')
.click();
await page.getByRole("button", { name: "Create Account" }).click();

await expect(page).toHaveURL(
`/en/auth/check-email?email=${email.replace("@", "%40")}`,
);
});

test("should show errors when entering invalid data", async ({ page }) => {
await expect(
page.getByRole("heading", { name: "Sign Up to City Catalyst" }),
).toBeVisible();

await page.getByPlaceholder("Your full name").fill("asd");
await page
.getByPlaceholder("e.g. youremail@domain.com")
.fill("testopenearthorg");
await page.getByLabel("Password", { exact: true }).fill("Pas");
await page.getByLabel("Confirm Password").fill("Pa1");
await page.getByPlaceholder("Enter the code you received").fill("12345");
await page.getByRole("button", { name: "Create Account" }).click();

await expect(page).toHaveURL(`/en/auth/signup`);
await expectText(page, "valid email address");
await expectText(page, "Minimum length");
await expectText(page, "Invalid invite code");
await expectText(page, "Please accept the terms");
});

test("should require matching passwords", async ({ page }) => {
await expect(
page.getByRole("heading", { name: "Sign Up to City Catalyst" }),
).toBeVisible();

await page.getByPlaceholder("Your full name").fill("Test Account");
await page
.getByPlaceholder("e.g. youremail@domain.com")
.fill("e2e-test-fail@example.com");
await page.getByLabel("Password", { exact: true }).fill("Password1");
await page.getByLabel("Confirm Password").fill("Password2");
await page.getByPlaceholder("Enter the code you received").fill("123456");
await page
.locator('input[name="acceptTerms"] + .chakra-checkbox__control') // sibling
.click();
await page.getByRole("button", { name: "Create Account" }).click();

await expect(page).toHaveURL(`/en/auth/signup`);
await expectText(page, "Passwords don't match");
});

test.skip("should correctly handle and pass callbackUrl", () => {});
});
60 changes: 60 additions & 0 deletions app/package-lock.json

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

8 changes: 4 additions & 4 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
"build": "next build",
"start": "next start",
"lint": "next lint",
"test": "npm run api:test & npm run cy:test",
"test": "npm run api:test & npm run e2e:test",
"api:test": "glob -c \"tsx --no-warnings --test\" \"./tests/**/*.test.ts\"",
"e2e:test": "npx playwright test",
"e2e:test:head": "npx playwright test -- --headed",
"test-single": "tsx --no-warnings --test",
"storybook": "storybook dev -p 6006",
"build-storybook": "storybook build",
Expand All @@ -22,9 +24,6 @@
"db:gen-seed": "sequelize-cli seed:generate --name",
"sync-catalogue": "tsx scripts/catalogue-sync.ts",
"ci:test": "tsx --test --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=lcov.info tests/**/*.test.ts",
"cy:open": "cypress open",
"cy:run": "cypress run",
"cy:test": "start-server-and-test start http://localhost:3000/en/auth/login cy:run",
"prettier": "npx prettier . --write",
"email": "email dev --dir src/lib/emails",
"create-admin": "tsx scripts/create-admin.ts"
Expand Down Expand Up @@ -99,6 +98,7 @@
"zod": "^3.22.4"
},
"devDependencies": {
"@playwright/test": "^1.43.1",
"@storybook/addon-essentials": "^7.6.16",
"@storybook/addon-interactions": "^7.4.5",
"@storybook/addon-links": "^7.6.17",
Expand Down
Loading

0 comments on commit 01d2a9a

Please sign in to comment.