Skip to content

Commit

Permalink
Add stub Playwright test and URL apiKey support
Browse files Browse the repository at this point in the history
  • Loading branch information
bvaughn committed Mar 23, 2024
1 parent 05885c6 commit a73652c
Show file tree
Hide file tree
Showing 10 changed files with 245 additions and 7 deletions.
5 changes: 4 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ AUTH0_CLIENT_ID=
AUTH0_CLIENT_SECRET=

# Stripe
STRIPE_KEY=
STRIPE_KEY=

# e2e test keys
TEST_USER_API_KEY=
27 changes: 27 additions & 0 deletions .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Playwright Tests
on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]
jobs:
test:
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: lts/*
- 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
- uses: actions/upload-artifact@v4
if: always()
with:
name: playwright-report
path: playwright-report/
retention-days: 30
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/blob-report/
/playwright/.cache/
/playwright-report/
/test-results/
.DS_Store
.env.local
.next
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"lint": "next lint",
"start": "next start",
"test": "jest",
"test:e2e": "playwright test",
"typescript": "tsc --noEmit",
"typescript:watch": "tsc --noEmit --watch"
},
Expand All @@ -20,6 +21,7 @@
"@replayio/overboard": "^0.4.1",
"@stripe/react-stripe-js": "^2.6.2",
"@stripe/stripe-js": "^3.0.10",
"chalk": "^4",
"date-fns": "^3.3.1",
"graphql": "^16.8.1",
"lodash": "^4.17.21",
Expand All @@ -36,6 +38,7 @@
"@graphql-codegen/client-preset": "4.2.4",
"@graphql-typed-document-node/core": "^3.2.0",
"@jest/globals": "^29.7.0",
"@playwright/test": "^1.42.1",
"@testing-library/jest-dom": "^6.4.1",
"@testing-library/react": "^14.2.1",
"@types/jest": "^29.5.12",
Expand All @@ -45,14 +48,16 @@
"@types/react-dom": "^18.2.21",
"apollo": "^2.34.0",
"autoprefixer": "^10.4.17",
"dotenv": "^16.4.4",
"eslint": "^8",
"eslint-config-next": "14.1.0",
"graphqurl": "^1.0.1",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"postcss": "^8.4.33",
"strip-ansi": "^6",
"tailwindcss": "^3.4.1",
"ts-jest": "^29.1.2",
"typescript": "^5.4.2"
}
}
}
78 changes: 78 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { resolve } from "path";
import { defineConfig, devices } from "@playwright/test";

/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
require("dotenv").config({ path: resolve(__dirname, ".env.local") });

/**
* 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: "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: 'npm run start',
// url: 'http://127.0.0.1:3000',
// reuseExistingServer: !process.env.CI,
// },
});
49 changes: 44 additions & 5 deletions pnpm-lock.yaml

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

11 changes: 11 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ export async function middleware(request: NextRequest) {

// Require authentication for protected routes
if (pathname.startsWith("/org") || pathname.startsWith("/team")) {
// e2e tests
const url = new URL(nextUrl);
if (url.searchParams.has("e2e")) {
const apiKey = url.searchParams.get("apiKey");
if (apiKey) {
response.headers.set(HEADERS.accessToken, apiKey);

return response;
}
}

const session = await getSession(request, response);
if (!session) {
const loginUrl = new URL("/api/auth/login", request.url);
Expand Down
44 changes: 44 additions & 0 deletions tests/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Page } from "@playwright/test";
import chalk from "chalk";
import stripAnsi from "strip-ansi";

export async function debugPrint(
page: Page | null,
message: string,
scope?: string
) {
const formattedScope = scope ? `(${scope})` : "";
console.log(message, formattedScope ? chalk.dim(formattedScope) : "");

if (page !== null) {
// Strip out all ANSI escape codes when we log this inside the browser.
// Otherwise, the console messages in the recording are hard to read.
const plainMessage = stripAnsi(message);
await page.evaluate(
({ message, scope }) => {
console.log(`${message} ${scope}`);
},
{ message: plainMessage, scope: formattedScope }
);
}
}

export async function startTest({
apiKey = process.env.TEST_USER_API_KEY,
isTestWorkspace,
page,
workspaceId = "me",
}: {
apiKey?: string;
isTestWorkspace?: boolean;
page: Page;
workspaceId?: string;
}) {
const host = process.env.PLAYWRIGHT_TEST_BASE_URL || "http://localhost:8080";
const url = `${host}/team/${workspaceId}/${
isTestWorkspace ? "runs" : "recordings"
}?e2e=1&apiKey=${apiKey}`;

await debugPrint(page, `Navigating to ${chalk.bold(url)}`, "startTestView");
await page.goto(url);
}
13 changes: 13 additions & 0 deletions tests/login.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { expect, test } from "@playwright/test";
import { startTest } from "./helpers";

test("should require sign-in", async ({ page }) => {
await startTest({
apiKey: "",
page,
});

await expect(
page.getByText("Log in to Replay to continue to Replay.")
).toBeVisible();
});
14 changes: 14 additions & 0 deletions tests/my-library.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { test, expect } from "@playwright/test";
import { startTest } from "./helpers";

test("should require sign-in", async ({ page }) => {
await startTest({
page,
});

await expect(
page.getByRole("button", {
name: "Launch Replay",
})
).toBeVisible();
});

0 comments on commit a73652c

Please sign in to comment.