-
Notifications
You must be signed in to change notification settings - Fork 43
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
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7092376
added sample scripts
kamal-kaur04 96ea7db
added minor changes
kamal-kaur04 1dc44f0
added csharp sample scripts
kamal-kaur04 0c5ecd6
updated README.md
kamal-kaur04 a22b5c2
added markTestStatus and other script changes
kamal-kaur04 84729f8
improved parallel script for java and c#
kamal-kaur04 c20eaa2
replaced dotnet single test filename
kamal-kaur04 66d3463
added local using bindings test for python and java
kamal-kaur04 b9282c8
added pixel and iphone tests for python and c#
kamal-kaur04 20f0c5a
removed print statements
kamal-kaur04 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\"}}"); | ||
} | ||
kamal-kaur04 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
catch (Exception e) { | ||
Console.WriteLine(e); | ||
await page.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"failed\", \"reason\": \" Title did not match\"}}"); | ||
} | ||
await browser.CloseAsync(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\"}}"); | ||
} | ||
kamal-kaur04 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
catch { | ||
await page.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"failed\", \"reason\": \" Title did not match\"}}"); | ||
} | ||
await browser.CloseAsync(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
 | ||
|
||
## 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. | ||
kamal-kaur04 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
`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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
 | ||
|
||
## 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.