-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Adds headless tests for example apps #2478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
ab1d91a
a8ea42c
af79320
81f09f8
9dc07b4
5acf4e4
7378cc1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,6 @@ node_modules/ | |
# Don't ignore example dotenv files. | ||
!.env.example | ||
!.env.*.example | ||
|
||
# Headless tests | ||
test-results/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { defineConfig, devices } from "@playwright/test"; | ||
|
||
/** | ||
* Read environment variables from file. | ||
* https://github.com/motdotla/dotenv | ||
*/ | ||
// require('dotenv').config(); | ||
|
||
/** | ||
* See https://playwright.dev/docs/test-configuration. | ||
*/ | ||
export default defineConfig({ | ||
testDir: "./tests", | ||
/* 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: process.env.CI ? "dot" : "list", | ||
/* 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://localhost:3000", | ||
|
||
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */ | ||
trace: "on-first-retry", | ||
}, | ||
|
||
projects: [ | ||
{ | ||
name: "chromium", | ||
use: { ...devices["Desktop Chrome"] }, | ||
}, | ||
/* Test against mobile viewports. */ | ||
{ | ||
name: "Mobile Chrome", | ||
use: { ...devices["Pixel 5"] }, | ||
}, | ||
], | ||
|
||
/* Run your local dev server before starting the tests */ | ||
webServer: { | ||
command: "run-wasp-app dev --path-to-app=../", | ||
|
||
// Wait for the backend to start | ||
url: "http://localhost:3001", | ||
reuseExistingServer: !process.env.CI, | ||
timeout: 180 * 1000, | ||
gracefulShutdown: { signal: "SIGTERM", timeout: 500 }, | ||
}, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import type { Page } from "@playwright/test"; | ||
|
||
export async function performSignup( | ||
page: Page, | ||
{ username, password }: { username: string; password: string } | ||
) { | ||
await page.goto("/signup"); | ||
|
||
await page.waitForSelector("text=Create a new account"); | ||
|
||
await page.locator("input[name='username']").fill(username); | ||
await page.locator("input[type='password']").fill(password); | ||
await page.locator("button").click(); | ||
} | ||
|
||
export async function performLogin( | ||
page: Page, | ||
{ | ||
username, | ||
password, | ||
}: { | ||
username: string; | ||
password: string; | ||
} | ||
) { | ||
await page.goto("/login"); | ||
|
||
await page.waitForSelector("text=Log in to your account"); | ||
|
||
await page.locator("input[name='username']").fill(username); | ||
await page.locator("input[type='password']").fill(password); | ||
await page.getByRole("button", { name: "Log in" }).click(); | ||
} | ||
|
||
export function generateRandomCredentials(): { | ||
username: string; | ||
password: string; | ||
} { | ||
return { | ||
username: `test${Math.random().toString(36).substring(7)}`, | ||
password: "12345678", | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { test, expect } from "@playwright/test"; | ||
import { | ||
generateRandomCredentials, | ||
performLogin, | ||
performSignup, | ||
} from "./helpers"; | ||
|
||
test.describe("auth and work tasks", () => { | ||
const { username, password } = generateRandomCredentials(); | ||
|
||
test.describe.configure({ mode: "serial" }); | ||
|
||
test("can sign up", async ({ page }) => { | ||
await performSignup(page, { | ||
username, | ||
password, | ||
}); | ||
|
||
await expect(page).toHaveURL("/"); | ||
|
||
await page.getByText("Logout").click(); | ||
|
||
await expect(page).toHaveURL("/login"); | ||
}); | ||
|
||
test("can log in and interact with tasks", async ({ page }) => { | ||
await performLogin(page, { | ||
username, | ||
password: "12345678xxx", | ||
}); | ||
|
||
await expect(page.locator("body")).toContainText("Invalid credentials"); | ||
|
||
await performLogin(page, { | ||
username, | ||
password, | ||
}); | ||
|
||
await expect(page).toHaveURL("/"); | ||
|
||
const randomTask = `New Task ${Math.random().toString(36).substring(7)}`; | ||
// Fill input[name="description"] with random task | ||
await page.locator("input[name='description']").fill(randomTask); | ||
// Click input[type="submit"] to submit the form | ||
await page.locator("input[type='submit']").click(); | ||
// Expect to see the task on the page | ||
await expect(page.locator("body")).toContainText(randomTask); | ||
// Check the task as done input[type="checkbox"] | ||
await page.locator("input[type='checkbox']").click(); | ||
// Reload the page | ||
await page.reload(); | ||
// Expect the task to be checked | ||
await expect(page.locator("input[type='checkbox']")).toBeChecked(); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comments are probably gpt leftovers, we can remove them :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, probably me telling Copilot what to write (or I generated this with Playwright's extension, not sure). |
||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,6 @@ node_modules/ | |
# Don't ignore example dotenv files. | ||
!.env.example | ||
!.env.*.example | ||
|
||
# Headless tests | ||
test-results/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could probably skip on testing the signup and the login in all apps. It's a lot of repetitive code that brings little benefit I'd say.
However, if and where we test for it (e.g., in
waspc/todoApp
), we should also test for unsuccessful logins and signups.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jumping in a bit, but I see this is in helper.ts and I guess login is just needed to test the rest of the app, so probably no point in trying to do less of it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, testing login and signup is something we do along the way because we need to authenticate anyways to test the apps.