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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
bin
obj
.DS_Store
.vs
local.log
BrowserStackLocal.exe
19 changes: 19 additions & 0 deletions PlaywrightDotnetTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Playwright" Version="1.28.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="BrowserStackLocal" Version="2.2.0" />
</ItemGroup>

<ItemGroup>
<None Remove="BrowserStackLocal" />
</ItemGroup>
</Project>
25 changes: 25 additions & 0 deletions PlaywrightDotnetTests.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.810.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PlaywrightDotnetTests", "PlaywrightDotnetTests.csproj", "{DAF08465-4D94-4B65-AB98-C0965E732F81}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DAF08465-4D94-4B65-AB98-C0965E732F81}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DAF08465-4D94-4B65-AB98-C0965E732F81}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DAF08465-4D94-4B65-AB98-C0965E732F81}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DAF08465-4D94-4B65-AB98-C0965E732F81}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7FAE2CF5-37B9-4938-B8F6-00B4DD9EFE94}
EndGlobalSection
EndGlobal
152 changes: 152 additions & 0 deletions PlaywrightLocalTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
using Microsoft.Playwright;
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using BrowserStack;
using System.Collections;

class PlaywrightLocalTest
{

public static async Task main(string[] args)
{
// The following capability variables contains the set of os/browser environments where you want to run your tests. You can choose to alter this list according to your needs. Read more on https://browserstack.com/docs/automate/playwright/browsers-and-os
try
{
ArrayList capabilitiesList = getCapabilitiesList();
Task[] taskList = new Task[capabilitiesList.Count];

Local local = new Local();

List<KeyValuePair<string, string>> bsLocalArgs = new List<KeyValuePair<string, string>>();
bsLocalArgs.Add(new KeyValuePair<string, string>("key", "BROWSERSTACK_ACCESS_KEY"));
bsLocalArgs.Add(new KeyValuePair<string, string>("localIdentifier", "Test123"));

local.start(bsLocalArgs);

for (int i = 0; i < capabilitiesList.Count; i++)
{
string capsJson;
capsJson = JsonConvert.SerializeObject(capabilitiesList[i]);
var task = Executetestwithcaps(capsJson);
taskList[i] = task;
}

await Task.WhenAll(taskList);
local.stop();

}
catch (Exception e)
{
Console.WriteLine(e);
}
}

static ArrayList getCapabilitiesList()
{
ArrayList capabilitiesList = new ArrayList();

string? BROWSERSTACK_USERNAME = Environment.GetEnvironmentVariable("BROWSERSTACK_USERNAME");
string? BROWSERSTACK_ACCESS_KEY = Environment.GetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY");

Dictionary<string, string> windowsChromeCap = new Dictionary<string, string>();
windowsChromeCap.Add("browser", "chrome"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
windowsChromeCap.Add("browser_version", "latest");
windowsChromeCap.Add("os", "Windows");
windowsChromeCap.Add("os_version", "11");
windowsChromeCap.Add("build", "browserstack-build-1");
windowsChromeCap.Add("buildTag", "Regression");
windowsChromeCap.Add("browserstack.debug", "true");
windowsChromeCap.Add("browserstack.networkLogs", "true");
windowsChromeCap.Add("browserstack.console", "info");
windowsChromeCap.Add("browserstack.local", "true");
windowsChromeCap.Add("browserstack.localIdentifier", "Test123");
windowsChromeCap.Add("browserstack.username", BROWSERSTACK_USERNAME);
windowsChromeCap.Add("browserstack.accessKey", BROWSERSTACK_ACCESS_KEY);
capabilitiesList.Add(windowsChromeCap);


Dictionary<string, string> venturaWebkitCap = new Dictionary<string, string>();
venturaWebkitCap.Add("browser", "playwright-webkit"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
venturaWebkitCap.Add("browser_version", "latest");
venturaWebkitCap.Add("os", "osx");
venturaWebkitCap.Add("os_version", "Ventura");
venturaWebkitCap.Add("build", "browserstack-build-1");
venturaWebkitCap.Add("buildTag", "Regression");
venturaWebkitCap.Add("browserstack.debug", "true");
venturaWebkitCap.Add("browserstack.networkLogs", "true");
venturaWebkitCap.Add("browserstack.console", "info");
venturaWebkitCap.Add("browserstack.local", "true");
venturaWebkitCap.Add("browserstack.localIdentifier", "Test123");
venturaWebkitCap.Add("browserstack.username", BROWSERSTACK_USERNAME);
venturaWebkitCap.Add("browserstack.accessKey", BROWSERSTACK_ACCESS_KEY);
capabilitiesList.Add(venturaWebkitCap);

Dictionary<string, string> windowsFirefoxCap = new Dictionary<string, string>();
windowsFirefoxCap.Add("browser", "playwright-firefox"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`\
windowsFirefoxCap.Add("browser_version", "latest");
windowsFirefoxCap.Add("os", "Windows");
windowsFirefoxCap.Add("os_version", "11");
windowsFirefoxCap.Add("build", "browserstack-build-1");
windowsFirefoxCap.Add("buildTag", "Regression");
windowsFirefoxCap.Add("browserstack.debug", "true");
windowsFirefoxCap.Add("browserstack.networkLogs", "true");
windowsFirefoxCap.Add("browserstack.console", "info");
windowsFirefoxCap.Add("browserstack.local", "true");
windowsFirefoxCap.Add("browserstack.localIdentifier", "Test123");
windowsFirefoxCap.Add("browserstack.username", BROWSERSTACK_USERNAME);
windowsFirefoxCap.Add("browserstack.accessKey", BROWSERSTACK_ACCESS_KEY);
capabilitiesList.Add(windowsFirefoxCap);

return capabilitiesList;
}

public static async Task Executetestwithcaps(string capabilities)
{
using var playwright = await Playwright.CreateAsync();
string cdpUrl = "wss://cdp.browserstack.com/playwright?caps=" + Uri.EscapeDataString(capabilities);

await using var browser = await playwright.Chromium.ConnectAsync(cdpUrl);
var page = await browser.NewPageAsync();
try
{
await MarkTestName(page);
page.SetDefaultTimeout(60000);
await page.GotoAsync("http://localhost:3000/");
await page.Locator("#\\31 > .shelf-item__buy-btn").ClickAsync();

var text = await page.Locator("#__next > div > div > div.float-cart.float-cart--open > div.float-cart__content > div.float-cart__shelf-container > div > div.shelf-item__details > p.title").TextContentAsync();

if (text == "iPhone 12")
{
await MarkTestStatus("passed", "Item Added", page);
}
else
{
await MarkTestStatus("failed", "Test Failed", page);
}
}
catch (Exception err)
{
Console.WriteLine(err.Message);
await MarkTestStatus("failed", "Something Failed", page);
}
await browser.CloseAsync();

}
public static async Task MarkTestStatus(string status, string reason, IPage page)
{
await page.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"" + status + "\", \"reason\": \"" + reason + "\"}}");
}

public static async Task MarkTestName(IPage page)
{
string? thisFile = new System.Diagnostics.StackTrace(true).GetFrame(0).GetFileName();
string[] result = thisFile.Split(new char[] { '/' });
string[] filenameResult = result.Last().Split(new char[] { '.' });
string? testName = filenameResult.First();

await page.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\": \"setSessionName\", \"arguments\": {\"name\":\"" + testName + "\"}}");
}
}
133 changes: 133 additions & 0 deletions PlaywrightParallelTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using Microsoft.Playwright;
using System;
using System.Threading;
using Newtonsoft.Json;
using System.Collections;

class PlaywrightParallelTest
{
public static async Task main(string[] args)
{
// The following capability variables contains the set of os/browser environments where you want to run your tests. You can choose to alter this list according to your needs. Read more on https://browserstack.com/docs/automate/playwright/browsers-and-os
try
{
ArrayList capabilitiesList = getCapabilitiesList();
Task[] taskList = new Task[capabilitiesList.Count];

for (int i = 0; i < capabilitiesList.Count; i++)
{
string capsJson;
capsJson = JsonConvert.SerializeObject(capabilitiesList[i]);
var task = Executetestwithcaps(capsJson);
taskList[i] = task;
}

await Task.WhenAll(taskList);

}
catch (Exception e)
{
Console.WriteLine(e);
}
}
static ArrayList getCapabilitiesList()
{
ArrayList capabilitiesList = new ArrayList();

string? BROWSERSTACK_USERNAME = Environment.GetEnvironmentVariable("BROWSERSTACK_USERNAME");
string? BROWSERSTACK_ACCESS_KEY = Environment.GetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY");


Dictionary<string, string> windowsChromeCap = new Dictionary<string, string>();
windowsChromeCap.Add("browser", "chrome"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
windowsChromeCap.Add("browser_version", "latest");
windowsChromeCap.Add("os", "Windows");
windowsChromeCap.Add("os_version", "11");
windowsChromeCap.Add("build", "browserstack-build-1");
windowsChromeCap.Add("buildTag", "Regression");
windowsChromeCap.Add("browserstack.debug", "true");
windowsChromeCap.Add("browserstack.networkLogs", "true");
windowsChromeCap.Add("browserstack.console", "info");
windowsChromeCap.Add("browserstack.username", BROWSERSTACK_USERNAME);
windowsChromeCap.Add("browserstack.accessKey", BROWSERSTACK_ACCESS_KEY);
capabilitiesList.Add(windowsChromeCap);


Dictionary<string, string> venturaWebkitCap = new Dictionary<string, string>();
venturaWebkitCap.Add("browser", "playwright-webkit"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
venturaWebkitCap.Add("browser_version", "latest");
venturaWebkitCap.Add("os", "osx");
venturaWebkitCap.Add("os_version", "Ventura");
venturaWebkitCap.Add("build", "browserstack-build-1");
venturaWebkitCap.Add("buildTag", "Regression");
venturaWebkitCap.Add("browserstack.debug", "true");
venturaWebkitCap.Add("browserstack.networkLogs", "true");
venturaWebkitCap.Add("browserstack.console", "info");
venturaWebkitCap.Add("browserstack.username", BROWSERSTACK_USERNAME);
venturaWebkitCap.Add("browserstack.accessKey", BROWSERSTACK_ACCESS_KEY);
capabilitiesList.Add(venturaWebkitCap);

Dictionary<string, string> windowsFirefoxCap = new Dictionary<string, string>();
windowsFirefoxCap.Add("browser", "playwright-firefox"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`\
windowsFirefoxCap.Add("browser_version", "latest");
windowsFirefoxCap.Add("os", "Windows");
windowsFirefoxCap.Add("os_version", "11");
windowsFirefoxCap.Add("build", "browserstack-build-1");
windowsFirefoxCap.Add("buildTag", "Regression");
windowsFirefoxCap.Add("browserstack.debug", "true");
windowsFirefoxCap.Add("browserstack.networkLogs", "true");
windowsFirefoxCap.Add("browserstack.console", "info");
windowsFirefoxCap.Add("browserstack.username", BROWSERSTACK_USERNAME);
windowsFirefoxCap.Add("browserstack.accessKey", BROWSERSTACK_ACCESS_KEY);
capabilitiesList.Add(windowsFirefoxCap);

return capabilitiesList;
}

//Executetestwithcaps function takes capabilities from 'SampleTestCase' function and executes the test
public static async Task Executetestwithcaps(string capabilities)
{
using var playwright = await Playwright.CreateAsync();
string cdpUrl = "wss://cdp.browserstack.com/playwright?caps=" + Uri.EscapeDataString(capabilities);

await using var browser = await playwright.Chromium.ConnectAsync(cdpUrl);
var page = await browser.NewPageAsync();
try
{
await MarkTestName(page);
page.SetDefaultTimeout(60000);
await page.GotoAsync("https://bstackdemo.com/");
await page.Locator("#\\31 > .shelf-item__buy-btn").ClickAsync();

var text = await page.Locator("#__next > div > div > div.float-cart.float-cart--open > div.float-cart__content > div.float-cart__shelf-container > div > div.shelf-item__details > p.title").TextContentAsync();

if (text == "iPhone 12")
{
await MarkTestStatus("passed", "Item Added", page);
}
else
{
await MarkTestStatus("failed", "Test Failed", page);
}
}
catch (Exception err)
{
Console.WriteLine(err.Message);
await MarkTestStatus("failed", "Something Failed", page);
}
await browser.CloseAsync();
}
public static async Task MarkTestStatus(string status, string reason, IPage page)
{
await page.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"" + status + "\", \"reason\": \"" + reason + "\"}}");
}
public static async Task MarkTestName(IPage page)
{
string? thisFile = new System.Diagnostics.StackTrace(true).GetFrame(0).GetFileName();
string[] result = thisFile.Split(new char[] { '/' });
string[] filenameResult = result.Last().Split(new char[] { '.' });
string? testName = filenameResult.First();

await page.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\": \"setSessionName\", \"arguments\": {\"name\":\"" + testName + "\"}}");
}
}
27 changes: 27 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;

namespace PlaywrightTesting
{
class Program
{
public static async Task Main(string[] args)
{
switch (args[0])
{
case "parallel":
Console.WriteLine("Running Parallel Test");
await PlaywrightParallelTest.main(args);
break;
case "local":
Console.WriteLine("Running Local Test");
await PlaywrightLocalTest.main(args);
break;
default:
Console.WriteLine("Running Single Test by default");
await PlaywrightParallelTest.main(args);
break;
}
}
}
}
Loading