diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..1ff0c42 --- /dev/null +++ b/.gitattributes @@ -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 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..22ca79e --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +packages +TestResults +TestResult.xml +bin +obj +.vs +.DS_Store +log diff --git a/CODEOWNERS b/CODEOWNERS index 444c87b..09a587d 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -1 +1,3 @@ -* @browserstack/afd-dev +.github/* @browserstack/asi-devs + +* @browserstack/automate-public-repos diff --git a/CSharp-Playwright-BrowserStack.sln b/CSharp-Playwright-BrowserStack.sln new file mode 100644 index 0000000..0c7f63a --- /dev/null +++ b/CSharp-Playwright-BrowserStack.sln @@ -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 diff --git a/CSharp-Playwright-BrowserStack/CSharp-Playwright-BrowserStack.csproj b/CSharp-Playwright-BrowserStack/CSharp-Playwright-BrowserStack.csproj new file mode 100644 index 0000000..8462b03 --- /dev/null +++ b/CSharp-Playwright-BrowserStack/CSharp-Playwright-BrowserStack.csproj @@ -0,0 +1,24 @@ + + + + net6.0 + CSharpPlaywrightBrowserStack + CSharpPlaywrightBrowserStack + enable + enable + false + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + diff --git a/CSharp-Playwright-BrowserStack/SampleLocalTest.cs b/CSharp-Playwright-BrowserStack/SampleLocalTest.cs new file mode 100644 index 0000000..d0996ae --- /dev/null +++ b/CSharp-Playwright-BrowserStack/SampleLocalTest.cs @@ -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); + } + } +} + diff --git a/CSharp-Playwright-BrowserStack/SampleTest.cs b/CSharp-Playwright-BrowserStack/SampleTest.cs new file mode 100644 index 0000000..98973dc --- /dev/null +++ b/CSharp-Playwright-BrowserStack/SampleTest.cs @@ -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 phone = await Page.Locator("[id=\"\\31 \"]").Locator(".shelf-item__title").AllInnerTextsAsync(); + Console.WriteLine("Phone =>" + phone[0]); + + + // Get the items from Cart + IReadOnlyList 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 cartItemText = await cartItem.AllInnerTextsAsync(); + Console.WriteLine("Cart item => " + cartItemText[0]); + + Assert.That(phone[0], Is.EqualTo(cartItemText[0])); + } + } +} diff --git a/CSharp-Playwright-BrowserStack/browserstack.yml b/CSharp-Playwright-BrowserStack/browserstack.yml new file mode 100644 index 0000000..aaa1611 --- /dev/null +++ b/CSharp-Playwright-BrowserStack/browserstack.yml @@ -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 # (Default false) +# browserStackLocalOptions: +# Options to be passed to BrowserStack local in-case of advanced configurations + # localIdentifier: # (Default: null) Needed if you need to run multiple instances of local. + # forceLocal: true # (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 # # Set to true if you need screenshots for every selenium command ran +networkLogs: false # Set to true to enable HAR logs capturing +consoleLogs: errors # Remote browser's console debug levels to be printed (Default: errors) +# Available options are `disable`, `errors`, `warnings`, `info`, `verbose` (Default: errors) + diff --git a/README.md b/README.md index d381fe3..c9c9e54 100644 --- a/README.md +++ b/README.md @@ -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) + + + +## 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= && + export BROWSERSTACK_ACCESS_KEY= + ``` + + * For Windows Cmd: + ``` + set BROWSERSTACK_USERNAME= + set BROWSERSTACK_ACCESS_KEY= + ``` + + * For Windows Powershell: + ``` + $env:BROWSERSTACK_USERNAME= + $env: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)