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..9078b2e --- /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 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. +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..07c5dec --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,47 @@ +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 + timeout-minutes: 15 + defaults: + run: + shell: bash + 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 --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 | 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 + 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..74be1c2 --- /dev/null +++ b/playwright/HomePageTests.cs @@ -0,0 +1,22 @@ +using VisualRegressionTracker; + +public class HomePageTests +{ + [Test] + public async Task HomePageMatchesBaseline() + { + 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 }); + + var result = await TestSession.Vrt.Track( + "Home page", + screenshot, + browser: TestSession.Browser.BrowserType.Name, + 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/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..9f51136 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 + +- 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 --only-shell chromium` + +## 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..b4b4008 --- /dev/null +++ b/playwright/TestSession.cs @@ -0,0 +1,43 @@ +using Microsoft.Playwright; +using Tracker = VisualRegressionTracker.VisualRegressionTracker; + +[SetUpFixture] +public class TestSession +{ + private static IPlaywright? playwright; + + 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() + { + try + { + if (Vrt?.IsStarted == true) + { + await Vrt.Stop(); + } + } + finally + { + 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 +}