Skip to content

Commit 3759a47

Browse files
committed
initial commit
1 parent 000f63e commit 3759a47

File tree

13 files changed

+243
-0
lines changed

13 files changed

+243
-0
lines changed

.github/workflows/ci.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build-and-test:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v4
16+
17+
- name: Setup .NET 9
18+
uses: actions/setup-dotnet@v4
19+
with:
20+
dotnet-version: '9.0.x'
21+
22+
- name: Install Playwright Browsers
23+
run: dotnet tool install --global Microsoft.Playwright.CLI && playwright install
24+
25+
- name: Restore dependencies
26+
run: dotnet restore
27+
28+
- name: Build
29+
run: dotnet build --configuration Release --no-restore
30+
31+
- name: Test (NUnit)
32+
run: dotnet test --configuration Release --no-build --filter "FullyQualifiedName~Playwright.NUnit.Testing"
33+
34+
- name: Test (TUnit)
35+
run: dotnet test --configuration Release --no-build --filter "FullyQualifiedName~Playwright.TUnit.Testing"
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using Microsoft.Playwright;
2+
using NUnit.Framework;
3+
4+
namespace Playwright.NUnit.Testing.Base;
5+
6+
public class TestBase(BrowserKind kind) {
7+
protected readonly BrowserKind _kind = kind;
8+
9+
private IPlaywright _pw = default!;
10+
private IBrowser _browser = default!;
11+
private IBrowserContext _context = default!;
12+
protected IPage Page = default!;
13+
14+
[OneTimeSetUp]
15+
public async Task OneTimeSetUpAsync() {
16+
_pw = await Microsoft.Playwright.Playwright.CreateAsync();
17+
18+
var launch = new BrowserTypeLaunchOptions {
19+
Headless = true, // flip to false to watch the run
20+
Args = ["--no-sandbox"] // helpful in some CI agents
21+
};
22+
23+
_browser = _kind switch {
24+
BrowserKind.Chromium => await _pw.Chromium.LaunchAsync(launch),
25+
BrowserKind.Firefox => await _pw.Firefox.LaunchAsync(launch),
26+
BrowserKind.WebKit => await _pw.Webkit.LaunchAsync(launch),
27+
_ => throw new AssertionException($"Unknown browser: {_kind}")
28+
};
29+
}
30+
31+
[SetUp]
32+
public async Task SetUpAsync() {
33+
// Fresh, isolated state per test
34+
_context = await _browser.NewContextAsync(new BrowserNewContextOptions {
35+
ViewportSize = new ViewportSize { Width = 1280, Height = 800 }
36+
});
37+
Page = await _context.NewPageAsync();
38+
}
39+
40+
[TearDown]
41+
public async Task TearDownAsync() {
42+
if(_context is not null) {
43+
await _context.CloseAsync();
44+
}
45+
}
46+
47+
[OneTimeTearDown]
48+
public async Task OneTimeTearDownAsync() {
49+
if(_browser is not null) {
50+
await _browser.CloseAsync();
51+
}
52+
_pw?.Dispose();
53+
}
54+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Playwright.NUnit.Testing;
2+
3+
public enum BrowserKind
4+
{
5+
Chromium,
6+
Firefox,
7+
WebKit
8+
}
9+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Install:
2+
// Once per repo/machine: dotnet playwright install
3+
using NUnit.Framework;
4+
using Playwright.NUnit.Testing.Base;
5+
6+
namespace Playwright.NUnit.Testing;
7+
8+
[TestFixtureSource(nameof(AllBrowsers))]
9+
[Parallelizable(ParallelScope.Fixtures)]
10+
public class CrossBrowserTests(BrowserKind kind) : TestBase(kind) {
11+
12+
private static readonly BrowserKind[] AllBrowsers = [
13+
BrowserKind.Chromium,
14+
BrowserKind.Firefox,
15+
BrowserKind.WebKit];
16+
17+
[Test]
18+
public async Task HomePage_Title_ContainsAppName() {
19+
await Page.GotoAsync("https://playwright.dev/dotnet/");
20+
var title = await Page.TitleAsync();
21+
StringAssert.Contains("Playwright", title,
22+
$"[{_kind}] title should contain 'Playwright'");
23+
}
24+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
11+
<PackageReference Include="NUnit" Version="3.14.0" />
12+
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
13+
<PackageReference Include="Microsoft.Playwright.NUnit" Version="1.55.0" />
14+
</ItemGroup>
15+
16+
</Project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using TUnit.Playwright;
2+
3+
namespace Playwright.TUnit.Testing.Base;
4+
5+
// Shared tests. Page/Context/Browser/Playwright are provided by PageTest.
6+
public abstract class CrossBrowserTestsBase : PageTest {
7+
[Test]
8+
public async Task HomePage_Title_ContainsAppName() {
9+
await Page.GotoAsync("https://playwright.dev/dotnet/");
10+
var title = await Page.TitleAsync();
11+
await Assert.That(title).Contains("Playwright");
12+
}
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using Playwright.TUnit.Testing.Base;
2+
3+
namespace Playwright.TUnit.Testing.Browsers;
4+
5+
[InheritsTests]
6+
public sealed class ChromiumBrowser : CrossBrowserTestsBase {
7+
public override string BrowserName => "chromium";
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using Playwright.TUnit.Testing.Base;
2+
3+
namespace Playwright.TUnit.Testing.Browsers;
4+
5+
[InheritsTests]
6+
public sealed class FirefoxBrowser : CrossBrowserTestsBase {
7+
public override string BrowserName => "firefox";
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using Playwright.TUnit.Testing.Base;
2+
3+
namespace Playwright.TUnit.Testing.Browsers;
4+
5+
[InheritsTests]
6+
public sealed class WebKitBrowser : CrossBrowserTestsBase {
7+
public override string BrowserName => "webkit";
8+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[assembly: System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]

0 commit comments

Comments
 (0)