Skip to content

Commit 09336e2

Browse files
asambstackfrancisf
authored andcommitted
fix: browserstack local scripts for python and java
1 parent a67df91 commit 09336e2

File tree

4 files changed

+36
-49
lines changed

4 files changed

+36
-49
lines changed

playwright-java/src/test/java/com/browserstack/PlaywrightLocalTest.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,10 @@ public static void main(String[] args) {
2424
Browser browser = chromium.connect(ws_endpoint);
2525
Page page = browser.newPage();
2626
try {
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();
27+
page.navigate("http://localhost:45454");
3228
String title = page.title();
3329

34-
if (title.equals("BrowserStack - Google Search")) {
30+
if (title.equals("BrowserStack Local")) {
3531
// 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
3632
markTestStatus("passed", "Title matched", page);
3733
} else {

playwright-java/src/test/java/com/browserstack/PlaywrightLocalUsingBindingsTest.java

+8-14
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,15 @@ public static void main(String[] args) {
4040
Browser browser = chromium.connect(ws_endpoint);
4141
Page page = browser.newPage();
4242
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-
}
43+
page.navigate("http://localhost:45691");
44+
page.waitForFunction("document" +
45+
".querySelector(\"body\")" +
46+
".innerText" +
47+
".includes(\"This is an internal server for BrowserStack Local\")");
48+
49+
markTestStatus("passed", "Local is up and running", page);
5650
} catch (Exception err) {
57-
markTestStatus("failed", err.getMessage(), page);
51+
markTestStatus("failed", "BrowserStack Local binary is not running", page);
5852
}
5953
browser.close();
6054

playwright-python/local-playwright-test.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,18 @@
1515
'browserstack.accessKey': 'BROWSERSTACK_ACCESS_KEY'
1616
}
1717

18-
def run_local_session():
18+
def run_local_session(playwright):
1919
clientPlaywrightVersion = str(subprocess.getoutput('playwright --version')).strip().split(" ")[1]
2020
desired_cap['client.playwrightVersion'] = clientPlaywrightVersion
2121

2222
cdpUrl = 'wss://cdp.browserstack.com/playwright?caps=' + urllib.parse.quote(json.dumps(desired_cap))
2323
browser = playwright.chromium.connect(cdpUrl)
2424
page = browser.new_page()
2525
try:
26-
page.goto("https://www.google.co.in/")
27-
page.fill("[aria-label='Search']", 'Browserstack')
28-
locator = page.locator("[aria-label='Google Search'] >> nth=0")
29-
locator.click()
26+
page.goto("http://localhost:45454")
3027
title = page.title()
3128

32-
if title == "Browserstack - Google Search":
29+
if title == "BrowserStack Local":
3330
# 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
3431
mark_test_status("passed", "Title matched", page)
3532
else:

playwright-python/local-using-bindings-playwright-test.py

+23-23
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,33 @@ def run_local_session(playwright):
2727
bs_local.start(**bs_local_args)
2828

2929
# Check if BrowserStack local instance is running
30-
print("BrowserStackLocal running: " + bs_local.isRunning())
30+
print("BrowserStackLocal running: " + str(bs_local.isRunning()))
3131

3232
clientPlaywrightVersion = str(subprocess.getoutput('playwright --version')).strip().split(" ")[1]
3333
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()
34+
3835
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()
36+
cdpUrl = 'wss://cdp.browserstack.com/playwright?caps=' + urllib.parse.quote(json.dumps(desired_cap))
37+
browser = playwright.chromium.connect(cdpUrl)
38+
page = browser.new_page()
39+
try:
40+
page.goto("http://localhost:45691")
41+
page.main_frame.wait_for_function("""
42+
document
43+
.querySelector("body")
44+
.innerText
45+
.includes("This is an internal server for BrowserStack Local")
46+
""")
47+
mark_test_status("passed", "Local is up and running", page)
48+
except Exception:
49+
mark_test_status("failed", "BrowserStack Local binary is not running", page)
50+
browser.close()
51+
except Exception as ex:
52+
print("Exception while creating page context: ", str(ex))
53+
finally:
54+
# Stop the Local instance
55+
bs_local.stop()
56+
print("BrowserStackLocal stopped")
5757

5858
def mark_test_status(status, reason, page):
5959
page.evaluate("_ => {}", "browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\""+ status + "\", \"reason\": \"" + reason + "\"}}");

0 commit comments

Comments
 (0)