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
63 changes: 63 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto

###############################################################################
# Set default behavior for command prompt diff.
#
# This is need for earlier builds of msysgit that does not have it on by
# default for csharp files.
# Note: This is only used by command line
###############################################################################
#*.cs diff=csharp

###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.csproj merge=binary
#*.vbproj merge=binary
#*.vcxproj merge=binary
#*.vcproj merge=binary
#*.dbproj merge=binary
#*.fsproj merge=binary
#*.lsproj merge=binary
#*.wixproj merge=binary
#*.modelproj merge=binary
#*.sqlproj merge=binary
#*.wwaproj merge=binary

###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary

###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
packages
TestResults
TestResult.xml
bin
obj
.vs
.DS_Store
log
4 changes: 3 additions & 1 deletion CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
* @browserstack/afd-dev
.github/* @browserstack/asi-devs

* @browserstack/automate-public-repos
25 changes: 25 additions & 0 deletions CSharp-Playwright-BrowserStack.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 = 25.0.1706.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSharp-Playwright-BrowserStack", "CSharp-Playwright-BrowserStack\CSharp-Playwright-BrowserStack.csproj", "{5DC1EC78-C0C5-4869-9769-90718B820BB9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5DC1EC78-C0C5-4869-9769-90718B820BB9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5DC1EC78-C0C5-4869-9769-90718B820BB9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5DC1EC78-C0C5-4869-9769-90718B820BB9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5DC1EC78-C0C5-4869-9769-90718B820BB9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0A720641-5ECB-4F2D-8678-EB726CF8DDA7}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>CSharpPlaywrightBrowserStack</RootNamespace>
<AssemblyName>CSharpPlaywrightBrowserStack</AssemblyName>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit.Analyzers" Version="3.6.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Microsoft.Playwright" Version="1.36.0" />
<PackageReference Include="Microsoft.Playwright.NUnit" Version="1.36.0" />
<PackageReference Include="BrowserStack.TestAdapter" Version="0.*" />
</ItemGroup>
</Project>
24 changes: 24 additions & 0 deletions CSharp-Playwright-BrowserStack/SampleLocalTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.Playwright.NUnit;
using NUnit.Framework;

namespace CSharpPlaywrightBrowserStack
{
[TestFixture]
[Category("sample-local-test")]
public class SampleLocalTest : PageTest
{
public SampleLocalTest() : base() { }

[Test]
public async Task BStackHealthCheck()
{
// 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);
}
}
}

44 changes: 44 additions & 0 deletions CSharp-Playwright-BrowserStack/SampleTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using NUnit.Framework;
using Microsoft.Playwright;
using Microsoft.Playwright.NUnit;

namespace CSharpPlaywrightBrowserStack
{
[TestFixture]
[Category("sample-test")]
public class SampleTest : PageTest
{
public SampleTest() : base() { }

[Test]
public async Task SearchBstackDemo()
{
//Navigate to the bstackdemo url
_ = await Page.GotoAsync("https://bstackdemo.com/");

// Add the first item to cart
await Page.Locator("[id=\"\\31 \"]").GetByText("Add to Cart").ClickAsync();
IReadOnlyList<string> phone = await Page.Locator("[id=\"\\31 \"]").Locator(".shelf-item__title").AllInnerTextsAsync();
Console.WriteLine("Phone =>" + phone[0]);


// Get the items from Cart
IReadOnlyList<string> quantity = await Page.Locator(".bag__quantity").AllInnerTextsAsync();
Console.WriteLine("Bag quantity =>" + quantity[0]);

// Verify if there is a shopping cart
StringAssert.Contains("1", await Page.Locator(".bag__quantity").InnerTextAsync());


//Get the handle for cart item
ILocator cartItem = Page.Locator(".shelf-item__details").Locator(".title");

// Verify if the cart has the right item
StringAssert.Contains(await cartItem.InnerTextAsync(), string.Join(" ", phone));
IReadOnlyList<string> cartItemText = await cartItem.AllInnerTextsAsync();
Console.WriteLine("Cart item => " + cartItemText[0]);

Assert.That(phone[0], Is.EqualTo(cartItemText[0]));
}
}
}
65 changes: 65 additions & 0 deletions CSharp-Playwright-BrowserStack/browserstack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# =============================
# Set BrowserStack Credentials
# =============================
# Add your BrowserStack userName and accessKey here or set BROWSERSTACK_USERNAME and
# BROWSERSTACK_ACCESS_KEY as env variables
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: browserstack build
# `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}

# =======================================
# 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)
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

# ==========================================
# 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)
# browserStackLocalOptions:
# Options to be passed to BrowserStack local in-case of advanced configurations
# 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

source: csharp-playwright-browserstack:sample-sdk:v1.0

# ===================
# 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)

62 changes: 61 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,62 @@
# csharp-playwright-browserstack
Creating a sample repo for different Playwright languages and runners
Creating a sample repo for CSharp Playwright

This sample elaborates the [NUnit](http://www.nunit.org/) Integration with BrowserStack.

![BrowserStack Logo](https://d98b8t1nnulk5.cloudfront.net/production/images/layout/logo-header.png?1469004780)

<img src ="https://nunit.org/img/nunit.svg" height = "71">

## Run Sample Build
* Clone the repo
* Open the solution `CSharp-Playwright-BrowserStack.sln` in Visual Studio
* Build the solution
* Update `browserstack.yml` file with your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings)
### Running your tests from CLI
* To run the test suite having cross-platform with parallelization, dotnet test --filter "Category=sample-test"
* To run local tests, dotnet test --filter "Category=sample-local-test"
### Running your tests from Test Explorer
- To run a parallel tests, run test with fixture `sample-test`
- To run local tests, run test with fixture `sample-local-test`

Understand how many parallel sessions you need by using our [Parallel Test Calculator](https://www.browserstack.com/automate/parallel-calculator?ref=github)

## Integrate your test suite

This repository uses the BrowserStack SDK to run tests on BrowserStack. Follow the steps below to install the SDK in your test suite and run tests on BrowserStack:

* Create sample browserstack.yml file with the browserstack related capabilities with your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings) and place it in your root folder.
* Add nuget library BrowserStack.TestAdapter
```sh
dotnet add BrowserStack.TestAdapter
```
* Build project `dotnet build`

## Notes
* You can view your test results on the [BrowserStack automate dashboard](https://www.browserstack.com/automate)
* To test on a different set of browsers, check out our [platform configurator](https://www.browserstack.com/automate/c-sharp#setting-os-and-browser)
* You can export the environment variables for the Username and Access Key of your BrowserStack account

* For Unix-like or Mac machines:
```
export BROWSERSTACK_USERNAME=<browserstack-username> &&
export BROWSERSTACK_ACCESS_KEY=<browserstack-access-key>
```

* For Windows Cmd:
```
set BROWSERSTACK_USERNAME=<browserstack-username>
set BROWSERSTACK_ACCESS_KEY=<browserstack-access-key>
```

* For Windows Powershell:
```
$env:BROWSERSTACK_USERNAME=<browserstack-username>
$env:BROWSERSTACK_ACCESS_KEY=<browserstack-access-key>
```

## Additional Resources
* [Documentation for writing automate playwright test scripts in C#](https://www.browserstack.com/docs/automate/playwright/getting-started/c-sharp)
* [Customizing your tests on BrowserStack](https://www.browserstack.com/automate/capabilities)
* [Browsers & mobile devices for selenium testing on BrowserStack](https://www.browserstack.com/list-of-browsers-and-platforms?product=automate)
* [Using REST API to access information about your tests via the command-line interface](https://www.browserstack.com/automate/rest-api)