-
Notifications
You must be signed in to change notification settings - Fork 1
Add BrowserStack SDK + Playwright NUnit sample #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6a37d35
Add BrowserStack SDK + Playwright NUnit sample
Jimesh-browserstack dd1530b
Restore Example 1/Example 2 generic comment in Parallels block
Jimesh-browserstack 97bef30
Repoint comments to xunit-reqnroll as canonical reference
Jimesh-browserstack ce21957
Address review: trim meta/cross-repo comments
Jimesh-browserstack ed3541a
Address review: ppp=1, local=true, Category filters
Jimesh-browserstack cadbcfa
Align sanity workflow with Category= filters and default-on Local
Jimesh-browserstack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| # Sanity workflow that verifies the NUnit + 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 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 "FullyQualifiedName~BStackDemoCart" | ||
|
|
||
| - 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. Flip the SDK Local toggle so the SDK starts/stops the tunnel. | ||
| sed -i.bak 's/^browserstackLocal: false/browserstackLocal: true/' browserstack.yml && rm -f browserstack.yml.bak | ||
| # 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') | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| bin/ | ||
| obj/ | ||
| log/ | ||
| .config/ | ||
| .browserstack/ | ||
| .idea/ | ||
| .vs/ | ||
| *.user | ||
| *.suo | ||
| *.bak |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| // Allow NUnit to run [TestFixture] classes in parallel within each SDK-spawned | ||
| // platform process. Combined with `parallelsPerPlatform: 2` in browserstack.yml, | ||
| // each `dotnet test` produces 4 platforms x 2 fixtures = 8 concurrent sessions. | ||
| [assembly: NUnit.Framework.Parallelizable(NUnit.Framework.ParallelScope.Fixtures)] | ||
| [assembly: NUnit.Framework.LevelOfParallelism(4)] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| namespace NunitPlaywrightBrowserstack.Tests; | ||
|
|
||
| [TestFixture] | ||
| [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)); | ||
| } | ||
| } |
24 changes: 24 additions & 0 deletions
24
NunitPlaywrightBrowserstack.Tests/BStackLocalSampleTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| namespace NunitPlaywrightBrowserstack.Tests; | ||
|
|
||
| // Mirrors xunit-reqnroll-playwright-browserstack's Features/LocalSample.feature | ||
|
Jimesh-browserstack marked this conversation as resolved.
Outdated
|
||
| // + StepDefinitions/LocalSampleSteps.cs: | ||
| // page.GotoAsync("http://bs-local.com:45454/") + title.Contains("BrowserStack Local") | ||
| // | ||
| // Requires: | ||
| // * browserstack.yml has `browserstackLocal: true` | ||
| // * a local HTTP server is serving a page with title containing "BrowserStack Local" | ||
| // on port 45454 (the sanity workflow stands up `python -m http.server 45454` | ||
| // against a one-file index.html harness). | ||
| [TestFixture] | ||
| [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")); | ||
| } | ||
| } | ||
27 changes: 27 additions & 0 deletions
27
NunitPlaywrightBrowserstack.Tests/NunitPlaywrightBrowserstack.Tests.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
36
NunitPlaywrightBrowserstack.Tests/PlaywrightFixtureBase.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: 2 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: 2 | ||
|
|
||
| # ========================================== | ||
| # 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.