Integrate Your Test Suite with BrowserStack
This section will help you migrate your existing test suite to run on BrowserStack Automate. It also covers key features and best practice recommendations for smooth integration.
Setup authentication
Step 1: Set environment variables for BrowserStack credentials
# Set these values in your ~/.zprofile (zsh) or ~/.profile (bash)
export BROWSERSTACK_USERNAME="YOUR_USERNAME"
export BROWSERSTACK_ACCESS_KEY="YOUR_ACCESS_KEY"
# setx.exe does not set the environment variable in the current command prompt, but it will be available in subsequent command prompts
setx BROWSERSTACK_USERNAME "YOUR_USERNAME"
setx BROWSERSTACK_ACCESS_KEY "YOUR_ACCESS_KEY"
# Verify whether the variables have been set
echo BROWSERSTACK_USERNAME
echo BROWSERSTACK_ACCESS_KEY
Alternatively, you can also add your BrowserStack credentials in the test cases as shown in the following example:
desired_cap = {
'browserstack.username': 'BROWSERSTACK_USERNAME',
'browserstack.accessKey': 'BROWSERSTACK_ACCESS_KEY'
}
In the previous section, we set up BrowserStack credentials directly in the test script and the sample build. However, for a production-grade integration we recommend you store your credentials as environment variables and use those environment variables in your code.
Step 2: Connect CDP endpoint
Connect to the CDP endpoint at BrowserStack as shown in the following example:
def run_session(playwright):
clientPlaywrightVersion = str(subprocess.getoutput('playwright --version')).strip().split(" ")[1]
desired_cap['client.playwrightVersion'] = clientPlaywrightVersion
cdpUrl = 'wss://cdp.browserstack.com/playwright?caps=' + urllib.parse.quote(json.dumps(desired_cap))
browser = playwright.chromium.connect(cdpUrl)
page = browser.new_page()
Migrate your test cases
This section helps you with all the configuration changes, commonly used features, and best practices for a smooth migration of your existing test cases to BrowserStack.
Step 1: Run test suite on a single browser
We recommend running your build using a single browser like Chrome, WebKit, or Firefox to begin with. This will isolate issues during the migration phase and help with faster debugging. Refer the capabilities as shown to use Chrome.
Once you’ve migrated your test cases or have achieved stability with Chrome or Firefox, you can set up cross-browser testing.
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',
}
Step 2: Organize tests
Naming your tests and builds properly is crucial for effective debugging, test reporting, and analyzing your build execution time. Here are the capabilities you can use.
Capability | Description |
---|---|
sessionName |
Name for your test case. Example, Homepage - Get started |
buildName |
CI/CD job or build name. Example, Website build #23, staging_1.3.27 |
projectName |
Name of your project. Example, Marketing Website |
desired_cap = {
'name': 'Branded Google Chrome on Catalina',
'build': 'playwright-python-1',
'project': 'My playwright project',
}
Step 3: Mark test as passed or failed
To mark whether your test has passed or failed on BrowserStack, use the following Javascript executor in your test script. You can mark a test as passed
or failed
based on your test assertions.
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
mark_test_status("<status>", "<reason>", page)
else:
mark_test_status("<status>", "<reason>", page)
The arguments passed in the Javascript method for setting the status and the corresponding reason of the test are status and reason:
-
status
accepts eitherpassed
orfailed
as the value -
reason
accepts a value in string datatype
passed
or failed
.
Step 4: Set up debugging capabilities
BrowserStack provides the following debugging capabilities for your tests:
- Use visual logs capability to capture screenshots at every Playwright command automatically. You can enable visual logs by using the
browserstack.debug
capability. - Console Logs with log level ‘errors’ are enabled by default. You can enable different log levels viz.
warnings
,info
,verbose
,errors
, anddisable
using thebrowserstack.console
capability. - Network Logs capture the browser’s performance data such as network traffic, latency, HTTP requests, and responses in a HAR format. You can enable network logs using the
browserstack.networkLogs
capability.
Enable these debugging capabilities as shown in the following code example:
capabilities = {
"browserstack.debug" : "true",
"browserstack.console" : "disable"
"browserstack.networkLogs" : "true",
"browserstack.networkLogsOptions": {
"captureContent": "true"
}
Connect your website under test
BrowserStack can integrate with test suites pointing to your localhost URL, staging environment, and even websites behind one or more proxies/firewalls. To test using localhost, perform the following steps:
Step 1: Install the browserstack-local
package
pip install browserstack-local
Step 2: Set the access key and use available methods in your test script
# Creates an instance of Local
bs_local = new Local();
# You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY".
bs_local_args = { "key": "YOUR_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()))
# Your test code goes here, from creating the driver instance till the end, i.e. $bs_local->stop()
# Stop the Local instance
bs_local.stop()
Set the bs_local_args
variable to your BrowserStack Access key and and use the following methods provided by the local library to manage your local connection:
Method | Description |
---|---|
bs_local.start() |
Expects bs_local object. Returns a callback when the tunnel has started successfully. Your test script should start executing after this callback has been invoked. |
bs_local.stop() |
Call this method after your test suite is complete. |
bs_local.isRunning() |
Check if the BrowserStack local instance is running. |
Step 3: Add the browserstack.local
capability
desired_cap = {
'browserstack.local': 'true',
}
Copy and set the browserstack.local
capability to true
in your test script. You may face errors running your test script if any other capability is enabled before setting up browserstack.local
.
Step 4: Run a test using Browserstack Local
Execute the command to run your local test.
Step 1: Download BrowserStack Local
mac OS
Windows (XP and above)
Linux(64 bit)
Linux(32 bit)
Step 2: Unzip the binary Unzip the downloaded file and move it to a folder/directory from which you have permission to start it using your command line or terminal.
Step 3: Run the binary using your command line or terminal
Copy the following command to initiate the BrowserStack Local connection:
# Run this command in your terminal to start the BrowserStack Local binary. Your working directory should be where you have the downloaded binary.
./BrowserStackLocal --key <YOUR_ACCESS_KEY>
Step 4: Set up configuration to enable browserstack.local
Copy the capabilities as shown into your configuration file:
desired_cap = {
'browserstack.local': 'true',
Next Steps
- Run your first test
- Learn how to test localhost and staging websites
- Set browser options
- Select Playwright version
- Run test from behind a proxy
We're sorry to hear that. Please share your feedback so we can do better
Contact our Support team for immediate help while we work on improving our docs.
We're continuously improving our docs. We'd love to know what you liked
We're sorry to hear that. Please share your feedback so we can do better
Contact our Support team for immediate help while we work on improving our docs.
We're continuously improving our docs. We'd love to know what you liked
Thank you for your valuable feedback!