Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -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:
- "*"
14 changes: 14 additions & 0 deletions .github/vrt/approve-latest-build.sh
Original file line number Diff line number Diff line change
@@ -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"
33 changes: 33 additions & 0 deletions .github/vrt/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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
47 changes: 47 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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
13 changes: 5 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
.coverage
junit
visual_regression_tracker.egg-info
htmlcov
coverage.xml
build
.idea
dist
bin/
obj/
TestResults/
.idea/
.vs/
vrt.json
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 9 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"sdk": {
"version": "10.0.100",
"rollForward": "latestFeature"
},
"test": {
"runner": "Microsoft.Testing.Platform"
}
}
22 changes: 22 additions & 0 deletions playwright/HomePageTests.cs
Original file line number Diff line number Diff line change
@@ -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));
}
}
23 changes: 23 additions & 0 deletions playwright/PlaywrightExample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EnableNUnitRunner>true</EnableNUnitRunner>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Playwright" Version="1.64.1" />
<PackageReference Include="NUnit" Version="5.0.0" />
<PackageReference Include="NUnit3TestAdapter" Version="6.3.1" />
<PackageReference Include="VisualRegressionTracker" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<Using Include="NUnit.Framework" />
<None Include="vrt.json" CopyToOutputDirectory="PreserveNewest" Condition="Exists('vrt.json')" />
</ItemGroup>

</Project>
34 changes: 29 additions & 5 deletions playwright/README.md
Original file line number Diff line number Diff line change
@@ -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.
43 changes: 43 additions & 0 deletions playwright/TestSession.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
20 changes: 0 additions & 20 deletions playwright/example.csx

This file was deleted.

7 changes: 7 additions & 0 deletions playwright/vrt.json.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"apiUrl": "http://localhost:4200",
"apiKey": "DEFAULTUSERAPIKEYTOBECHANGED",
"project": "Default project",
"branchName": "master",
"enableSoftAssert": false
}
Loading