Skip to content

Commit a092893

Browse files
kamal-kaur04francisf
authored andcommitted
added local using bindings test for python and java
1 parent 667c6a3 commit a092893

File tree

8 files changed

+159
-4
lines changed

8 files changed

+159
-4
lines changed

playwright-java/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919

2020
### Run sample test on privately hosted websites
2121

22+
**Using Language Bindings**
23+
1. Run
24+
`mvn -Dexec.mainClass="com.browserstack.PlaywrightLocalUsingBindingsTest" -Dexec.classpathScope=test test-compile exec:java`
25+
26+
**Using Command-line Interface**
27+
2228
1. You have to download the BrowserStack Local binary from the links below (depending on your environment):
2329
* [OS X (10.7 and above)](https://www.browserstack.com/browserstack-local/BrowserStackLocal-darwin-x64.zip)
2430
* [Linux 32-bit](https://www.browserstack.com/browserstack-local/BrowserStackLocal-linux-ia32.zip)

playwright-java/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,11 @@
1919
<version>1.19.0</version>
2020
<scope>compile</scope>
2121
</dependency>
22+
<dependency>
23+
<groupId>com.browserstack</groupId>
24+
<artifactId>browserstack-local-java</artifactId>
25+
<version>1.0.6</version>
26+
<scope>compile</scope>
27+
</dependency>
2228
</dependencies>
2329
</project>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.browserstack;
2+
3+
import com.google.gson.JsonObject;
4+
import com.microsoft.playwright.*;
5+
import java.net.URLEncoder;
6+
import java.util.HashMap;
7+
8+
import com.browserstack.local.Local;
9+
10+
public class PlaywrightLocalUsingBindingsTest {
11+
public static void main(String[] args) {
12+
try (Playwright playwright = Playwright.create()) {
13+
JsonObject capabilitiesObject = new JsonObject();
14+
capabilitiesObject.addProperty("browser", "chrome"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
15+
capabilitiesObject.addProperty("browser_version", "latest");
16+
capabilitiesObject.addProperty("os", "osx");
17+
capabilitiesObject.addProperty("os_version", "catalina");
18+
capabilitiesObject.addProperty("name", "Playwright first local test");
19+
capabilitiesObject.addProperty("build", "playwright-java-3");
20+
capabilitiesObject.addProperty("browserstack.username", "BROWSERSTACK_USERNAME");
21+
capabilitiesObject.addProperty("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY");
22+
capabilitiesObject.addProperty("browserstack.local", "true");
23+
24+
//Creates an instance of Local
25+
Local bsLocal = new Local();
26+
27+
// You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY".
28+
HashMap<String, String> bsLocalArgs = new HashMap<String, String>();
29+
bsLocalArgs.put("key", "BROWSERSTACK_ACCESS_KEY");
30+
31+
// Starts the Local instance with the required arguments
32+
bsLocal.start(bsLocalArgs);
33+
34+
// Check if BrowserStack local instance is running
35+
System.out.println(bsLocal.isRunning());
36+
37+
BrowserType chromium = playwright.chromium();
38+
String caps = URLEncoder.encode(capabilitiesObject.toString(), "utf-8");
39+
String ws_endpoint = "wss://cdp.browserstack.com/playwright?caps=" + caps;
40+
Browser browser = chromium.connect(ws_endpoint);
41+
Page page = browser.newPage();
42+
try {
43+
page.navigate("https://www.google.co.in/");
44+
Locator locator = page.locator("[aria-label='Search']");
45+
locator.click();
46+
page.fill("[aria-label='Search']", "BrowserStack");
47+
page.locator("[aria-label='Google Search'] >> nth=0").click();
48+
String title = page.title();
49+
50+
if (title.equals("BrowserStack - Google Search")) {
51+
// 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
52+
markTestStatus("passed", "Title matched", page);
53+
} else {
54+
markTestStatus("failed", "Title did not match", page);
55+
}
56+
} catch (Exception err) {
57+
markTestStatus("failed", err.getMessage(), page);
58+
}
59+
browser.close();
60+
61+
//Stop the Local instance
62+
bsLocal.stop();
63+
} catch (Exception err) {
64+
System.out.println(err);
65+
}
66+
}
67+
68+
public static void markTestStatus(String status, String reason, Page page) {
69+
Object result;
70+
result = page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"" + status + "\", \"reason\": \"" + reason + "\"}}");
71+
System.out.println(result);
72+
}
73+
}

playwright-python/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121
- To run parallel tests, run `python parallel-playwright-test.py`
2222

2323
### Run sample test on privately hosted websites
24+
**Using Language Bindings**
25+
1. Follow the steps below:
26+
```
27+
pip install browserstack-local
28+
python local-using-bindings-playwright-test.py
29+
```
30+
31+
**Using Command-line Interface**
2432
2533
1. You have to download the BrowserStack Local binary from the links below (depending on your environment):
2634
* [OS X (10.7 and above)](https://www.browserstack.com/browserstack-local/BrowserStackLocal-darwin-x64.zip)

playwright-python/local-playwright-test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def run_local_session():
3636
mark_test_status("failed", "Title did not match", page)
3737

3838
except Exception as err:
39-
mark_test_status("failed", err, page)
39+
mark_test_status("failed", str(err), page)
4040

4141
browser.close()
4242

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import json
2+
import urllib
3+
import subprocess
4+
from playwright.sync_api import sync_playwright
5+
from browserstack.local import Local
6+
7+
desired_cap = {
8+
'browser': 'chrome', # allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
9+
'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.
10+
'os': 'osx',
11+
'os_version': 'catalina',
12+
'name': 'Branded Google Chrome on Catalina',
13+
'build': 'playwright-build-3',
14+
'browserstack.local': 'true',
15+
'browserstack.username': 'BROWSERSTACK_USERNAME',
16+
'browserstack.accessKey': 'BROWSERSTACK_ACCESS_KEY'
17+
}
18+
19+
def run_local_session(playwright):
20+
# Creates an instance of Local
21+
bs_local = Local()
22+
23+
# You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY".
24+
bs_local_args = { "key": "BROWSERSTACK_ACCESS_KEY" }
25+
26+
# Starts the Local instance with the required arguments
27+
bs_local.start(**bs_local_args)
28+
29+
# Check if BrowserStack local instance is running
30+
print(bs_local.isRunning())
31+
32+
clientPlaywrightVersion = str(subprocess.getoutput('playwright --version')).strip().split(" ")[1]
33+
desired_cap['client.playwrightVersion'] = clientPlaywrightVersion
34+
35+
cdpUrl = 'wss://cdp.browserstack.com/playwright?caps=' + urllib.parse.quote(json.dumps(desired_cap))
36+
browser = playwright.chromium.connect(cdpUrl)
37+
page = browser.new_page()
38+
try:
39+
page.goto("https://www.google.co.in/")
40+
page.fill("[aria-label='Search']", 'Browserstack')
41+
locator = page.locator("[aria-label='Google Search'] >> nth=0")
42+
locator.click()
43+
title = page.title()
44+
45+
if title == "Browserstack - Google Search":
46+
# 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
47+
mark_test_status("passed", "Title matched", page)
48+
else:
49+
mark_test_status("failed", "Title did not match", page)
50+
except Exception as err:
51+
mark_test_status("failed", str(err), page)
52+
53+
browser.close()
54+
55+
# Stop the Local instance
56+
bs_local.stop()
57+
58+
def mark_test_status(status, reason, page):
59+
page.evaluate("_ => {}", "browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\""+ status + "\", \"reason\": \"" + reason + "\"}}");
60+
61+
with sync_playwright() as playwright:
62+
run_local_session(playwright)

playwright-python/parallel-playwright-test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ def run_parallel_session(desired_cap):
7474
else:
7575
mark_test_status("failed", "Title did not match", page)
7676

77-
except Exception as error:
78-
mark_test_status("failed", error, page)
77+
except Exception as err:
78+
mark_test_status("failed", str(err), page)
7979

8080
browser.close()
8181

playwright-python/playwright-test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def run_session(playwright):
3434
else:
3535
mark_test_status("failed", "Title did not match", page)
3636
except Exception as err:
37-
mark_test_status("failed", err, page)
37+
mark_test_status("failed", str(err), page)
3838

3939
browser.close()
4040

0 commit comments

Comments
 (0)