Repository navigation
Expand file tree
/
Copy pathlocal-using-bindings-playwright-test.py
More file actions
63 lines (54 loc) · 2.62 KB
/
Copy pathlocal-using-bindings-playwright-test.py
File metadata and controls
63 lines (54 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import json
import urllib
import subprocess
from playwright.sync_api import sync_playwright
from browserstack.local import Local
desired_cap = {
'browser': 'chrome', # allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
'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.
'os': 'osx',
'os_version': 'catalina',
'name': 'Branded Google Chrome on Catalina',
'build': 'playwright-python-3',
'browserstack.local': 'true',
'browserstack.username': 'BROWSERSTACK_USERNAME',
'browserstack.accessKey': 'BROWSERSTACK_ACCESS_KEY'
}
def run_local_session(playwright):
# Creates an instance of Local
bs_local = Local()
# You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY".
bs_local_args = { "key": "BROWSERSTACK_ACCESS_KEY" }
# Starts the Local instance with the required arguments
bs_local.start(**bs_local_args)
# Check if BrowserStack local instance is running
print("BrowserStackLocal running: " + str(bs_local.isRunning()))
clientPlaywrightVersion = str(subprocess.getoutput('playwright --version')).strip().split(" ")[1]
desired_cap['client.playwrightVersion'] = clientPlaywrightVersion
try:
cdpUrl = 'wss://cdp.browserstack.com/playwright?caps=' + urllib.parse.quote(json.dumps(desired_cap))
browser = playwright.chromium.connect(cdpUrl)
page = browser.new_page()
try:
page.goto("http://localhost:45691")
page.main_frame.wait_for_function("""
document
.querySelector("body")
.innerText
.includes("This is an internal server for BrowserStack Local")
""")
# 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
mark_test_status("passed", "Local is up and running", page)
except Exception:
mark_test_status("failed", "BrowserStack Local binary is not running", page)
browser.close()
except Exception as ex:
print("Exception while creating page context: ", str(ex))
finally:
# Stop the Local instance
bs_local.stop()
print("BrowserStackLocal stopped")
def mark_test_status(status, reason, page):
page.evaluate("_ => {}", "browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\""+ status + "\", \"reason\": \"" + reason + "\"}}");
with sync_playwright() as playwright:
run_local_session(playwright)