diff --git a/README.md b/README.md index e10f74e..659e4b8 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,30 @@ -# Playwright Check Suites example with Checkly +# Checkly Playwright Check Suites examples -This repository demonstrates how to use Playwright natively for end-to-end testing and how to integrate these tests with Checkly for continuous monitoring. +![Use your Playwright end-to-end tests for synthetic monitoring with Playwright Check Suites.](/assets/header.jpg) + +> Take your existing Playwright Test Suite and reuse it for synthetic monitoring. ## Overview -This repository showcases: -- Native Playwright test setup with multiple browser configurations -- Authentication state management for logged-in test scenarios -- Integration with Checkly for running Playwright tests as synthetic monitors -- Test organization with tags for selective execution +This repository demonstrates how to use Playwright natively for end-to-end testing and [Checkly synthetic monitoring](https://www.checklyhq.com/product/synthetic-monitoring/). + +It showcases Playwright Check Suites that: + +- [Run Playwright tests with multiple browsers](#1-run-playwright-tests-with-multiple-browsers) +- [Monitor different environments using Playwright projects](#2-monitor-different-environments-using-playwright-projects) +- [Monitor specific application areas using Playwright tags](#3-monitor-specific-application-areas-using-playwright-tags) +- [Authenticate tests for logged-in scenarios using Playwright project dependencies](#4-authenticate-tests-for-logged-in-scenarios-using-playwright-project-dependencies) + +> [!NOTE] +> The examples focus on showing how to structure and organize your Playwright tests and reuse them for Checkly synthetic monitoring. +> +> To leverage the Playwright Check Suites' full potential mix and match the shown practices in your projects. ## Prerequisites - Node.js (v22 or higher recommended) - npm package manager -- [Checkly account](https://www.checklyhq.com/) for synthetic monitoring +- [a Checkly account](https://www.checklyhq.com/) for synthetic monitoring ## Installation @@ -25,119 +35,399 @@ npm install ## Project Structure ``` -playwright-native-example/ -├── tests/ # Test files -│ ├── home-dashboard.spec.ts # Dashboard tests -│ ├── homepage.spec.ts # Homepage tests with @sanity tag -│ ├── login.setup.ts # Authentication setup, logs into Checkly -│ ├── please.cleanup.ts # Cleanup after tests -│ ├── test-sessions.spec.ts # Test sessions page loads correctly -│ └── traces.spec.ts # Observability Traces overview page loads correctly +playwright-check-suite-examples/ +├── tests/ # Playwright Test files +│ ├── multiple-browsers/ # Example spec files +│ ├── different-environments/ # Example spec files +│ └── ... ├── checkly.config.ts # Checkly configuration ├── playwright.config.ts # Playwright configuration -├── package.json # Project dependencies -└── .env # Environment variables (add this file or create the env vars USER_NAME and PW in Checkly so that they're used) +└── package.json # Project dependencies ``` -## Configuration +## Basic Configuration + +This repository includes an initialized Playwright and [Checkly CLI project](https://www.checklyhq.com/docs/cli/overview/) to show how to use Playwright Check Suites. + +Checkly Playwright Check Suites enable you to reuse and bundle your existing Playwright end-to-end test suite for end-to-end testing and synthetic monitoring runing in the global Checkly infrastructure. ### Playwright Configuration -The `playwright.config.ts` file configures: -- **Test directory**: `./tests` -- **Projects**: Chromium, Firefox and Iphone-13 with shared authentication state -- **Authentication**: Uses storage state from `.auth/user.json` -- **Setup/Teardown**: Login before tests and cleanup after -- **Trace**: Always enabled for debugging +Every Playwright project is configured via a `playwright.config.ts` file in the root of your project. The main options to look out for are: + +- **Test directory**: the directory of your Playwright `spec.ts` files (`testDir`) +- **Projects**: projects to select, configure and run spec files with different configuration (`projects`) +- **Trace**: trace file configuration for easier failure debugging (`use.trace` or `projects[].use.trace`) + +The Checkly CLI parses and reuses your existing Playwright configuration to guarantee a smooth transition from local end-to-end testing to global synthetic monitoring with Playwright Check Suites. + +> [!TIP] +> Learn more about all test configuration options in [the official Playwright documentation](https://playwright.dev/docs/test-configuration). ### Checkly Configuration -The `checkly.config.ts` file defines: -- **Project Name**: DemoCheckSuite -- **LogicalID**: -- **Check Suites**: - 1. **Chromium Suite**: Runs every 10 minutes in 3 locations - 2. **Critical Suite**: Runs tests tagged with `@critical` every 5 minutes - 3. **Sanity Suite**: Runs Chromium tests tagged with `@sanity` every 2 minutes -- **Locations**: us-west-1, eu-west-2, ap-northeast-1 +Every Checkly CLI projects is configured via a `checkly.config.ts` file in the root of the project. Use this main configuration to define your Playwright Check Suites. + +Specify the following properties to control your Playwright setup for synthetic monitoring. + +- **Project Name**: a human readable name for your project (`Demo Playwright Check Suites`) +- **LogicalID**: a unique identifier across your Checkly account (`pwt-check-suite-examples`) +- **Playwright configuration**: your general Playwright configuration (`checks.playwrightConfigPath`) +- **Check Suite Definitions**: subsets of your Playwright test suite used as Playwright Check Suites (`checks.playwrightChecks`) + +> [!TIP] +> Learn more about all the Checkly monitoring options in [the official Checkly documentation](https://www.checklyhq.com/docs/constructs/project/). ## Running Tests -### Checkly Remote Execution +Playwright tests usually run on your local machine or in your CI/CD pipeline when you execture `npx playwright test`. The Checkly CLI lets you take your tests and run them from one of the global Checkly locations. + +### Run your Playwright tests in the Checkly infrastructure + +Use [the `pw-test` command](https://www.checklyhq.com/docs/cli/checkly-pw-test/) to run your Playwright tests in the Checkly infrastructure before deploying them as synthetic monitors. The command accepts Checkly and Playwright CLI options. + +```bash +npx checkly pw-test [checkly options] -- [playwright options] +``` Run all tests: + ```bash npx checkly pw-test ``` +Run tests from a specific location: + +```bash +npx checkly pw-test --location="eu-central-1" +``` + Run specific project: + ```bash -npx pw-test -- --project=chromium +npx checkly pw-test -- --project="chromium" ``` -Run tests with specific tag: +Run tests with a specific tag: + +```bash +npx checkly pw-test -- --grep @sanity +``` + +### Test and run your Playwright Check Suite configuration + +Once you have configured your Playwright Check Suites use [the `npx checkly test` command](https://www.checklyhq.com/docs/cli/checkly-test/) to run and test your combined Playwright and Checkly configuration. + +Run a specific Playwright Check Suite: + +```bash +npx checkly test --grep="Multiple Browser Suite" --record +``` + +Run a specific Playwright Check Suite from a specific location: + ```bash -npx pw-test -- --grep @sanity +npx checkly test --grep="Multiple Browser Suite" --record --location="eu-central-1" ``` -### Deploy your checks +## Deploy your checks -Deploy your tests as scheduled checks to Checkly: +Once your tests pass, deploy them as scheduled monitors to Checkly: ```bash npx checkly deploy ``` -## Authentication +## Examples -This project uses Playwright's authentication state feature: -1. The `login.setup.ts` file handles the initial login -2. Authentication state is saved to `.auth/user.json` -3. All subsequent tests reuse this authentication state -4. The `please.cleanup.ts` file runs after all tests for cleanup +These following examples highlight configurations to reuse, select and configure your Playwright end-to-end tests for Checkly synthetic monitoring. -## Test Tags +> [!TIP] +> Learn more about Playwright Check Suites, best practices and how to structure your tests in [the organizing Playwright Check Suites documentation](https:/www.checklyhq.com/docs/detect/synthetic-monitoring/playwright-checks/test-organization). -Tests can be tagged for selective execution: -- `@sanity`: Quick smoke tests -- `@critical`: Critical user flows +### 1. Run Playwright tests with multiple browsers + +Use the `pwProjects` option to reuse existing Playwright projects and their configuration in your Playwright Check Suite. -Example: ```typescript -test('Visit Checkly home page @sanity', async ({ page }) => { - // test implementation -}); +// playwright.config.ts + +export default defineConfig({ + projects: [ + { + name: "chromium", + testMatch: /.*\/multiple-browsers\/.*\.spec\.ts/, + use: { + ...devices["Desktop Chrome"], + }, + }, + { + name: "firefox", + testMatch: /.*\/multiple-browsers\/.*\.spec\.ts/, + use: { + ...devices["Desktop Firefox"], + }, + }, + ], +}) ``` -## Environment Variables +Reuse and configure the different Playwright projects in your `checkly.config.ts`. -Create a `.env` file in the root directory with necessary environment variables: +```typescript +// checkly.config.ts + +export default defineConfig({ + checks: { + playwrightChecks: [ + { + name: "Multiple Browser Suite", + logicalId: "browser-compat-e2e-suite", + // Specify which projects should be + // included in the Playwright Check Suite + pwProjects: ["chromium", "firefox"], + frequency: Frequency.EVERY_10M, + }, + ], + }, +}) ``` -# Add your environment variables here -# -USER_EMAIL=youruseremail -PW=yourpassword + +Use the `--grep` option to run a specific Playwright Check Suite. + +```bash +npx checkly test --grep="Multiple Browser Suite" --record ``` -## Playwright assets / Debugging: +### 2. Monitor different environments using Playwright projects -- **Traces**: Always enabled, available in all check and test runs +Configure different `baseURL` Playwright project settings if you want to run the same monitoring tests against different environments. +```typescript +// playwright.config.ts + +export default defineConfig({ + projects: [ + { + name: "environment-marketing", + testMatch: /.*\/different-environments\/.*\.spec\.ts/, + use: { ...devices["Desktop Chrome"], baseURL: "https://checklyhq.com" }, + }, + { + name: "environment-docs", + testMatch: /.*\/different-environments\/.*\.spec\.ts/, + use: { + ...devices["Desktop Chrome"], + // Change the base URL for this environment + baseURL: "https://docs.checklyhq.com", + }, + }, + ], +}) +``` -## CI/CD Integration +Reuse and configure the different Playwright projects in your `checkly.config.ts`. -The Playwright configuration includes CI-specific settings: -- Retries: 2 test-level attempts on CI and Checkly, 0 locally -- Workers: A single worker on CI and Checkly, parallel locally -- Forbid test.only: Prevents accidental exclusive tests +```typescript +// checkly.config.ts + +export default defineConfig({ + checks: { + playwrightChecks: [ + { + name: "Marketing Environment", + logicalId: "environment-marketing-suite", + pwProjects: ["environment-marketing"], + // Run this Playwright check suite in three locations every ten minutes + frequency: Frequency.EVERY_10M, + locations: ["us-west-1", "eu-west-2", "af-south-1"], + }, + { + name: "Docs Environment", + logicalId: "environment-docs-suite", + pwProjects: ["environment-docs"], + // Run this Playwright check suite in one location every hour + frequency: Frequency.EVERY_1H, + locations: ["us-west-1"], + }, + ], + }, +}) +``` +> [!TIP] +> Use this approach to monitor different development environments (`production`, `staging`, etc.) or localized environments (`your-company.com/en`, `your-company.com/de`, etc.) with the same Playwright code base. -## License +### 3. Monitor specific application areas using Playwright tags + +When you reuse your existing Playwright test suite for synthetic monitoring it is common to run only a subset of your tests as Checkly monitors. [Use Playwright tags](https://playwright.dev/docs/test-annotations#tag-tests) to specify which tests should become part of your monitoring setup. -ISC +Annotate tests with a tag signaling that they are used for end-to-end monitoring. + +```typescript +import { expect, test } from "@playwright/test" + +// Annotate a test to reuse is for synthetic monitoring +test("Visit Checkly home page", { tag: "@checkly" }, async ({ page }) => { + await page.goto("/") + + // More test code ... +}) +``` + +Once your future monitoring tests are tagged you can target them via Playwright projects or the `pwTags` Playwright Check Suite option. + +#### Run tagged tests with Playwright projects + +Configure a Playwright project that will only run tests with your specific tag. + +```typescript +// playwright.config.ts + +export default defineConfig({ + projects: [ + { + name: "checkly-monitoring", + use: { ...devices["Desktop Chrome"], baseURL: "https://checklyhq.com" }, + // Filter tests by tag + grep: /@checkly/, + }, + ], +}) +``` + +Reuse and configure the Playwright project in your `checkly.config.ts`. + +```typescript +// checkly.config.ts + +export default defineConfig({ + checks: { + playwrightChecks: [ + { + name: "Tagged Checkly Tests (tagged-via-project)", + logicalId: "tagged-tests-via-project", + // Tests are filtered by tag by using a separated Playwright project + pwProjects: ["checkly-monitoring"], + frequency: Frequency.EVERY_1H, + }, + ], + }, +}) +``` + +#### Run tagged tests with `pwTags` + +If you don't want to include the test filtering logic in your Playwright project, use `pwTags` to filter the tests in your `checkly.config.ts`. + +```typescript +// checkly.config.ts + +export default defineConfig({ + checks: { + playwrightChecks: [ + { + name: "Tagged Checkly Tests (tagged-via-pwtags)", + logicalId: "tagged-tests-via-pwtags", + // Tests are filtered by tag by using `pwTags` + pwTags: ["@checkly"], + frequency: Frequency.EVERY_1H, + }, + ], + }, +}) +``` + +> [!TIP] +> Playwright Check Suites let you filter existing tests using `pwTags`. However, we recommend to always start with a separated Playwright project and reuse it via `pwProjects` in your `checkly.config.ts`. +> +> This approach improves the maintainability and separates the Playwright test configuration (`playwright.config.ts`) from the Checkly monitoring configuration (`checkly.config.ts`). + +### 4. Authenticate tests for logged-in scenarios using Playwright project dependencies + +If your existing Playwright tests require authentication and a login step use [Playwright project dependencies and storage state](https://playwright.dev/docs/test-global-setup-teardown#option-1-project-dependencies) to log in once and reuse the browser session information. + +Create a Playwright test that performs your login actions and calls [`context().storageState()`](https://playwright.dev/docs/api/class-browsercontext#browser-context-storage-state). + +```typescript +// login.setup.ts + +const AUTH_FILE = ".auth/user.json" + +setup("Log into Checkly", async ({ page }) => { + await page.goto("/") + + // Perform your login actions + // ... + + // Use storage state to write the browser state to disk + await page.context().storageState({ path: AUTH_FILE }) +}) +``` + +Configure two new Playwright projects. The first one performs the login actions to persist the browser state, while the other one imports the browser state to avoid the login steps. + +```typescript +// playwright.config.ts + +export default defineConfig({ + projects: [ + { + name: "login-setup", + use: { ...devices["Desktop Chrome"], baseURL: "https://checklyhq.com" }, + testMatch: /.*\/storage-state-and-dependencies\/.*\.setup\.ts/, + }, + { + name: "logged-in-tests", + use: { + ...devices["Desktop Chrome"], + baseURL: "https://checklyhq.com", + // 2. Reuse the written browser state to avoid login steps + storageState: path.resolve(__dirname, AUTH_FILE), + }, + testMatch: /.*\/storage-state-and-dependencies\/.*\.spec\.ts/, + // 1. Set the project doing the login as a dependency + dependencies: ["login-setup"], + }, + ], +}) +``` + +This Playwright setup will always run the `login-setup` project before running `logged-in-tests` so that the authentication will be available and the browser state can be reused. + +Reuse the `logged-in-tests` project in your Checkly configuration. + +```typescript +// checkly.config.ts + +export default defineConfig({ + checks: { + playwrightChecks: [ + { + name: "Logged-in tests", + logicalId: "logged-in-tests", + // Run the `logged-in-tests` project which will automatically run + // `login-setup` to write the authentication file to disk + pwProjects: ["logged-in-tests"], + frequency: Frequency.EVERY_1H, + }, + ], + }, +}) +``` + +Project dependencies and storage state work the same way as your standard Playwright project. + +> [!TIP] +> Check this video to learn more about [Playwright `storageState` and how to configure Playwright project dependencies](https://www.youtube.com/watch?v=nSHPCLUwwVk). ## Resources - [Playwright Documentation](https://playwright.dev) -- [Checkly Documentation](https://www.checklyhq.com/docs) -- [Checkly Playwright Check Suites](https://www.checklyhq.com/docs/playwright-checks/) \ No newline at end of file +- [Checkly Documentation](https://www.checklyhq.com/docs/) +- [Checkly Playwright Check Suites](https://www.checklyhq.com/docs/detect/synthetic-monitoring/playwright-checks/overview/) +- [Checkly Playwright Tips on YouTube](https://www.youtube.com/playlist?list=PLMZDRUOi3a8NtMq3PUS5iJc2pee38rurc) + +## License + +MIT diff --git a/assets/header.jpg b/assets/header.jpg new file mode 100644 index 0000000..c0ddba5 Binary files /dev/null and b/assets/header.jpg differ diff --git a/checkly.config.ts b/checkly.config.ts index f6e5d72..a1ce4fc 100644 --- a/checkly.config.ts +++ b/checkly.config.ts @@ -1,42 +1,113 @@ -//checkly.config.ts -import { defineConfig } from 'checkly' -import { Frequency } from 'checkly/constructs' +// checkly.config.ts +import { defineConfig } from "checkly" +import { Frequency } from "checkly/constructs" export default defineConfig({ - projectName: 'Demo Playwright Check Suites', - logicalId: 'demo-pwCheckSuites', - repoUrl: 'https://github.com/checkly/playwright-check-suite-examples', - checks: { - playwrightConfigPath: './playwright.config.ts', - locations:['us-west-1','eu-west-2','ap-northeast-1'], + projectName: "Playwright Check Suite Examples", + logicalId: "pwt-check-suite-examples", + repoUrl: "https://github.com/checkly/playwright-check-suite-examples", + checks: { + // reuse your existing Playwright configuration + playwrightConfigPath: "./playwright.config.ts", + // define locations from which your tests will run as monitors + locations: ["us-west-1", "eu-west-2", "ap-northeast-1"], - playwrightChecks: [ - { - //Run the Chromium project with checkly configuration - name: 'Chromium Suite', - logicalId: 'chromium-e2e-suite', - pwProjects: 'chromium', // get complete set of tests run in chromium - frequency: Frequency.EVERY_10M, }, - { - name: 'Critical tagged tests in all devices', - logicalId:'critical-suite', - pwTags: 'critical', // get all tests tagged critical - frequency: 5, - }, - { - name: 'Sanity tagged tests in Chromium, only in Ireland', - logicalId:'sanity-suite', - pwProjects: 'chromium', // use chromium - pwTags: 'sanity', // get all tests tagged sanity - frequency: Frequency.EVERY_2M, - locations:['eu-west-1'], + playwrightChecks: [ + /** + * Example 1: Run Playwright tests with multiple browsers + * + * This Playwright Check Suite combines the tests defined in `chromium` + * and `firefox` project and runs them every 10 minutes. + * + * Test it and record the results with: + * $ npx checkly test --grep="Multiple Browser Suite" --record + */ + { + name: "Multiple Browser Suite", + logicalId: "browser-compat-e2e-suite", + pwProjects: ["chromium", "firefox"], + frequency: Frequency.EVERY_10M, + }, - }, - ], - }, - //also include: - cli: { - runLocation: 'eu-west-1', - retries: 0, - }, + /** + * Example 2: Monitor different environments + * with multiple Playwright Check Suites. + * + * These Playwright Check Suites monitor different environments with + * different monitoring frequencies and locations. + * + * Test them and record the results with: + * $ npx checkly test --grep="Environment" --record + */ + { + name: "Marketing Environment", + logicalId: "environment-marketing-suite", + pwProjects: ["environment-marketing"], + frequency: Frequency.EVERY_10M, + locations: ["us-west-1", "eu-west-2", "af-south-1"], + }, + { + name: "Docs Environment", + logicalId: "environment-docs-suite", + pwProjects: ["environment-docs"], + frequency: Frequency.EVERY_1H, + locations: ["us-west-1"], + }, + + /** + * Example 3: Monitor different application areas using Playwright tags + */ + + /** + * Example 3a: Monitor different application areas using Playwright projects + * + * Test them and record the results with: + * $ npx checkly test --grep="tagged-via-project" --record + */ + { + name: "Tagged Checkly Tests (tagged-via-project)", + logicalId: "tagged-tests-via-project", + // Tests are filtered by tag by using a separated Playwright project + pwProjects: ["checkly-monitoring"], + frequency: Frequency.EVERY_1H, + }, + /** + * Example 3b: Monitor different application areas using `pwTags` + * + * Test them and record the results with: + * $ npx checkly test --grep="tagged-via-pwtags" --record + */ + { + name: "Tagged Checkly Tests (tagged-via-pwtags)", + logicalId: "tagged-tests-via-pwtags", + // Tests are filtered by tag by using `pwTags` + pwTags: ["@checkly"], + frequency: Frequency.EVERY_1H, + }, + + /** + * Example 4: Authenticate tests for logged-in scenarios + * using Playwright project dependencies + * + * This Playwright Check Suite will run the setup step defined in the + * `playwright.config.ts` first, log in and write storage data to disk. + * Then the actual Playwright project will be run and reuse the browser + * session information. + * + * Test them and record the results with: + * $ npx checkly test --grep="Logged-in tests" --record + */ + { + name: "Logged-in tests", + logicalId: "logged-in-tests", + pwProjects: ["logged-in-tests"], + frequency: Frequency.EVERY_1H, + }, + ], + }, + //also include: + cli: { + runLocation: "eu-west-1", + retries: 0, + }, }) diff --git a/playwright.config.ts b/playwright.config.ts index 44e0c3e..d6f6931 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -1,13 +1,14 @@ import { defineConfig, devices } from "@playwright/test" -const iphone13 = devices['iPhone 13']; /** * Read environment variables from file. * https://github.com/motdotla/dotenv */ -import dotenv from 'dotenv'; -import path from 'path'; -dotenv.config({ path: path.resolve(__dirname, '.env') }); +import dotenv from "dotenv" +import path from "path" +dotenv.config({ path: path.resolve(__dirname, ".env") }) + +export const AUTH_FILE = ".auth/user.json" /** * See https://playwright.dev/docs/test-configuration. @@ -23,60 +24,82 @@ export default defineConfig({ /* Opt out of parallel tests on CI. */ workers: process.env.CI ? 1 : undefined, /* Reporter to use. See https://playwright.dev/docs/test-reporters */ - reporter: [ - ['list'], - ['html', { open: 'never', outputFolder: path.resolve(__dirname, 'test-results/html') }], - // ['monocart-reporter', { - // name: 'My Test Report', - // outputFile: path.resolve(__dirname, 'test-results/monocart/index.html'), - // outputFolder: path.resolve(__dirname, 'test-results/monocart') - // }] - ], + reporter: [["list"], ["html"]], /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */ use: { /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */ trace: "on", viewport: { width: 1280, - height: 720 - } + height: 720, + }, + baseURL: "https://www.checklyhq.com", }, - /* Configure projects*/ + /** + * Configure projects + * + * More info in github.com/checkly/playwright-check-suite-examples/#examples + */ projects: [ + /** + * Example 1 + * Running multiple browsers + */ { - name: "setup", - use: { ...devices["Desktop Chrome"] }, - testMatch: /.*\.setup\.ts/, - teardown: 'cleanup', + name: "chromium", + testMatch: /.*\/multiple-browsers\/.*\.spec\.ts/, + use: { + ...devices["Desktop Chrome"], + }, }, { - name: 'cleanup', - // This might be multiple files that end with .teardown.ts - testMatch: '**/*.cleanup.ts', + name: "firefox", + testMatch: /.*\/multiple-browsers\/.*\.spec\.ts/, + use: { + ...devices["Desktop Firefox"], + }, }, + /** + * Example 2 + */ { - name: 'chromium', + name: "environment-marketing", + testMatch: /.*\/different-environments\/.*\.spec\.ts/, + use: { ...devices["Desktop Chrome"], baseURL: "https://checklyhq.com" }, + }, + { + name: "environment-docs", + testMatch: /.*\/different-environments\/.*\.spec\.ts/, use: { ...devices["Desktop Chrome"], - storageState: ".auth/user.json", + baseURL: "https://docs.checklyhq.com", }, - dependencies: ["setup"], }, - { - name: 'firefox', - use: { - ...devices["Desktop Firefox"], - storageState: ".auth/user.json", - }, - dependencies: ["setup"], + /** + * Example 3 + */ + { + name: "checkly-monitoring", + use: { ...devices["Desktop Chrome"], baseURL: "https://checklyhq.com" }, + grep: /@checkly/, }, - { - name: 'iphone-14', + /** + * Example 4 + */ + { + name: "login-setup", + use: { ...devices["Desktop Chrome"], baseURL: "https://checklyhq.com" }, + testMatch: /.*\/storage-state-and-dependencies\/.*\.setup\.ts/, + }, + { + name: "logged-in-tests", use: { - ...devices["iPhone 14"], - storageState: ".auth/user.json", + ...devices["Desktop Chrome"], + baseURL: "https://checklyhq.com", + storageState: path.resolve(__dirname, AUTH_FILE), }, - dependencies: ["setup"], + testMatch: /.*\/storage-state-and-dependencies\/.*\.spec\.ts/, + dependencies: ["login-setup"], }, ], diff --git a/tests/different-environments/homepage.spec.ts b/tests/different-environments/homepage.spec.ts new file mode 100644 index 0000000..3dfcfac --- /dev/null +++ b/tests/different-environments/homepage.spec.ts @@ -0,0 +1,9 @@ +import { expect, test } from "@playwright/test" + +test("Visit Checkly home page", async ({ page }) => { + await page.goto("/") + + await expect(page).toHaveTitle(/Checkly/) + + // More test code ... +}) diff --git a/tests/home-dashboard.spec.ts b/tests/home-dashboard.spec.ts deleted file mode 100644 index 2ab956e..0000000 --- a/tests/home-dashboard.spec.ts +++ /dev/null @@ -1,9 +0,0 @@ - -import { expect, test } from "@playwright/test" - -test("home dashboard loads @critical", async ({ page }) => { - await page.goto("https://app.checklyhq.com") - await expect(page.getByTestId("home-dashboard-table")).toBeVisible() - // more test instructions - // ... -}) \ No newline at end of file diff --git a/tests/homepage.spec.ts b/tests/homepage.spec.ts deleted file mode 100644 index c3efe8b..0000000 --- a/tests/homepage.spec.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { expect, test } from '@playwright/test'; - -test('Visit Checkly home page @sanity', async ({ page }) => { - const response = await page.goto('https://checklyhq.com'); - - expect(response).not.toBeNull(); - - if (response === null) { - return; - } - - expect(response.status()).toBeLessThan(400); - }); \ No newline at end of file diff --git a/tests/login.setup.ts b/tests/login.setup.ts deleted file mode 100644 index 3717daa..0000000 --- a/tests/login.setup.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { expect, test as setup } from "@playwright/test" - -const AUTH_FILE = ".auth/user.json" - -setup("Log into Checkly", async ({ page }) => { - console.log(process.env.USER_NAME); - await page.goto("https://app.checklyhq.com") - await page.getByRole('textbox', { name: 'Email address' }).click(); - await page.getByRole('textbox', { name: 'Email address' }).fill(process.env.USER_EMAIL as string); - await page.getByRole('button', { name: 'Log in' }).click(); - await page.getByRole('textbox', { name: 'Password' }).click(); - await page.getByRole('textbox', { name: 'Password' }).fill(process.env.PW as string); - await page.getByRole('button', { name: 'Continue' }).click(); - await expect(page.getByRole('button', { name: /switch accounts/i })).toBeVisible() - await page.context().storageState({ path: AUTH_FILE }) -}); \ No newline at end of file diff --git a/tests/multiple-browsers/homepage.spec.ts b/tests/multiple-browsers/homepage.spec.ts new file mode 100644 index 0000000..3dfcfac --- /dev/null +++ b/tests/multiple-browsers/homepage.spec.ts @@ -0,0 +1,9 @@ +import { expect, test } from "@playwright/test" + +test("Visit Checkly home page", async ({ page }) => { + await page.goto("/") + + await expect(page).toHaveTitle(/Checkly/) + + // More test code ... +}) diff --git a/tests/please.cleanup.ts b/tests/please.cleanup.ts deleted file mode 100644 index 8d8d761..0000000 --- a/tests/please.cleanup.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { expect, test } from '@playwright/test'; - -test('please clean up', async ({ page }) => { - console.log('just a cleanup demo') - }); \ No newline at end of file diff --git a/tests/storage-state-and-dependencies/homepage.spec.ts b/tests/storage-state-and-dependencies/homepage.spec.ts new file mode 100644 index 0000000..ce36b76 --- /dev/null +++ b/tests/storage-state-and-dependencies/homepage.spec.ts @@ -0,0 +1,7 @@ +import { test } from "@playwright/test" + +test("Visit logged in area", async ({ page }) => { + await page.goto("/") + + // More test code ... +}) diff --git a/tests/storage-state-and-dependencies/login.setup.ts b/tests/storage-state-and-dependencies/login.setup.ts new file mode 100644 index 0000000..3a53ae7 --- /dev/null +++ b/tests/storage-state-and-dependencies/login.setup.ts @@ -0,0 +1,15 @@ +import { test as setup } from "@playwright/test" + +const AUTH_FILE = ".auth/user.json" + +setup("Log into Checkly", async ({ page }) => { + await page.goto("/") + + // Perform your login actions which will set cookies and localstorage entries + // ... + + // Use storage state to write the browser state to disk + // More info: https://playwright.dev/docs/api/class-browsercontext#browser-context-storage-state + await page.context().storageState({ path: AUTH_FILE }) + console.log(`Wrote storage state to ${AUTH_FILE}`) +}) diff --git a/tests/test-sessions.spec.ts b/tests/test-sessions.spec.ts deleted file mode 100644 index 8d6f697..0000000 --- a/tests/test-sessions.spec.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { expect, test } from "@playwright/test" - -test('Test Sessions page loads', async ({ page }) => { - const response = await page.goto('https://app.checklyhq.com/test-sessions'); - - expect(response).not.toBeNull(); - - if (response === null) { - return; - } -}); \ No newline at end of file diff --git a/tests/traces.spec.ts b/tests/traces.spec.ts deleted file mode 100644 index 02b7ca2..0000000 --- a/tests/traces.spec.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { expect, test } from "@playwright/test" - -test('Visit Traces page', async ({ page }) => { - const response = await page.goto('https://app.checklyhq.com/traces'); - - expect(response).not.toBeNull(); - - if (response === null) { - return; - } - - expect(response.status()).toBeLessThan(400); -}); \ No newline at end of file diff --git a/tests/using-tags/homepage.spec.ts b/tests/using-tags/homepage.spec.ts new file mode 100644 index 0000000..0f11707 --- /dev/null +++ b/tests/using-tags/homepage.spec.ts @@ -0,0 +1,9 @@ +import { expect, test } from "@playwright/test" + +test("Visit Checkly home page", { tag: "@checkly" }, async ({ page }) => { + await page.goto("/") + + await expect(page).toHaveTitle(/Checkly/) + + // More test code ... +})