Skip to content

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

Merged
merged 7 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/tutorials/TodoApp/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ node_modules/
# Don't ignore example dotenv files.
!.env.example
!.env.*.example

# Headless tests
test-results/
55 changes: 55 additions & 0 deletions examples/tutorials/TodoApp/headless-tests/playwright.config.ts
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 },
},
});
43 changes: 43 additions & 0 deletions examples/tutorials/TodoApp/headless-tests/tests/helpers.ts
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(
Copy link
Contributor

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.

Copy link
Member

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?

Copy link
Contributor Author

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.

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",
};
}
55 changes: 55 additions & 0 deletions examples/tutorials/TodoApp/headless-tests/tests/simple.spec.ts
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();
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comments are probably gpt leftovers, we can remove them :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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).

});
125 changes: 119 additions & 6 deletions examples/tutorials/TodoApp/package-lock.json

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

1 change: 1 addition & 0 deletions examples/tutorials/TodoApp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "TodoApp",
"type": "module",
"dependencies": {
"@playwright/test": "^1.51.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.26.2",
Expand Down
3 changes: 3 additions & 0 deletions examples/tutorials/TodoAppTs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ node_modules/
# Don't ignore example dotenv files.
!.env.example
!.env.*.example

# Headless tests
test-results/
Loading