Skip to content
Closed
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
85 changes: 85 additions & 0 deletions .github/workflows/sanity-workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# This job is to test different profiles in sdk branch against full-commit id
# This workflow targets nunit

name: C-sharp Playwright SDK Test workflow on workflow_dispatch

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

jobs:
comment-run:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
max-parallel: 3
matrix:
dotnet: ['6.0.x', '5.0.x', '7.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: Install dependencies
run: dotnet build

- name: Run sample tests
run: dotnet test --filter "Category=sample-test"

- name: Run local tests
run: 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')
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 25.0.1706.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSharp-Playwright-BrowserStack", "CSharp-Playwright-BrowserStack.csproj", "{D309DDB3-1E3B-428B-B00B-1257F2532079}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSharp-Playwright-BrowserStack", "CSharp-Playwright-BrowserStack\CSharp-Playwright-BrowserStack.csproj", "{D309DDB3-1E3B-428B-B00B-1257F2532079}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
64 changes: 0 additions & 64 deletions CSharp-Playwright-BrowserStack/App.config

This file was deleted.

100 changes: 53 additions & 47 deletions CSharp-Playwright-BrowserStack/BrowserStackNUnitTest.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;

using Microsoft.Playwright;
using Microsoft.Playwright;
using Newtonsoft.Json;
using NUnit.Framework;
using BrowserStack;
using Newtonsoft.Json.Linq;

namespace CSharpPlaywrightBrowserStack
{
Expand All @@ -17,65 +13,68 @@ public class BrowserStackNUnitTest
protected IPage page;
protected string profile;
protected string environment;
protected string configFile;

private Local browserStackLocal;

public BrowserStackNUnitTest(string profile, string environment)
public BrowserStackNUnitTest(string profile, string environment, string configFile)
{
this.profile = profile;
this.environment = environment;
this.configFile = configFile;
}

[SetUp]
public async Task Init()
{
NameValueCollection? caps =
ConfigurationManager.GetSection("capabilities/" + profile) as NameValueCollection;
NameValueCollection? settings =
ConfigurationManager.GetSection("environments/" + environment)
as NameValueCollection;
// Get Configuration for correct profile
string currentDirectory = Directory.GetCurrentDirectory();
string path = Path.Combine(currentDirectory, configFile);
JObject config = JObject.Parse(File.ReadAllText(path));
if (config is null)
throw new Exception("Configuration not found!");

NameValueCollection cc = (NameValueCollection) ConfigurationManager.AppSettings;
// Get Environment specific capabilities
JObject capabilitiesJsonArr = config.GetValue("environments") as JObject;
JObject capabilities = capabilitiesJsonArr.GetValue(environment) as JObject;

Console.WriteLine("KAMAL " + settings + "-" + caps + "-" + profile + "-" + environment
+ cc["user"]);
// Get Common Capabilities
JObject commonCapabilities = config.GetValue("capabilities") as JObject;

Dictionary<string, object> browserstackOptions = new Dictionary<string, object>
{
{ "browserName", settings["browser"] }
};

foreach (string key in caps.AllKeys)
{
browserstackOptions.Add(key, caps[key]);
}
// Merge Capabilities
capabilities.Merge(commonCapabilities);

String username = Environment.GetEnvironmentVariable("BROWSERSTACK_USERNAME");
if (username == null)
{
username = ConfigurationManager.AppSettings.Get("user");
}
// Get username and accesskey
string? username = Environment.GetEnvironmentVariable("BROWSERSTACK_USERNAME");
if (username is null)
username = config.GetValue("user").ToString();

String accesskey = Environment.GetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY");
if (accesskey == null)
{
accesskey = ConfigurationManager.AppSettings.Get("key");
}
string? accessKey = Environment.GetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY");
if (accessKey is null)
accessKey = config.GetValue("key").ToString();

browserstackOptions.Add("userName", username);
browserstackOptions.Add("accessKey", accesskey);
capabilities["browserstack.user"] = username;
capabilities["browserstack.key"] = accessKey;

if (caps.Get("local").ToString() == "true")
// Start Local if browserstack.local is set to true
if (profile.Equals("local") && accessKey is not null)
{
capabilities["browserstack.local"] = true;
browserStackLocal = new Local();
List<KeyValuePair<string, string>> bsLocalArgs = new List<
KeyValuePair<string, string>
>()
{
new KeyValuePair<string, string>("key", accesskey)
List<KeyValuePair<string, string>> bsLocalArgs = new List<KeyValuePair<string, string>>() {
new KeyValuePair<string, string>("key", accessKey)
};
foreach (var localOption in config.GetValue("localOptions") as JObject)
{
if (localOption.Value is not null)
{
bsLocalArgs.Add(new KeyValuePair<string, string>(localOption.Key, localOption.Value.ToString()));
}
}
browserStackLocal.start(bsLocalArgs);
}
string capsJson = JsonConvert.SerializeObject(browserstackOptions);

string capsJson = JsonConvert.SerializeObject(capabilities);
string cdpUrl = "wss://cdp.browserstack.com/playwright?caps=" + Uri.EscapeDataString(capsJson);

var playwright = await Playwright.CreateAsync();
Expand All @@ -86,10 +85,6 @@ public async Task Init()
[TearDown]
public async Task Cleanup()
{
if (page != null)
{
page.CloseAsync();
}
if (browser != null)
{
browser.CloseAsync();
Expand All @@ -99,5 +94,16 @@ public async Task Cleanup()
browserStackLocal.stop();
}
}

public static async Task SetStatus(IPage browserPage, bool passed)
{
if (browserPage is not null)
{
if (passed)
await browserPage.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"passed\", \"reason\": \"Test Passed!\"}}");
else
await browserPage.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"failed\", \"reason\": \"Test Failed!\"}}");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,21 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Microsoft.Playwright" Version="1.36.0" />
<PackageReference Include="Microsoft.Playwright" Version="*" />

<Content Include="local.conf.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="parallel.conf.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="single.conf.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<None Include="App.config">
<SubType>Designer</SubType>
</None>
<None Include="packages.config" />
<None Remove="local.conf.json" />
<None Remove="parallel.conf.json" />
<None Remove="single.conf.json" />
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
</Project>
</Project>
24 changes: 15 additions & 9 deletions CSharp-Playwright-BrowserStack/LocalTest.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
using NUnit.Framework;
using Microsoft.Playwright;
using System.Text.RegularExpressions;

namespace CSharpPlaywrightBrowserStack
{
[TestFixture("local", "chrome")]
[TestFixture("local", "chrome", "local.conf.json")]
[Category("sample-local-test")]
public class LocalTest : BrowserStackNUnitTest
{
public LocalTest(string profile, string environment) : base(profile, environment) { }
public LocalTest(string profile, string environment, string configFile) : base(profile, environment, configFile) { }

[Test]
public async Task HealthCheck()
{
// Navigate to the base url
await page.GotoAsync("http://bs-local.com:45454/");
try
{
// Navigate to the base url
await page.GotoAsync("http://bs-local.com:45454/");

// Verify if BrowserStackLocal running
var title = await page.TitleAsync();
StringAssert.Contains("BrowserStack Local", title);
// Verify if BrowserStackLocal running
var title = await page.TitleAsync();
StringAssert.Contains("BrowserStack Local", title);
SetStatus(page, title.Contains("BrowserStack Local"));
} catch (Exception)
{
SetStatus(page, false);
throw;
}
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions CSharp-Playwright-BrowserStack/ParallelTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

namespace CSharpPlaywrightBrowserStack
{
[TestFixture("parallel", "chrome")]
[TestFixture("parallel", "firefox")]
[TestFixture("parallel", "safari")]
[TestFixture("parallel", "edge")]
[TestFixture("parallel", "chrome", "parallel.conf.json")]
[TestFixture("parallel", "playwright-firefox", "parallel.conf.json")]
[TestFixture("parallel", "playwright-webkit", "parallel.conf.json")]
[Parallelizable(ParallelScope.Fixtures)]
[Category("sample-parallel-test")]
public class ParallelTest : SingleTest
{
public ParallelTest(string profile, string environment) : base(profile, environment) { }
public ParallelTest(string profile, string environment, string configFile) : base(profile, environment, configFile) { }
}
}
Loading