Skip to content
Open
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
123 changes: 123 additions & 0 deletions .github/workflows/sanity-workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# Sanity workflow that verifies the NUnit + Reqnroll + Playwright BrowserStack
# SDK sample against a full commit id, mirroring browserstack/xunit-reqnroll-playwright-browserstack.
# Two test runs:
# 1. Public bstackdemo scenario (browserstackLocal: false in yml).
# 2. BrowserStack Local scenario (yml flipped to true; 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 Reqnroll 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]
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: NunitCsharpReqnrollPlaywrightBrowserstack.Tests
run: |
sed -i '/^userName:/d; /^accessKey:/d' browserstack.yml

- name: Install dependencies
run: dotnet build

- name: Run sample tests (public bstackdemo)
working-directory: NunitCsharpReqnrollPlaywrightBrowserstack.Tests
run: dotnet test --filter "FullyQualifiedName~BStackDemoCart"

- name: Run local tests (BrowserStack Local + python http.server harness)
shell: bash
working-directory: NunitCsharpReqnrollPlaywrightBrowserstack.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" && python -m http.server 45454 ) &
HTTP_PID=$!
trap 'kill "$HTTP_PID" 2>/dev/null || true' EXIT
sleep 2
# 2. Flip the SDK Local toggle so the SDK starts/stops the tunnel.
sed -i 's/^browserstackLocal: false/browserstackLocal: true/' browserstack.yml
# 3. Run only the local scenario; cloud browser reaches the harness through bs-local.com.
dotnet test --filter "FullyQualifiedName~BStackLocalSample"

- 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')
}
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
bin/
obj/
log/
TestResults/
.vs/
.vscode/
.idea/
.config/
.browserstack/
*.user
*.suo
.DS_Store
browserstack.err

# Reqnroll-generated code-behind for .feature files
**/Features/*.feature.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Feature: BStackLocalSample

As a developer testing a private host
I want BrowserStack Local to tunnel my localhost to BrowserStack
So that the cloud browser can reach a page only my machine can serve

Scenario: Reach a private host via BrowserStack Local
Given I open the local sample page on bs-local
Then the local sample page title contains "BrowserStack Local"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Feature: BStackDemo cart

As a shopper on bstackdemo.com
I want to add an item to my cart
So that I can verify the cart shows what I picked

Scenario: Add the first item to cart
Given I open the bstackdemo home page
When I add the first product to the cart
Then the cart shows 1 item that matches the product I added
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.Playwright;
using Reqnroll;

namespace NunitCsharpReqnrollPlaywrightBrowserstack.Tests.Hooks;

[Binding]
public class PlaywrightHooks
{
private readonly ScenarioContext _scenario;
private IPlaywright? _pw;
private IBrowser? _browser;

public PlaywrightHooks(ScenarioContext scenario) => _scenario = scenario;

[BeforeScenario]
public async Task SetUp()
{
_pw = await Playwright.CreateAsync();
_browser = await _pw.Chromium.LaunchAsync();
var context = await _browser.NewContextAsync();
var page = await context.NewPageAsync();
_scenario.Set(page, "page");
}

[AfterScenario]
public async Task TearDown()
{
if (_browser is not null) await _browser.CloseAsync();
_pw?.Dispose();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<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="Reqnroll.NUnit" Version="3.3.4" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
</ItemGroup>

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

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.Playwright;
using Reqnroll;

namespace NunitCsharpReqnrollPlaywrightBrowserstack.Tests.StepDefinitions;

// Mirrors browserstack/csharp-playwright-browserstack -> SampleLocalTest.cs:
// page.GotoAsync("http://bs-local.com:45454/") + title.Contains("BrowserStack Local")
[Binding]
public class LocalSampleSteps
{
private readonly ScenarioContext _scenario;
private IPage Page => _scenario.Get<IPage>("page");

public LocalSampleSteps(ScenarioContext scenario) => _scenario = scenario;

[Given(@"I open the local sample page on bs-local")]
public async Task OpenLocalSamplePage()
{
await Page.GotoAsync("http://bs-local.com:45454/");
}

[Then(@"the local sample page title contains ""(.*)""")]
public async Task LocalSamplePageTitleContains(string expected)
{
var actual = await Page.TitleAsync();
Assert.That(actual, Does.Contain(expected));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Microsoft.Playwright;
using Reqnroll;

namespace NunitCsharpReqnrollPlaywrightBrowserstack.Tests.StepDefinitions;

[Binding]
public class SampleSteps
{
private readonly ScenarioContext _scenario;
private IPage Page => _scenario.Get<IPage>("page");
private string _productTitle = string.Empty;

public SampleSteps(ScenarioContext scenario) => _scenario = scenario;

[Given(@"I open the bstackdemo home page")]
public async Task OpenHome()
{
await Page.GotoAsync("https://bstackdemo.com/");
}

[When(@"I add the first product to the cart")]
public async Task AddFirstProduct()
{
var firstProduct = Page.Locator("[id=\"\\31 \"]");
var titles = await firstProduct.Locator(".shelf-item__title").AllInnerTextsAsync();
_productTitle = titles[0];
await firstProduct.GetByText("Add to Cart").ClickAsync();
}

[Then(@"the cart shows 1 item that matches the product I added")]
public async Task CartHasOneMatchingItem()
{
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));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# =============================
# 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-csharp-reqnroll-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).
# Reqnroll (BDD layer) is auto-detected by the SDK on top of the runner declared here.
framework: nunit

source: nunit-csharp-reqnroll-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 Hooks/PlaywrightHooks.cs calls `pw.Chromium.LaunchAsync()`
# unconditionally -- the SDK transparently routes the launch to the per-platform
# browser configured here at runtime.
platforms:
- os: Windows
osVersion: 11
browserName: chrome
browserVersion: latest
- os: OS X
osVersion: Ventura
browserName: playwright-webkit
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
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: false # <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 NunitCsharpReqnrollPlaywrightBrowserstack.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}") = "NunitCsharpReqnrollPlaywrightBrowserstack.Tests", "NunitCsharpReqnrollPlaywrightBrowserstack.Tests\NunitCsharpReqnrollPlaywrightBrowserstack.Tests.csproj", "{A6D6CB01-9FC5-4A8E-94E8-1C450639104D}"
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
{A6D6CB01-9FC5-4A8E-94E8-1C450639104D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A6D6CB01-9FC5-4A8E-94E8-1C450639104D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A6D6CB01-9FC5-4A8E-94E8-1C450639104D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A6D6CB01-9FC5-4A8E-94E8-1C450639104D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Loading
Loading