From e6854774c287bebc19ddd8cb7467e864bf40acd5 Mon Sep 17 00:00:00 2001 From: Pavlo Strunkin Date: Mon, 2 Nov 2026 21:17:04 +0200 Subject: [PATCH 1/4] Rework the example for sdk-dotnet 5.0.0 and VRT 6.0 Replace the 2020 dotnet-script with an NUnit project on .NET 10 that uses Microsoft.Playwright and VisualRegressionTracker 5.0.0 from NuGet. CI runs it against a 6.0.0-rc1 server: the first run fails on the new baseline, the build is approved through the API, and the second run passes. Co-Authored-By: Claude Opus 5.5 --- .github/dependabot.yml | 43 +++++++++++++++++++++++++++++ .github/vrt/approve-latest-build.sh | 14 ++++++++++ .github/vrt/docker-compose.yml | 33 ++++++++++++++++++++++ .github/workflows/ci.yml | 43 +++++++++++++++++++++++++++++ .gitignore | 13 ++++----- README.md | 7 +++-- global.json | 9 ++++++ playwright/HomePageTests.cs | 21 ++++++++++++++ playwright/PlaywrightExample.csproj | 23 +++++++++++++++ playwright/README.md | 34 +++++++++++++++++++---- playwright/TestSession.cs | 38 +++++++++++++++++++++++++ playwright/example.csx | 20 -------------- playwright/vrt.json.example | 7 +++++ 13 files changed, 270 insertions(+), 35 deletions(-) create mode 100644 .github/dependabot.yml create mode 100755 .github/vrt/approve-latest-build.sh create mode 100644 .github/vrt/docker-compose.yml create mode 100644 .github/workflows/ci.yml create mode 100644 global.json create mode 100644 playwright/HomePageTests.cs create mode 100644 playwright/PlaywrightExample.csproj create mode 100644 playwright/TestSession.cs delete mode 100644 playwright/example.csx create mode 100644 playwright/vrt.json.example diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..4661569 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,43 @@ +version: 2 +updates: + - package-ecosystem: nuget + directory: "/playwright" + schedule: + interval: weekly + open-pull-requests-limit: 5 + groups: + nuget-minor-patch: + patterns: + - "*" + update-types: + - minor + - patch + nuget-major: + patterns: + - "*" + update-types: + - major + nuget-security: + applies-to: security-updates + patterns: + - "*" + + - package-ecosystem: docker-compose + directory: "/.github/vrt" + schedule: + interval: weekly + open-pull-requests-limit: 5 + groups: + docker: + patterns: + - "*" + + - package-ecosystem: github-actions + directory: "/" + schedule: + interval: weekly + open-pull-requests-limit: 5 + groups: + github-actions: + patterns: + - "*" diff --git a/.github/vrt/approve-latest-build.sh b/.github/vrt/approve-latest-build.sh new file mode 100755 index 0000000..3831ea1 --- /dev/null +++ b/.github/vrt/approve-latest-build.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +set -euo pipefail + +login='{"email": "visual-regression-tracker@example.com", "password": "123456"}' +token=$(curl -sf -H 'Content-Type: application/json' -d "$login" "$VRT_APIURL/users/login" | jq -r .token) +auth="Authorization: Bearer $token" + +project=$(curl -sf -H "$auth" "$VRT_APIURL/projects" | jq -r --arg name "$VRT_PROJECT" '.[] | select(.name == $name) | .id') +build=$(curl -sf -H "$auth" "$VRT_APIURL/builds?projectId=$project&take=1&skip=0" | jq '.data[0]') + +# The example must have stopped the build although its test failed. +jq -e '.isRunning == false and .unresolvedCount > 0' <<<"$build" + +curl -sf -X PATCH -H "$auth" "$VRT_APIURL/builds/$(jq -r .id <<<"$build")/approve?merge=false" diff --git a/.github/vrt/docker-compose.yml b/.github/vrt/docker-compose.yml new file mode 100644 index 0000000..9399a96 --- /dev/null +++ b/.github/vrt/docker-compose.yml @@ -0,0 +1,33 @@ +services: + api: + image: visualregressiontracker/api:6.0.0-rc1 + environment: + DATABASE_URL: postgresql://vrt:vrt@postgres:5432/vrt + JWT_SECRET: ci + APP_FRONTEND_URL: http://localhost:8080 + BODY_PARSER_JSON_LIMIT: 5mb + ports: + - "4200:3000" + depends_on: + migration: + condition: service_completed_successfully + migration: + image: visualregressiontracker/migration:6.0.0-rc1 + environment: + DATABASE_URL: postgresql://vrt:vrt@postgres:5432/vrt + DEFAULT_USER_EMAIL: visual-regression-tracker@example.com + DEFAULT_USER_PASSWORD: "123456" + DEFAULT_USER_API_KEY: DEFAULTUSERAPIKEYTOBECHANGED + depends_on: + postgres: + condition: service_healthy + postgres: + image: postgres:17 + environment: + POSTGRES_USER: vrt + POSTGRES_PASSWORD: vrt + POSTGRES_DB: vrt + healthcheck: + test: ["CMD-SHELL", "pg_isready -d vrt -U vrt"] + interval: 2s + retries: 30 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..3f2f189 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,43 @@ +name: CI + +on: + push: + branches: [main] + pull_request: + +permissions: + contents: read + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + playwright: + runs-on: ubuntu-latest + defaults: + run: + working-directory: playwright + env: + VRT_APIURL: http://localhost:4200 + VRT_APIKEY: DEFAULTUSERAPIKEYTOBECHANGED + VRT_PROJECT: Default project + VRT_BRANCHNAME: master + steps: + - uses: actions/checkout@v7 + - name: Start Visual Regression Tracker + working-directory: .github/vrt + run: docker compose up -d --quiet-pull + - uses: actions/setup-dotnet@v6 + with: + global-json-file: global.json + - run: dotnet build + - run: pwsh bin/Debug/net10.0/playwright.ps1 install --with-deps chromium + - name: Wait for Visual Regression Tracker + run: timeout 180 bash -c 'until curl -s -o /dev/null http://localhost:4200; do sleep 2; done' + - name: First run fails, the screenshot has no baseline yet + run: if dotnet test --no-build; then exit 1; fi + - name: Approve the new baseline + run: ../.github/vrt/approve-latest-build.sh + - name: Second run passes against the approved baseline + run: dotnet test --no-build diff --git a/.gitignore b/.gitignore index cd645c7..00f3237 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,6 @@ -.coverage -junit -visual_regression_tracker.egg-info -htmlcov -coverage.xml -build -.idea -dist +bin/ +obj/ +TestResults/ +.idea/ +.vs/ vrt.json diff --git a/README.md b/README.md index fac8f8c..4bea30d 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,5 @@ -# List of examples -* [Playwright + SDK](https://github.com/Visual-Regression-Tracker/examples-dotnet/tree/master/playwright) +# Visual Regression Tracker examples for .NET + +The example uses [sdk-dotnet](https://github.com/Visual-Regression-Tracker/sdk-dotnet) 5.0.0 from NuGet (.NET 10) and opens https://the-internet.herokuapp.com headless: + +- [Playwright + NUnit + SDK](playwright) diff --git a/global.json b/global.json new file mode 100644 index 0000000..1d364c6 --- /dev/null +++ b/global.json @@ -0,0 +1,9 @@ +{ + "sdk": { + "version": "10.0.100", + "rollForward": "latestFeature" + }, + "test": { + "runner": "Microsoft.Testing.Platform" + } +} diff --git a/playwright/HomePageTests.cs b/playwright/HomePageTests.cs new file mode 100644 index 0000000..50a1342 --- /dev/null +++ b/playwright/HomePageTests.cs @@ -0,0 +1,21 @@ +using VisualRegressionTracker; + +public class HomePageTests +{ + [Test] + public async Task HomePageMatchesBaseline() + { + var page = await TestSession.Browser.NewPageAsync(new() { ViewportSize = new() { Width = 1280, Height = 720 } }); + await page.GotoAsync("https://the-internet.herokuapp.com/"); + var screenshot = await page.ScreenshotAsync(new() { FullPage = true }); + + var result = await TestSession.Vrt.Track( + "Home page", + screenshot, + browser: TestSession.Browser.BrowserType.Name, + viewport: "1280x720"); + + TestContext.Out.WriteLine($"{result.Status}: {result.Url}"); + Assert.That(result.Status, Is.AnyOf(TestRunStatus.Ok, TestRunStatus.Approved, TestRunStatus.AutoApproved)); + } +} diff --git a/playwright/PlaywrightExample.csproj b/playwright/PlaywrightExample.csproj new file mode 100644 index 0000000..eea98b1 --- /dev/null +++ b/playwright/PlaywrightExample.csproj @@ -0,0 +1,23 @@ + + + + net10.0 + Exe + enable + enable + true + + + + + + + + + + + + + + + diff --git a/playwright/README.md b/playwright/README.md index 8699afa..2d249ba 100644 --- a/playwright/README.md +++ b/playwright/README.md @@ -1,5 +1,29 @@ -# How to use -* Requires dotnet 5.0 -* Install dependencies `dotnet tool install -g dotnet-script` -* Copy configuration file `vrt.json` into directory. -* Run example `dotnet script example.csx` +# Playwright + NUnit + +[HomePageTests.cs](HomePageTests.cs) takes a full-page screenshot with [Playwright for .NET](https://playwright.dev/dotnet/) and tracks it with Visual Regression Tracker. [TestSession.cs](TestSession.cs) starts one VRT build before the tests and stops it after them, also when a test fails. + +## Prerequisites + +- [.NET 10 SDK](https://dotnet.microsoft.com/download) (pinned in [global.json](../global.json)) +- [PowerShell](https://learn.microsoft.com/powershell/scripting/install/installing-powershell) to install the Playwright browser +- A running Visual Regression Tracker 6.0 server and its API key + +## Setup + +- Build the project: `dotnet build` +- Install the browser: `pwsh bin/Debug/net10.0/playwright.ps1 install chromium` +- Copy `vrt.json.example` to `vrt.json` and set your connection, or use `VRT_*` environment variables (`VRT_APIURL`, `VRT_APIKEY`, `VRT_PROJECT`, `VRT_BRANCHNAME`, `VRT_CIBUILDID`, `VRT_ENABLESOFTASSERT`), which override the file + +## Run + +```sh +dotnet test +``` + +## What to expect + +- First run: the test fails with `No baseline` and a link to the test run. The VRT UI shows a build with one `New` test run, "Home page". +- Approve it in the UI. It becomes the baseline for the branch. +- Next runs pass while the page looks the same (`Ok`). When it changes, the test fails with `Difference found` and the build shows an `Unresolved` test run with the diff. Approve the change to make it the new baseline, or reject it. + +`New` and `Unresolved` fail the test; `Ok`, `Approved` and `AutoApproved` pass. With `enableSoftAssert`, the SDK writes the failure to stderr instead of throwing, and the status assertion in the test fails it. diff --git a/playwright/TestSession.cs b/playwright/TestSession.cs new file mode 100644 index 0000000..ea197da --- /dev/null +++ b/playwright/TestSession.cs @@ -0,0 +1,38 @@ +using Microsoft.Playwright; +using Tracker = VisualRegressionTracker.VisualRegressionTracker; + +[SetUpFixture] +public class TestSession +{ + private static IPlaywright playwright = null!; + + public static IBrowser Browser { get; private set; } = null!; + + public static Tracker Vrt { get; private set; } = null!; + + [OneTimeSetUp] + public async Task Start() + { + playwright = await Playwright.CreateAsync(); + Browser = await playwright.Chromium.LaunchAsync(); + + Vrt = new Tracker(); + await Vrt.Start(); + } + + [OneTimeTearDown] + public async Task Stop() + { + if (Vrt?.IsStarted == true) + { + await Vrt.Stop(); + } + Vrt?.Dispose(); + + if (Browser != null) + { + await Browser.CloseAsync(); + } + playwright?.Dispose(); + } +} diff --git a/playwright/example.csx b/playwright/example.csx deleted file mode 100644 index 85bab02..0000000 --- a/playwright/example.csx +++ /dev/null @@ -1,20 +0,0 @@ -#r "nuget: VisualRegressionTracker, 4.7.0" -#r "nuget: PlaywrightSharp, 0.162.1" - -using System; -using PlaywrightSharp; -using VisualRegressionTracker; - -{ - using var playwright = await Playwright.CreateAsync(); - await using var browser = await playwright.Chromium.LaunchAsync(); - var page = await browser.NewPageAsync(); - await page.GoToAsync("https://github.com/Visual-Regression-Tracker/sdk-dotnet/tree/main"); - var screenshot = await page.ScreenshotAsync(); - - var vrt = new VisualRegressionTracker.VisualRegressionTracker(); - await using var cleanup = await vrt.Start(); - await vrt.Track("sdk-dotnet", screenshot); - - Console.WriteLine("Done"); -} diff --git a/playwright/vrt.json.example b/playwright/vrt.json.example new file mode 100644 index 0000000..6167913 --- /dev/null +++ b/playwright/vrt.json.example @@ -0,0 +1,7 @@ +{ + "apiUrl": "http://localhost:4200", + "apiKey": "DEFAULTUSERAPIKEYTOBECHANGED", + "project": "Default project", + "branchName": "master", + "enableSoftAssert": false +} From eb642b6f7d06294d073f4f291dadc55282f9837a Mon Sep 17 00:00:00 2001 From: Pavlo Strunkin Date: Mon, 2 Nov 2026 21:20:19 +0200 Subject: [PATCH 2/4] Close the browser even when stopping the build fails, one context per test Also derive the viewport tag from the page, install only the headless shell, and require the first CI run to fail on the missing baseline. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 7 +++++-- playwright/HomePageTests.cs | 5 +++-- playwright/README.md | 4 ++-- playwright/TestSession.cs | 21 +++++++++++++-------- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3f2f189..7527bc9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,6 +15,7 @@ concurrency: jobs: playwright: runs-on: ubuntu-latest + timeout-minutes: 15 defaults: run: working-directory: playwright @@ -32,11 +33,13 @@ jobs: with: global-json-file: global.json - run: dotnet build - - run: pwsh bin/Debug/net10.0/playwright.ps1 install --with-deps chromium + - run: pwsh bin/Debug/net10.0/playwright.ps1 install --with-deps --only-shell chromium - name: Wait for Visual Regression Tracker run: timeout 180 bash -c 'until curl -s -o /dev/null http://localhost:4200; do sleep 2; done' - name: First run fails, the screenshot has no baseline yet - run: if dotnet test --no-build; then exit 1; fi + run: | + if dotnet test --no-build | tee first-run.log; then exit 1; fi + grep -q 'No baseline' first-run.log - name: Approve the new baseline run: ../.github/vrt/approve-latest-build.sh - name: Second run passes against the approved baseline diff --git a/playwright/HomePageTests.cs b/playwright/HomePageTests.cs index 50a1342..74be1c2 100644 --- a/playwright/HomePageTests.cs +++ b/playwright/HomePageTests.cs @@ -5,7 +5,8 @@ public class HomePageTests [Test] public async Task HomePageMatchesBaseline() { - var page = await TestSession.Browser.NewPageAsync(new() { ViewportSize = new() { Width = 1280, Height = 720 } }); + await using var context = await TestSession.Browser.NewContextAsync(); + var page = await context.NewPageAsync(); await page.GotoAsync("https://the-internet.herokuapp.com/"); var screenshot = await page.ScreenshotAsync(new() { FullPage = true }); @@ -13,7 +14,7 @@ public async Task HomePageMatchesBaseline() "Home page", screenshot, browser: TestSession.Browser.BrowserType.Name, - viewport: "1280x720"); + viewport: $"{page.ViewportSize!.Width}x{page.ViewportSize.Height}"); TestContext.Out.WriteLine($"{result.Status}: {result.Url}"); Assert.That(result.Status, Is.AnyOf(TestRunStatus.Ok, TestRunStatus.Approved, TestRunStatus.AutoApproved)); diff --git a/playwright/README.md b/playwright/README.md index 2d249ba..9f51136 100644 --- a/playwright/README.md +++ b/playwright/README.md @@ -10,9 +10,9 @@ ## Setup +- Copy `vrt.json.example` to `vrt.json` and set your connection, or use the [`VRT_*` environment variables](https://github.com/Visual-Regression-Tracker/sdk-dotnet#or-as-environment-variables), which override the file - Build the project: `dotnet build` -- Install the browser: `pwsh bin/Debug/net10.0/playwright.ps1 install chromium` -- Copy `vrt.json.example` to `vrt.json` and set your connection, or use `VRT_*` environment variables (`VRT_APIURL`, `VRT_APIKEY`, `VRT_PROJECT`, `VRT_BRANCHNAME`, `VRT_CIBUILDID`, `VRT_ENABLESOFTASSERT`), which override the file +- Install the browser: `pwsh bin/Debug/net10.0/playwright.ps1 install --only-shell chromium` ## Run diff --git a/playwright/TestSession.cs b/playwright/TestSession.cs index ea197da..b4b4008 100644 --- a/playwright/TestSession.cs +++ b/playwright/TestSession.cs @@ -4,7 +4,7 @@ [SetUpFixture] public class TestSession { - private static IPlaywright playwright = null!; + private static IPlaywright? playwright; public static IBrowser Browser { get; private set; } = null!; @@ -23,16 +23,21 @@ public async Task Start() [OneTimeTearDown] public async Task Stop() { - if (Vrt?.IsStarted == true) + try { - await Vrt.Stop(); + if (Vrt?.IsStarted == true) + { + await Vrt.Stop(); + } } - Vrt?.Dispose(); - - if (Browser != null) + finally { - await Browser.CloseAsync(); + Vrt?.Dispose(); + if (Browser != null) + { + await Browser.CloseAsync(); + } + playwright?.Dispose(); } - playwright?.Dispose(); } } From f9fa0b661cd4f06e2dc384843e691b59ade8c836 Mon Sep 17 00:00:00 2001 From: Pavlo Strunkin Date: Mon, 2 Nov 2026 21:21:01 +0200 Subject: [PATCH 3/4] Let the approval script take a project ID as well as a name Co-Authored-By: Claude Opus 5.5 --- .github/vrt/approve-latest-build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/vrt/approve-latest-build.sh b/.github/vrt/approve-latest-build.sh index 3831ea1..9078b2e 100755 --- a/.github/vrt/approve-latest-build.sh +++ b/.github/vrt/approve-latest-build.sh @@ -5,7 +5,7 @@ login='{"email": "visual-regression-tracker@example.com", "password": "123456"}' token=$(curl -sf -H 'Content-Type: application/json' -d "$login" "$VRT_APIURL/users/login" | jq -r .token) auth="Authorization: Bearer $token" -project=$(curl -sf -H "$auth" "$VRT_APIURL/projects" | jq -r --arg name "$VRT_PROJECT" '.[] | select(.name == $name) | .id') +project=$(curl -sf -H "$auth" "$VRT_APIURL/projects" | jq -r --arg name "$VRT_PROJECT" '.[] | select(.name == $name or .id == $name) | .id') build=$(curl -sf -H "$auth" "$VRT_APIURL/builds?projectId=$project&take=1&skip=0" | jq '.data[0]') # The example must have stopped the build although its test failed. From fa5216a0eac70f64295123e83f3be0ea7cbdf1c7 Mon Sep 17 00:00:00 2001 From: Pavlo Strunkin Date: Mon, 2 Nov 2026 21:23:50 +0200 Subject: [PATCH 4/4] Run CI steps with pipefail so the first run's failure is not masked by tee Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7527bc9..07c5dec 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,6 +18,7 @@ jobs: timeout-minutes: 15 defaults: run: + shell: bash working-directory: playwright env: VRT_APIURL: http://localhost:4200