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
121 changes: 121 additions & 0 deletions .github/workflows/sanity-workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# Sanity workflow that verifies the NUnit + Playwright BrowserStack SDK sample
# against a full commit id, mirroring browserstack/xunit-reqnroll-playwright-browserstack.
# Two test runs (yml ships with browserstackLocal: true; both runs benefit from it):
# 1. Public bstackdemo scenario (Category=sample-test).
# 2. BrowserStack Local scenario (Category=sample-local-test) — a python http.server
# hosts a tiny title-matching page on port 45454, the SDK starts the tunnel,
# and the test asserts that the cloud browser sees that page through bs-local.com.

name: NUnit Playwright SDK sanity workflow on workflow_dispatch

on:
workflow_dispatch:
inputs:
commit_sha:
description: 'The full commit id to build'
required: true

permissions:
contents: read
checks: write

jobs:
sanity:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
max-parallel: 1
matrix:
dotnet: ['8.0.x']
os: [windows-latest, macos-latest]
name: NUnit Repo ${{ matrix.dotnet }} - ${{ matrix.os }} Sample
env:
BROWSERSTACK_USERNAME: ${{ secrets.BROWSERSTACK_USERNAME }}
BROWSERSTACK_ACCESS_KEY: ${{ secrets.BROWSERSTACK_ACCESS_KEY }}

steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.event.inputs.commit_sha }}

- uses: actions/github-script@98814c53be79b1d30f795b907e553d8679345975
id: status-check-in-progress
env:
job_name: NUnit Repo ${{ matrix.dotnet }} - ${{ matrix.os }} Sample
commit_sha: ${{ github.event.inputs.commit_sha }}
with:
github-token: ${{ github.token }}
script: |
const result = await github.rest.checks.create({
owner: context.repo.owner,
repo: context.repo.repo,
name: process.env.job_name,
head_sha: process.env.commit_sha,
status: 'in_progress'
}).catch((err) => ({status: err.status, response: err.response}));
console.log(`The status-check response : ${result.status} Response : ${JSON.stringify(result.response)}`)
if (result.status !== 201) {
console.log('Failed to create check run')
}

- name: Setup dotnet
uses: actions/setup-dotnet@v3
with:
dotnet-version: ${{ matrix.dotnet }}

- name: Strip credential placeholders so env vars take effect
# The yml ships with literal YOUR_USERNAME / YOUR_ACCESS_KEY placeholders;
# the .NET SDK only falls back to env vars when those lines are absent.
shell: bash
working-directory: NunitPlaywrightBrowserstack.Tests
run: |
sed -i.bak '/^userName:/d; /^accessKey:/d' browserstack.yml && rm -f browserstack.yml.bak

- name: Install dependencies
run: dotnet build

- name: Run sample tests (public bstackdemo)
working-directory: NunitPlaywrightBrowserstack.Tests
run: dotnet test --filter "Category=sample-test"

- name: Run local tests (BrowserStack Local + python http.server harness)
shell: bash
working-directory: NunitPlaywrightBrowserstack.Tests
run: |
set -u
# 1. Stand up a tiny static page with a known <title>.
mkdir -p "$RUNNER_TEMP/bs-local-harness"
cat > "$RUNNER_TEMP/bs-local-harness/index.html" <<'HTML'
<!doctype html>
<html><head><title>BrowserStack Local Test</title></head>
<body>OK</body></html>
HTML
( cd "$RUNNER_TEMP/bs-local-harness" && python3 -m http.server 45454 ) &
HTTP_PID=$!
trap 'kill "$HTTP_PID" 2>/dev/null || true' EXIT
sleep 2
# 2. Run only the local scenario; cloud browser reaches the harness through bs-local.com (browserstackLocal:true ships as default).
dotnet test --filter "Category=sample-local-test"

- if: always()
uses: actions/github-script@98814c53be79b1d30f795b907e553d8679345975
id: status-check-completed
env:
conclusion: ${{ job.status }}
job_name: NUnit Repo ${{ matrix.dotnet }} - ${{ matrix.os }} Sample
commit_sha: ${{ github.event.inputs.commit_sha }}
with:
github-token: ${{ github.token }}
script: |
const result = await github.rest.checks.create({
owner: context.repo.owner,
repo: context.repo.repo,
name: process.env.job_name,
head_sha: process.env.commit_sha,
status: 'completed',
conclusion: process.env.conclusion
}).catch((err) => ({status: err.status, response: err.response}));
console.log(`The status-check response : ${result.status} Response : ${JSON.stringify(result.response)}`)
if (result.status !== 201) {
console.log('Failed to create check run')
}
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
bin/
obj/
log/
.config/
.browserstack/
.idea/
.vs/
*.user
*.suo
*.bak
3 changes: 3 additions & 0 deletions NunitPlaywrightBrowserstack.Tests/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Run [TestFixture] classes in parallel within each SDK-spawned platform process.
[assembly: NUnit.Framework.Parallelizable(NUnit.Framework.ParallelScope.Fixtures)]
[assembly: NUnit.Framework.LevelOfParallelism(4)]
24 changes: 24 additions & 0 deletions NunitPlaywrightBrowserstack.Tests/BStackDemoCartTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace NunitPlaywrightBrowserstack.Tests;

[TestFixture]
[Category("sample-test")]
[Parallelizable(ParallelScope.Self)]
public class BStackDemoCartTest : PlaywrightFixtureBase
{
[Test]
public async Task AddTheFirstItemToCart()
{
await Page.GotoAsync("https://bstackdemo.com/");

var firstProduct = Page.Locator("[id=\"\\31 \"]");
var titles = await firstProduct.Locator(".shelf-item__title").AllInnerTextsAsync();
var productTitle = titles[0];
await firstProduct.GetByText("Add to Cart").ClickAsync();

var quantity = await Page.Locator(".bag__quantity").InnerTextAsync();
Assert.That(quantity, Is.EqualTo("1"));

var cartTitle = await Page.Locator(".shelf-item__details").Locator(".title").InnerTextAsync();
Assert.That(cartTitle, Is.EqualTo(productTitle));
}
}
18 changes: 18 additions & 0 deletions NunitPlaywrightBrowserstack.Tests/BStackLocalSampleTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace NunitPlaywrightBrowserstack.Tests;

// Requires browserstack.yml has `browserstackLocal: true` and a local HTTP server
// is serving a page with title containing "BrowserStack Local" on port 45454.
[TestFixture]
[Category("sample-local-test")]
[Parallelizable(ParallelScope.Self)]
public class BStackLocalSampleTest : PlaywrightFixtureBase
{
[Test]
public async Task ReachPrivateHostViaBrowserStackLocal()
{
await Page.GotoAsync("http://bs-local.com:45454/");

var title = await Page.TitleAsync();
Assert.That(title, Does.Contain("BrowserStack Local"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BrowserStack.TestAdapter" Version="0.*" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Microsoft.Playwright" Version="*" />
<PackageReference Include="Microsoft.Playwright.NUnit" Version="*" />
<PackageReference Include="NUnit" Version="4.3.2" />
<PackageReference Include="NUnit.Analyzers" Version="4.6.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
</ItemGroup>

<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>

</Project>
36 changes: 36 additions & 0 deletions NunitPlaywrightBrowserstack.Tests/PlaywrightFixtureBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Microsoft.Playwright;

namespace NunitPlaywrightBrowserstack.Tests;

// NUnit equivalent of xunit-reqnroll-playwright-browserstack's
// Hooks/PlaywrightHooks.cs (BeforeScenario / AfterScenario) -- a per-test
// fixture that creates an IPage. Customer code calls
// `pw.Chromium.LaunchAsync()` unconditionally; the BrowserStack SDK rewrites
// the launch at runtime to route to the per-platform browser configured in
// browserstack.yml (chrome / playwright-webkit / playwright-firefox / edge).
// No Chromium.ConnectAsync(wss_url) plumbing here.
public abstract class PlaywrightFixtureBase
{
private IPlaywright? _pw;
private IBrowser? _browser;
private IBrowserContext? _context;

protected IPage Page { get; private set; } = null!;

[SetUp]
public async Task SetUp()
{
_pw = await Playwright.CreateAsync();
_browser = await _pw.Chromium.LaunchAsync();
_context = await _browser.NewContextAsync();
Page = await _context.NewPageAsync();
}

[TearDown]
public async Task TearDown()
{
if (_context is not null) await _context.CloseAsync();
if (_browser is not null) await _browser.CloseAsync();
_pw?.Dispose();
}
}
92 changes: 92 additions & 0 deletions NunitPlaywrightBrowserstack.Tests/browserstack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# =============================
# Set BrowserStack Credentials
# =============================
# Add your BrowserStack userName and accessKey here or set BROWSERSTACK_USERNAME and
# BROWSERSTACK_ACCESS_KEY as env variables.
# NOTE (.NET SDK quirk): when these fields are present in the yml, the SDK uses
# their literal values -- env vars override them only if the lines below are absent.
userName: YOUR_USERNAME
accessKey: YOUR_ACCESS_KEY

# ======================
# BrowserStack Reporting
# ======================
# The following capabilities are used to set up reporting on BrowserStack:
# Set 'projectName' to the name of your project. Example, Marketing Website
projectName: BrowserStack Samples
# Set `buildName` as the name of the job / testsuite being run
buildName: nunit-playwright-browserstack
# `buildIdentifier` is a unique id to differentiate every execution that gets appended to
# buildName. Choose your buildIdentifier format from the available expressions:
# ${BUILD_NUMBER} (Default): Generates an incremental counter with every execution
# ${DATE_TIME}: Generates a Timestamp with every execution. Eg. 05-Nov-19:30
# Read more about buildIdentifiers here -> https://www.browserstack.com/docs/automate/selenium/organize-tests
buildIdentifier: '#${BUILD_NUMBER}' # Supports strings along with either/both ${expression}
# Set `framework` of your test suite. Example, `nunit`, `xunit`, `mstest`
# This property is needed to send test context to BrowserStack (test name, status).
framework: nunit

source: nunit-playwright:sample-master:v1.0

# =======================================
# Platforms (Browsers / Devices to test)
# =======================================
# Platforms object contains all the browser / device combinations you want to test on.
# Entire list available here -> (https://www.browserstack.com/list-of-browsers-and-platforms/automate)
# Customer code in PlaywrightFixtureBase.cs calls `pw.Chromium.LaunchAsync()`
# unconditionally -- the SDK transparently routes the launch to the per-platform
# browser configured here at runtime (chrome / playwright-webkit / playwright-firefox / edge).
platforms:
- os: Windows
osVersion: 11
browserName: chrome
browserVersion: latest
- os: OS X
osVersion: Ventura
browserName: playwright-webkit
browserVersion: latest
- os: Windows
osVersion: 11
browserName: playwright-firefox
browserVersion: latest
- os: Windows
osVersion: 11
browserName: edge
browserVersion: latest

# =======================
# Parallels per Platform
# =======================
# The number of parallel threads to be used for each platform set.
# BrowserStack's SDK runner will select the best strategy based on the configured value
#
# Example 1 - If you have configured 3 platforms and set `parallelsPerPlatform` as 2, a total of 6 (2 * 3) parallel threads will be used on BrowserStack
#
# Example 2 - If you have configured 1 platform and set `parallelsPerPlatform` as 5, a total of 5 (1 * 5) parallel threads will be used on BrowserStack
#
# This sample sets parallelsPerPlatform: 1 to demonstrate NUnit's fixture-level
# parallelism on top of the SDK's per-platform fan-out: 4 platforms x 2 fixtures
# (BStackDemoCart + BStackLocalSample) = 8 concurrent sessions per `dotnet test`.
parallelsPerPlatform: 1

# ==========================================
# BrowserStack Local
# (For localhost, staging/private websites)
# ==========================================
# Set browserstackLocal to true if your website under test is not accessible publicly over the internet
# Learn more about how BrowserStack Local works here -> https://www.browserstack.com/docs/automate/selenium/local-testing-introduction
browserstackLocal: true # <boolean> (Default false)

# Options to be passed to BrowserStack local in-case of advanced configurations
# browserStackLocalOptions:
# localIdentifier: # <string> (Default: null) Needed if you need to run multiple instances of local.
# forceLocal: true # <boolean> (Default: false) Set to true if you need to resolve all your traffic via BrowserStack Local tunnel.
# Entire list of arguments available here -> https://www.browserstack.com/docs/automate/selenium/manage-incoming-connections

# ===================
# Debugging features
# ===================
debug: false # <boolean> # Set to true if you need screenshots for every selenium command ran
networkLogs: false # <boolean> Set to true to enable HAR logs capturing
consoleLogs: errors # <string> Remote browser's console debug levels to be printed (Default: errors)
# Available options are `disable`, `errors`, `warnings`, `info`, `verbose` (Default: errors)
22 changes: 22 additions & 0 deletions NunitPlaywrightBrowserstack.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NunitPlaywrightBrowserstack.Tests", "NunitPlaywrightBrowserstack.Tests\NunitPlaywrightBrowserstack.Tests.csproj", "{EC74430F-2BA0-40B4-86DD-7D24DDAA10BD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{EC74430F-2BA0-40B4-86DD-7D24DDAA10BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EC74430F-2BA0-40B4-86DD-7D24DDAA10BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EC74430F-2BA0-40B4-86DD-7D24DDAA10BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EC74430F-2BA0-40B4-86DD-7D24DDAA10BD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Loading
Loading