Skip to content

Commit a1747db

Browse files
kamal-kaur04francisf
authored andcommitted
added sample scripts
1 parent 66d130d commit a1747db

File tree

9 files changed

+466
-0
lines changed

9 files changed

+466
-0
lines changed

playwright-java/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Testing with playwright-browserstack in Java
2+
3+
[Playwright](https://playwright.dev/java/) Integration with BrowserStack.
4+
5+
![BrowserStack Logo](https://d98b8t1nnulk5.cloudfront.net/production/images/layout/logo-header.png?1469004780)
6+
7+
## Setup
8+
9+
* Clone the repo and run `cd playwright-java`
10+
11+
## Running your tests
12+
13+
- To run a single test, run
14+
`mvn -Dexec.mainClass="com.browserstack.PlaywrightSingleTest" -Dexec.classpathScope=test test-compile exec:java
15+
`
16+
- To run parallel tests, run
17+
`mvn -Dexec.mainClass="com.browserstack.PlaywrightParallelTest" -Dexec.classpathScope=test test-compile exec:java
18+
`
19+
20+
### Run sample test on privately hosted websites
21+
22+
1. You have to download the BrowserStack Local binary from the links below (depending on your environment):
23+
* [OS X (10.7 and above)](https://www.browserstack.com/browserstack-local/BrowserStackLocal-darwin-x64.zip)
24+
* [Linux 32-bit](https://www.browserstack.com/browserstack-local/BrowserStackLocal-linux-ia32.zip)
25+
* [Linux 64-bit](https://www.browserstack.com/browserstack-local/BrowserStackLocal-linux-x64.zip)
26+
* [Windows (XP and above)](https://www.browserstack.com/browserstack-local/BrowserStackLocal-win32.zip)
27+
2. Once you have downloaded and unzipped the file, you can initiate the binary by running the command: `./BrowserStackLocal --key YOUR_ACCESS_KEY`
28+
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.
29+
4. You can then run the sample Local test using
30+
`mvn -Dexec.mainClass="com.browserstack.PlaywrightLocalTest" -Dexec.classpathScope=test test-compile exec:java`
31+
32+
Understand how many parallel sessions you need by using our [Parallel Test Calculator](https://www.browserstack.com/automate/parallel-calculator?ref=github)
33+
34+
35+
## Notes
36+
* You can view your test results on the [BrowserStack Automate dashboard](https://www.browserstack.com/automate)
37+
38+
## Additional Resources
39+
* [Documentation for writing Automate test scripts with BrowserStack](https://www.browserstack.com/docs/automate/playwright)

playwright-java/pom.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>playwright-java</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>11</maven.compiler.source>
13+
<maven.compiler.target>11</maven.compiler.target>
14+
</properties>
15+
<dependencies>
16+
<dependency>
17+
<groupId>com.microsoft.playwright</groupId>
18+
<artifactId>playwright</artifactId>
19+
<version>1.19.0</version>
20+
<scope>compile</scope>
21+
</dependency>
22+
</dependencies>
23+
</project>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.browserstack;
2+
3+
import com.google.gson.JsonObject;
4+
import com.microsoft.playwright.*;
5+
import java.io.UnsupportedEncodingException;
6+
import java.net.URLEncoder;
7+
8+
public class PlaywrightLocalTest {
9+
public static void main(String[] args) {
10+
try (Playwright playwright = Playwright.create()) {
11+
JsonObject jsonObject = new JsonObject();
12+
jsonObject.addProperty("browser", "chrome"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
13+
jsonObject.addProperty("browser_version", "latest");
14+
jsonObject.addProperty("os", "osx");
15+
jsonObject.addProperty("os_version", "catalina");
16+
jsonObject.addProperty("name", "Playwright first local test");
17+
jsonObject.addProperty("build", "playwright-build-3");
18+
jsonObject.addProperty("browserstack.username", "BROWSERSTACK_USERNAME");
19+
jsonObject.addProperty("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY");
20+
jsonObject.addProperty("browserstack.local", "true");
21+
22+
BrowserType chromium = playwright.chromium();
23+
String caps = URLEncoder.encode(jsonObject.toString(), "utf-8");
24+
String ws_endpoint = "wss://cdp.browserstack.com/playwright?caps=" + caps;
25+
Browser browser = chromium.connect(ws_endpoint);
26+
Page page = browser.newPage();
27+
page.navigate("https://www.google.co.in/");
28+
Locator locator = page.locator("[aria-label='Search']");
29+
locator.click();
30+
page.fill("[aria-label='Search']", "BrowserStack");
31+
page.locator("[aria-label='Google Search'] >> nth=0").click();
32+
String title = page.title();
33+
34+
if (title.equals("BrowserStack - Google Search")) {
35+
// 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
36+
Object result = page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"passed\", \"reason\": \"Title matched\"}}");
37+
System.out.println(result);
38+
} else {
39+
Object result = page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"failed\", \"reason\": \"Title did not matched\"}}");
40+
System.out.println(result);
41+
}
42+
browser.close();
43+
} catch (UnsupportedEncodingException e) {
44+
System.out.println(e);
45+
}
46+
}
47+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package com.browserstack;
2+
3+
import com.google.gson.JsonObject;
4+
import com.microsoft.playwright.*;
5+
import java.io.UnsupportedEncodingException;
6+
7+
import java.net.URLEncoder;
8+
9+
class DeviceOne implements Runnable {
10+
public void run() {
11+
JsonObject jsonObject = new JsonObject();
12+
jsonObject.addProperty("browser", "chrome"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
13+
jsonObject.addProperty("browser_version", "latest");
14+
jsonObject.addProperty("os", "osx");
15+
jsonObject.addProperty("os_version", "catalina");
16+
jsonObject.addProperty("name", "Branded Google Chrome on Catalina");
17+
jsonObject.addProperty("build", "playwright-build-2");
18+
19+
PlaywrightParallelTest deviceOne = new PlaywrightParallelTest();
20+
deviceOne.executeTest(jsonObject);
21+
}
22+
}
23+
24+
class DeviceTwo implements Runnable {
25+
public void run() {
26+
JsonObject jsonObject = new JsonObject();
27+
jsonObject.addProperty("browser", "edge"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
28+
jsonObject.addProperty("browser_version", "latest");
29+
jsonObject.addProperty("os", "osx");
30+
jsonObject.addProperty("os_version", "catalina");
31+
jsonObject.addProperty("name", "Branded Microsoft Edge on Catalina");
32+
jsonObject.addProperty("build", "playwright-build-2");
33+
PlaywrightParallelTest deviceTwo = new PlaywrightParallelTest();
34+
deviceTwo.executeTest(jsonObject);
35+
}
36+
}
37+
38+
class DeviceThree implements Runnable {
39+
public void run() {
40+
JsonObject jsonObject = new JsonObject();
41+
jsonObject.addProperty("browser", "playwright-firefox"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
42+
jsonObject.addProperty("os", "osx");
43+
jsonObject.addProperty("os_version", "catalina");
44+
jsonObject.addProperty("name", "Playwright firefox on Catalina");
45+
jsonObject.addProperty("build", "playwright-build-2");
46+
PlaywrightParallelTest deviceThree = new PlaywrightParallelTest();
47+
deviceThree.executeTest(jsonObject);
48+
}
49+
}
50+
51+
class DeviceFour implements Runnable {
52+
public void run() {
53+
JsonObject jsonObject = new JsonObject();
54+
jsonObject.addProperty("browser", "playwright-webkit"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
55+
jsonObject.addProperty("os", "osx");
56+
jsonObject.addProperty("os_version", "catalina");
57+
jsonObject.addProperty("name", "Playwright webkit on Catalina");
58+
jsonObject.addProperty("build", "playwright-build-2");
59+
PlaywrightParallelTest deviceFour = new PlaywrightParallelTest();
60+
deviceFour.executeTest(jsonObject);
61+
}
62+
}
63+
64+
class DeviceFive implements Runnable {
65+
public void run() {
66+
JsonObject jsonObject = new JsonObject();
67+
jsonObject.addProperty("browser", "playwright-chromium"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
68+
jsonObject.addProperty("os", "osx");
69+
jsonObject.addProperty("os_version", "catalina");
70+
jsonObject.addProperty("name", "Chrome on Win10");
71+
jsonObject.addProperty("build", "playwright-build-2");
72+
PlaywrightParallelTest deviceFive = new PlaywrightParallelTest();
73+
deviceFive.executeTest(jsonObject);
74+
}
75+
}
76+
77+
public class PlaywrightParallelTest {
78+
79+
public static void main(String[] args) throws Exception {
80+
Thread threadOne = new Thread(new DeviceOne());
81+
threadOne.start();
82+
Thread threadTwo = new Thread(new DeviceTwo());
83+
threadTwo.start();
84+
Thread threadThree = new Thread(new DeviceThree());
85+
threadThree.start();
86+
Thread threadFour = new Thread(new DeviceFour());
87+
threadFour.start();
88+
Thread threadFive = new Thread(new DeviceFive());
89+
threadFive.start();
90+
}
91+
92+
public void executeTest( JsonObject jsonObject ) {
93+
jsonObject.addProperty("browserstack.username", "BROWSERSTACK_USERNAME");
94+
jsonObject.addProperty("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY");
95+
96+
try (Playwright playwright = Playwright.create()) {
97+
BrowserType chromium = playwright.chromium();
98+
String caps = URLEncoder.encode(jsonObject.toString(), "utf-8");
99+
String ws_endpoint = "wss://cdp.browserstack.com/playwright?caps=" + caps;
100+
Browser browser = chromium.connect(ws_endpoint);
101+
Page page = browser.newPage();
102+
page.navigate("https://www.google.co.in/");
103+
Locator locator = page.locator("[aria-label='Search']");
104+
locator.click();
105+
page.fill("[aria-label='Search']", "BrowserStack");
106+
page.locator("[aria-label='Google Search'] >> nth=0").click();
107+
String title = page.title();
108+
109+
Object result;
110+
if (title.equals("BrowserStack - Google Search")) {
111+
// 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
112+
result = page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"passed\", \"reason\": \"Title matched\"}}");
113+
System.out.println(result);
114+
} else {
115+
result = page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"failed\", \"reason\": \"Title did not matched\"}}");
116+
System.out.println(result);
117+
}
118+
browser.close();
119+
} catch (UnsupportedEncodingException e) {
120+
System.out.println(e);
121+
}
122+
}
123+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.browserstack;
2+
3+
import com.google.gson.JsonObject;
4+
import com.microsoft.playwright.*;
5+
import java.io.UnsupportedEncodingException;
6+
import java.net.URLEncoder;
7+
8+
public class PlaywrightSingleTest {
9+
public static void main(String[] args) {
10+
try (Playwright playwright = Playwright.create()) {
11+
JsonObject jsonObject = new JsonObject();
12+
jsonObject.addProperty("browser", "chrome"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
13+
jsonObject.addProperty("browser_version", "latest");
14+
jsonObject.addProperty("os", "osx");
15+
jsonObject.addProperty("os_version", "catalina");
16+
jsonObject.addProperty("name", "Playwright first single test");
17+
jsonObject.addProperty("build", "playwright-build-1");
18+
jsonObject.addProperty("browserstack.username", "BROWSERSTACK_USERNAME");
19+
jsonObject.addProperty("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY");
20+
21+
BrowserType chromium = playwright.chromium();
22+
String caps = URLEncoder.encode(jsonObject.toString(), "utf-8");
23+
String ws_endpoint = "wss://cdp.browserstack.com/playwright?caps=" + caps;
24+
Browser browser = chromium.connect(ws_endpoint);
25+
Page page = browser.newPage();
26+
page.navigate("https://www.google.co.in/");
27+
Locator locator = page.locator("[aria-label='Search']");
28+
locator.click();
29+
page.fill("[aria-label='Search']", "BrowserStack");
30+
page.locator("[aria-label='Google Search'] >> nth=0").click();
31+
String title = page.title();
32+
33+
if (title.equals("BrowserStack - Google Search")) {
34+
// 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
35+
Object result = page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"passed\", \"reason\": \"Title matched\"}}");
36+
System.out.println(result);
37+
} else {
38+
Object result = page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"failed\", \"reason\": \"Title did not matched\"}}");
39+
System.out.println(result);
40+
}
41+
browser.close();
42+
} catch (UnsupportedEncodingException e) {
43+
System.out.println(e);
44+
}
45+
}
46+
}

playwright-python/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Testing with playwright-browserstack in Python
2+
3+
[Playwright](https://playwright.dev/python/) Integration with BrowserStack.
4+
5+
![BrowserStack Logo](https://d98b8t1nnulk5.cloudfront.net/production/images/layout/logo-header.png?1469004780)
6+
7+
## Setup
8+
9+
* Clone the repo and run `cd playwright-python`
10+
* Make sure you have Python 3 (version < 3.9.0) installed on your system
11+
* Install Playwright
12+
```
13+
pip install --upgrade pip
14+
pip install playwright
15+
playwright install
16+
```
17+
18+
## Running your tests
19+
20+
- To run a single test, run `python single-playwright-test.py`
21+
- To run parallel tests, run `python parallel-playwright-test.py`
22+
23+
### Run sample test on privately hosted websites
24+
25+
1. You have to download the BrowserStack Local binary from the links below (depending on your environment):
26+
* [OS X (10.7 and above)](https://www.browserstack.com/browserstack-local/BrowserStackLocal-darwin-x64.zip)
27+
* [Linux 32-bit](https://www.browserstack.com/browserstack-local/BrowserStackLocal-linux-ia32.zip)
28+
* [Linux 64-bit](https://www.browserstack.com/browserstack-local/BrowserStackLocal-linux-x64.zip)
29+
* [Windows (XP and above)](https://www.browserstack.com/browserstack-local/BrowserStackLocal-win32.zip)
30+
2. Once you have downloaded and unzipped the file, you can initiate the binary by running the command: `./BrowserStackLocal --key YOUR_ACCESS_KEY`
31+
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.
32+
4. You can then run the sample Local test using `python parallel-playwright-tests.py`
33+
34+
Understand how many parallel sessions you need by using our [Parallel Test Calculator](https://www.browserstack.com/automate/parallel-calculator?ref=github)
35+
36+
37+
## Notes
38+
* You can view your test results on the [BrowserStack Automate dashboard](https://www.browserstack.com/automate)
39+
40+
## Additional Resources
41+
* [Documentation for writing Automate test scripts with BrowserStack](https://www.browserstack.com/docs/automate/playwright)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import json
2+
import urllib
3+
from playwright.sync_api import sync_playwright
4+
5+
desired_cap = {
6+
'browser': 'chrome', # allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
7+
'browser_version': 'latest', # this 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.
8+
'os': 'osx',
9+
'os_version': 'catalina',
10+
'name': 'Branded Google Chrome on Catalina',
11+
'build': 'playwright-build-3',
12+
'browserstack.local': 'true',
13+
'browserstack.username': 'BROWSERSTACK_USERNAME',
14+
'browserstack.accessKey': 'BROWSERSTACK_ACCESS_KEY'
15+
}
16+
17+
with sync_playwright() as playwright:
18+
cdpUrl = 'wss://cdp.browserstack.com/playwright?caps=' + urllib.parse.quote(json.dumps(desired_cap))
19+
browser = playwright.chromium.connect(cdpUrl)
20+
page = browser.new_page()
21+
try:
22+
page.goto("https://www.google.co.in/")
23+
page.fill("[aria-label='Search']", 'Browserstack')
24+
locator = page.locator("[aria-label='Google Search'] >> nth=0")
25+
locator.click()
26+
title = page.title()
27+
28+
if title == "Browserstack - Google Search":
29+
# 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
30+
page.evaluate("_ => {}", "browserstack_executor: {\"action\":\"setSessionStatus\",\"arguments\":{\"status\":\"passed\",\"reason\":\"Title matched\"}}");
31+
except:
32+
page.evaluate("_ => {}", "browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"failed\", \"reason\": \" Title did not match\"}}");
33+
34+
browser.close()
35+
36+

0 commit comments

Comments
 (0)