|
| 1 | +--- |
| 2 | +title: "How to Integrate Argos for Visual Testing with Vercel Preview: A Comprehensive Guide" |
| 3 | +description: "Learn how to set up Argos visual testing integration on Vercel Preview deployments using GitHub Actions, enhancing your CI/CD pipeline for better visual regression detection." |
| 4 | +category: Developer Experience |
| 5 | +slug: run-argos-on-vercel-preview |
| 6 | +author: Greg Bergé |
| 7 | +date: 2024-04-01 |
| 8 | +image: ./main.jpg |
| 9 | +imageAlt: Vercel covered with Argos |
| 10 | +--- |
| 11 | + |
| 12 | +Argos now offers a smooth integration with Vercel Preview for visual testing, enabling users to effectively identify visual regressions in an environment that closely simulates production. This guide provides a detailed walkthrough on configuring Argos on Vercel Preview through GitHub Actions, including shared source code for straightforward replication. |
| 13 | + |
| 14 | +<MainImage /> |
| 15 | + |
| 16 | +## The Benefits of Visual Testing with Vercel Preview |
| 17 | + |
| 18 | +-**Cost Efficiency in CI**: Eliminate the need for duplicate application builds, saving time and resources. |
| 19 | + |
| 20 | +- **Enhanced Test Reliability**: Run visual tests in an environment that mirrors production, ensuring consistency and accuracy. |
| 21 | + |
| 22 | +**Important Note:** This strategy is most effective for idempotent tests that can be executed repeatedly without altering the environment. Tests involving significant modifications, like user deletions, might not be compatible. |
| 23 | + |
| 24 | +## Prerequisites |
| 25 | + |
| 26 | +1. Setup [Playright](https://playwright.dev/) on your project. For a Next.js application, follow [this guide](https://nextjs.org/docs/pages/building-your-application/testing/playwright). |
| 27 | +2. Deploy your project on [Vercel](https://vercel.com/). |
| 28 | + |
| 29 | +## Setup Argos in my project |
| 30 | + |
| 31 | +### 1. Import your project in Argos |
| 32 | + |
| 33 | +[Sign up to Argos](https://app.argos-ci.com/signup) and import your project. Save the `ARGOS_TOKEN` for later. |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +### 2. Install Argos Playwright SDK |
| 38 | + |
| 39 | +```sh |
| 40 | +npm install --save-dev @argos-ci/playwright |
| 41 | +``` |
| 42 | + |
| 43 | +### 3. Update your Playwright config |
| 44 | + |
| 45 | +Argos integrates with Playwright using a custom reporter. To upload your screenshots to Argos when Playwright tests run, add `@argos-ci/playwright/reporter` in your `playwright.config.ts` file: |
| 46 | + |
| 47 | +```ts |
| 48 | +import { defineConfig } from "@playwright/test"; |
| 49 | + |
| 50 | +export default defineConfig({ |
| 51 | + // ... other configuration |
| 52 | + |
| 53 | + reporter: [ |
| 54 | + // Use "dot" reporter on CI, "list" otherwise (Playwright default). |
| 55 | + process.env.CI ? ["dot"] : ["list"], |
| 56 | + // Add Argos reporter. |
| 57 | + [ |
| 58 | + "@argos-ci/playwright/reporter", |
| 59 | + { |
| 60 | + // Upload to Argos on CI only. |
| 61 | + uploadToArgos: !!process.env.CI, |
| 62 | + |
| 63 | + // Set your Argos token. |
| 64 | + token: "<YOUR-ARGOS-TOKEN>", |
| 65 | + }, |
| 66 | + ], |
| 67 | + ], |
| 68 | + |
| 69 | + use: { |
| 70 | + // On CI, we will set `BASE_URL` from Vercel preview URL |
| 71 | + baseURL: process.env.CI ? process.env.BASE_URL : "http://localhost:3000", |
| 72 | + }, |
| 73 | + |
| 74 | + extraHTTPHeaders: { |
| 75 | + // Hide Vercel Toolbar in tests |
| 76 | + "x-vercel-skip-toolbar": "0", |
| 77 | + }, |
| 78 | +}); |
| 79 | +``` |
| 80 | + |
| 81 | +### 4. Take screenshots of your app |
| 82 | + |
| 83 | +The [Screenshot pages script](https://argos-ci.com/docs/screenshot-pages-script) from Argos's documentation was utilized for capturing the application's homepage, offering flexibility to include additional pages in the future. |
| 84 | + |
| 85 | +```ts |
| 86 | +import { argosScreenshot } from "@argos-ci/playwright"; |
| 87 | +import { test } from "@playwright/test"; |
| 88 | + |
| 89 | +const pages = [ |
| 90 | + { name: "homepage", path: "/" }, |
| 91 | + // Add other pages if needed |
| 92 | +]; |
| 93 | + |
| 94 | +for (const { name, path } of pages) { |
| 95 | + test(`run Argos on ${name} (${path})`, async ({ page }) => { |
| 96 | + await page.goto(path); |
| 97 | + await argosScreenshot(page, name); |
| 98 | + }); |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +## Run Argos on Vercel Preview |
| 103 | + |
| 104 | +To initiate tests on Vercel Preview, the workflow will respond to the `deployment_status` event, triggering the test workflow upon a Vercel deployment notification. |
| 105 | + |
| 106 | +**Note:** Alternative approaches, such as the [Await for Vercel Deployment](https://github.com/marketplace/actions/await-for-vercel-deployment) action, can be utilized to retrieve the Vercel deployment URL for testing purposes. |
| 107 | + |
| 108 | +Below is an example of a workflow file designed to execute tests on a Vercel deployment: |
| 109 | + |
| 110 | +```yml |
| 111 | +# .github/workflows/test-preview.yml |
| 112 | +name: Playwright + Argos Tests |
| 113 | +on: |
| 114 | + # Trigger on deployment event |
| 115 | + deployment_status: |
| 116 | +jobs: |
| 117 | + test: |
| 118 | + # Run tests only if the deployment is successful |
| 119 | + if: github.event_name == 'deployment_status' && github.event.deployment_status.state == 'success' |
| 120 | + runs-on: ubuntu-latest |
| 121 | + steps: |
| 122 | + - uses: actions/checkout@v4 |
| 123 | + - uses: actions/setup-node@v4 |
| 124 | + |
| 125 | + - name: Install dependencies |
| 126 | + run: npm ci |
| 127 | + |
| 128 | + - name: Run Playwright tests |
| 129 | + uses: docker://mcr.microsoft.com/playwright:v1.42.1-jammy |
| 130 | + run: npm exec -- playwright test |
| 131 | + env: |
| 132 | + # Set the GITHUB_TOKEN, Argos will use it to retrieve |
| 133 | + # informations about your deployment |
| 134 | + # Read more about GITHUB_TOKEN here: https://docs.github.com/en/actions/security-guides/automatic-token-authentication |
| 135 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 136 | + # Set `BASE_URL` with Vercel Preview URL |
| 137 | + BASE_URL: ${{ github.event.deployment_status.environment_url }} |
| 138 | + # If the test runs on the production deployment, use "main" as branch |
| 139 | + # Here's "main" must be the reference branch set up in Argos |
| 140 | + ARGOS_BRANCH: ${{ github.event.deployment_status.environment == 'Production' && 'main' || '' }} |
| 141 | +``` |
| 142 | +
|
| 143 | +For a comprehensive explanation of each step involved, refer to the [Argos Preview Deployment Guide](https://argos-ci.com/docs/run-on-preview-deployment). |
| 144 | +
|
| 145 | +Upon completion, you will receive status checks on GitHub: |
| 146 | +
|
| 147 | + |
| 148 | +
|
| 149 | +## Project Source Code |
| 150 | +
|
| 151 | +Explore the GitHub repository for the source code of this integration: |
| 152 | +
|
| 153 | +- [GitHub Repository](https://github.com/jsfez/argos-vercel-preview) |
| 154 | +- [Example of pull request that runs tests against preview](https://github.com/jsfez/argos-run-argos-on-vercel-preview/pull/2) |
| 155 | +
|
| 156 | +## Conclusion |
| 157 | +
|
| 158 | +Vercel Preview and Argos form a powerful duo in web development. Vercel Preview allows hands-on, manual verification of your web applications in real environments. Argos leverage automated visual regression testing, catching any discrepancies across devices and browsers. This combination, they make a great team, giving developers the peace of mind that their sites are both functionally robust and beautifully across all screens. |
0 commit comments