Skip to content

Added sample scripts for C#, Java and Python #19

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 10 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
15 changes: 15 additions & 0 deletions playwright-dotnet/PlaywrightDotnetTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<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.19.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

</Project>
45 changes: 45 additions & 0 deletions playwright-dotnet/PlaywrightLocalTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Microsoft.Playwright;
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using Newtonsoft.Json;

class PlaywrightLocalTest
{
public static async Task main(string[] args)
{
using var playwright = await Playwright.CreateAsync();

Dictionary<string, string> browserstackOptions = new Dictionary<string, string>();
browserstackOptions.Add("name", "Playwright local sample test");
browserstackOptions.Add("build", "playwright-build-3");
browserstackOptions.Add("os", "osx");
browserstackOptions.Add("os_version", "catalina");
browserstackOptions.Add("browser", "chrome"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
browserstackOptions.Add("browserstack.username", "BROWSERSTACK_USERNAME");
browserstackOptions.Add("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY");
browserstackOptions.Add("browserstack.local", "true");
string capsJson = JsonConvert.SerializeObject(browserstackOptions);
string cdpUrl = "wss://cdp.browserstack.com/playwright?caps=" + Uri.EscapeDataString(capsJson);

await using var browser = await playwright.Chromium.ConnectAsync(cdpUrl);
var page = await browser.NewPageAsync();
try {
await page.GotoAsync("https://www.google.co.in/");
await page.Locator("[aria-label='Search']").ClickAsync();
await page.FillAsync("[aria-label='Search']", "BrowserStack");
await page.Locator("[aria-label='Google Search'] >> nth=0").ClickAsync();
var title = await page.TitleAsync();

if (title == "BrowserStack - Google Search")
{
// following line of code is responsible for marking the status of the test on BrowserStack as 'passed'. You can use this code in your after hook after each test
await page.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\":\"setSessionStatus\",\"arguments\":{\"status\":\"passed\",\"reason\":\"Title matched\"}}");
}
}
catch {
await page.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"failed\", \"reason\": \" Title did not match\"}}");
}
await browser.CloseAsync();
}
}
122 changes: 122 additions & 0 deletions playwright-dotnet/PlaywrightParallelTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using Microsoft.Playwright;
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using Newtonsoft.Json;

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 {
// allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
// browser_version capability is valid only for branded `chrome` and `edge` browsers and you can specify any browser version like `latest`, `latest-beta`, `latest-1` and so on.
var device1 = SampleTestCase("playwright-chromium", "latest", "osx", "Catalina", "macOS Catalina - Chrome latest", "Parallel-build-2");
var device2 = SampleTestCase("chrome", "latest", "osx", "catalina", "Branded Google Chrome on Catalina", "Parallel-build-2");
var device3 = SampleTestCase("edge", "latest", "osx", "catalina", "Branded Microsoft Edge on Catalina", "Parallel-build-2");
var device4 = SampleTestCase("playwright-firefox", "latest", "osx", "catalina", "Playwright firefox on Catalina", "Parallel-build-2");
var device5 = SampleTestCase("playwright-webkit", "latest", "osx", "catalina", "Playwright webkit on Catalina", "Parallel-build-2");

//Executing the methods
await Task.WhenAll(device1, device2, device3, device4, device5);
} catch (Exception e) {
Console.WriteLine(e);
}
}
static async Task SampleTestCase(String browser_name, String browser_version, String os, String os_version, String test_name, String build_name)
{
Dictionary<string, string> browserstackOptions = new Dictionary<string, string>();
string capsJson;

try {

switch (browser_name)
{
case "playwright-chromium":
browserstackOptions.Add("build", build_name);
browserstackOptions.Add("name", test_name);
browserstackOptions.Add("os", os);
browserstackOptions.Add("os_version", os_version);
browserstackOptions.Add("browser", browser_name);
browserstackOptions.Add("browser_version", browser_version);
browserstackOptions.Add("browserstack.username", "BROWSERSTACK_USERNAME");
browserstackOptions.Add("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY");
capsJson = JsonConvert.SerializeObject(browserstackOptions);
var task = Executetestwithcaps(capsJson);
await task;
break;
case "chrome":
browserstackOptions.Add("build", build_name);
browserstackOptions.Add("name", test_name);
browserstackOptions.Add("os", os);
browserstackOptions.Add("os_version", os_version);
browserstackOptions.Add("browser", browser_name);
browserstackOptions.Add("browser_version", browser_version);
browserstackOptions.Add("browserstack.username", "BROWSERSTACK_USERNAME");
browserstackOptions.Add("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY");
capsJson = JsonConvert.SerializeObject(browserstackOptions);
task = Executetestwithcaps(capsJson);
await task;
break;
case "playwright-firefox":
browserstackOptions.Add("build", build_name);
browserstackOptions.Add("name", test_name);
browserstackOptions.Add("os", os);
browserstackOptions.Add("os_version", os_version);
browserstackOptions.Add("browser", browser_name);
browserstackOptions.Add("browser_version", browser_version);
browserstackOptions.Add("browserstack.username", "BROWSERSTACK_USERNAME");
browserstackOptions.Add("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY");
capsJson = JsonConvert.SerializeObject(browserstackOptions);
task = Executetestwithcaps(capsJson);
await task;
break;
default:
browserstackOptions.Add("build", build_name);
browserstackOptions.Add("name", test_name);
browserstackOptions.Add("os", os);
browserstackOptions.Add("os_version", os_version);
browserstackOptions.Add("browser", browser_name);
browserstackOptions.Add("browser_version", browser_version);
browserstackOptions.Add("browserstack.username", "BROWSERSTACK_USERNAME");
browserstackOptions.Add("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY");
capsJson = JsonConvert.SerializeObject(browserstackOptions);
task = Executetestwithcaps(capsJson);
await task;
break;
}
} catch (Exception e) {
Console.WriteLine(e);
}
}

//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 page.GotoAsync("https://www.google.co.in/");
await page.Locator("[aria-label='Search']").ClickAsync();
await page.FillAsync("[aria-label='Search']", "BrowserStack");
await page.Locator("[aria-label='Google Search'] >> nth=0").ClickAsync();
var title = await page.TitleAsync();

if (title == "BrowserStack - Google Search")
{
// following line of code is responsible for marking the status of the test on BrowserStack as 'passed'. You can use this code in your after hook after each test
await page.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\":\"setSessionStatus\",\"arguments\":{\"status\":\"passed\",\"reason\":\"Title matched\"}}");
}
}
catch (Exception e) {
Console.WriteLine(e);
await page.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"failed\", \"reason\": \" Title did not match\"}}");
}
await browser.CloseAsync();
}
}
44 changes: 44 additions & 0 deletions playwright-dotnet/PlaywrightSingleTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Microsoft.Playwright;
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using Newtonsoft.Json;

class PlaywrightSingleTest
{
public static async Task main(string[] args)
{
using var playwright = await Playwright.CreateAsync();

Dictionary<string, string> browserstackOptions = new Dictionary<string, string>();
browserstackOptions.Add("name", "Playwright first sample test");
browserstackOptions.Add("build", "playwright-build-1");
browserstackOptions.Add("os", "osx");
browserstackOptions.Add("os_version", "catalina");
browserstackOptions.Add("browser", "chrome"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
browserstackOptions.Add("browserstack.username", "BROWSERSTACK_USERNAME");
browserstackOptions.Add("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY");
string capsJson = JsonConvert.SerializeObject(browserstackOptions);
string cdpUrl = "wss://cdp.browserstack.com/playwright?caps=" + Uri.EscapeDataString(capsJson);

await using var browser = await playwright.Chromium.ConnectAsync(cdpUrl);
var page = await browser.NewPageAsync();
try {
await page.GotoAsync("https://www.google.co.in/");
await page.Locator("[aria-label='Search']").ClickAsync();
await page.FillAsync("[aria-label='Search']", "BrowserStack");
await page.Locator("[aria-label='Google Search'] >> nth=0").ClickAsync();
var title = await page.TitleAsync();

if (title == "BrowserStack - Google Search")
{
// following line of code is responsible for marking the status of the test on BrowserStack as 'passed'. You can use this code in your after hook after each test
await page.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\":\"setSessionStatus\",\"arguments\":{\"status\":\"passed\",\"reason\":\"Title matched\"}}");
}
}
catch {
await page.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"failed\", \"reason\": \" Title did not match\"}}");
}
await browser.CloseAsync();
}
}
31 changes: 31 additions & 0 deletions playwright-dotnet/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;

namespace PlaywrightTesting
{
class Program
{
public static async Task Main(string[] args)
{
switch (args[0])
{
case "single":
Console.WriteLine("Running Single Test");
await PlaywrightSingleTest.main(args); // redirects to class main() method
break;
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 PlaywrightSingleTest.main(args);
break;
}
}
}
}
37 changes: 37 additions & 0 deletions playwright-dotnet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Testing with playwright-browserstack in C#

[Playwright](https://playwright.dev/dotnet/) Integration with BrowserStack.

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

## Setup

* Clone the repo and run `cd Playwright-dotnet`
* Run `dotnet build`
* Install required browsers - replace netX with actual output folder name, f.ex. net6.0.
`pwsh bin/Debug/netX/playwright.ps1 install`

## Running your tests

- To run a single test, run `dotnet run single`
- To run a parallel test, run command `dotnet run parallel`

### Run sample test on privately hosted websites

1. You have to download the BrowserStack Local binary from the links below (depending on your environment):
* [OS X (10.7 and above)](https://www.browserstack.com/browserstack-local/BrowserStackLocal-darwin-x64.zip)
* [Linux 32-bit](https://www.browserstack.com/browserstack-local/BrowserStackLocal-linux-ia32.zip)
* [Linux 64-bit](https://www.browserstack.com/browserstack-local/BrowserStackLocal-linux-x64.zip)
* [Windows (XP and above)](https://www.browserstack.com/browserstack-local/BrowserStackLocal-win32.zip)
2. Once you have downloaded and unzipped the file, you can initiate the binary by running the command: `./BrowserStackLocal --key YOUR_ACCESS_KEY`
3. Once you see the terminal say "[SUCCESS]" You can now access your local server(s) in our remote browser”, your local testing connection is considered established.
4. You can then run the sample Local test using `dotnet run local`

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


## Notes
* You can view your test results on the [BrowserStack Automate dashboard](https://www.browserstack.com/automate)

## Additional Resources
* [Documentation for writing Automate test scripts with BrowserStack](https://www.browserstack.com/docs/automate/playwright)
39 changes: 39 additions & 0 deletions playwright-java/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Testing with playwright-browserstack in Java

[Playwright](https://playwright.dev/java/) Integration with BrowserStack.

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

## Setup

* Clone the repo and run `cd playwright-java`

## Running your tests

- To run a single test, run
`mvn -Dexec.mainClass="com.browserstack.PlaywrightSingleTest" -Dexec.classpathScope=test test-compile exec:java
`
- To run parallel tests, run
`mvn -Dexec.mainClass="com.browserstack.PlaywrightParallelTest" -Dexec.classpathScope=test test-compile exec:java
`

### Run sample test on privately hosted websites

1. You have to download the BrowserStack Local binary from the links below (depending on your environment):
* [OS X (10.7 and above)](https://www.browserstack.com/browserstack-local/BrowserStackLocal-darwin-x64.zip)
* [Linux 32-bit](https://www.browserstack.com/browserstack-local/BrowserStackLocal-linux-ia32.zip)
* [Linux 64-bit](https://www.browserstack.com/browserstack-local/BrowserStackLocal-linux-x64.zip)
* [Windows (XP and above)](https://www.browserstack.com/browserstack-local/BrowserStackLocal-win32.zip)
2. Once you have downloaded and unzipped the file, you can initiate the binary by running the command: `./BrowserStackLocal --key YOUR_ACCESS_KEY`
3. Once you see the terminal say "[SUCCESS]" You can now access your local server(s) in our remote browser”, your local testing connection is considered established.
4. You can then run the sample Local test using
`mvn -Dexec.mainClass="com.browserstack.PlaywrightLocalTest" -Dexec.classpathScope=test test-compile exec:java`

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


## Notes
* You can view your test results on the [BrowserStack Automate dashboard](https://www.browserstack.com/automate)

## Additional Resources
* [Documentation for writing Automate test scripts with BrowserStack](https://www.browserstack.com/docs/automate/playwright)
23 changes: 23 additions & 0 deletions playwright-java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>playwright-java</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>1.19.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Loading