Selenium with Protractor
A guide to run Selenium Webdriver tests with Protractor on BrowserStack.
- Selenium-based
- Other Frameworks
Introduction
BrowserStack gives you instant access to our Selenium Grid of 3000+ real devices and desktop browsers. Running your Selenium tests with Protractor on BrowserStack is simple. This guide will help you:
- Run your first test
- Integrate your tests with BrowserStack
- Mark tests as passed or failed
- Debug your app
Prerequisites
- You need to have BrowserStack Username and Access key, which you can find in your account settings. If you have not created an account yet, you can sign up for a Free Trial or purchase a plan.
Run your first test
- Edit or add capabilities in the W3C format using our W3C capability generator.
- Add the
seleniumVersion
capability in your test script and set the value to4.0.0
.
To run your first Protractor test on BrowserStack, follow the steps below:
Step 1: Clone the protractor-browserstack sample repo on GitHub using:
git clone https://github.com/browserstack/protractor-browserstack.git
cd protractor-browserstack
Step 2: Install the required dependencies by running the following command in your command line:
npm install
Step 3: Setup your credentials in the protractor-browserstack/conf/parallel.conf.js
file as shown below:
exports.config = {
...
'browserstackUser': process.env.BROWSERSTACK_USERNAME || 'BROWSERSTACK_USERNAME',
'browserstackKey': process.env.BROWSERSTACK_ACCESS_KEY || 'BROWSERSTACK_ACCESS_KEY',
...
}
Alternatively, you can set the environment variables in your system as shown below:
# 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
Step 4: Run your first test using the following command:
npm run parallel
You can visit BrowserStack Automate Dashboard and see your test there once it has successfully completed.
Details of your first test
The specs for the test that you just ran can be found in protractor-browserstack/specs/single.js
. The test case below searches for the string “BrowserStack” on Google, and checks if the title of the resulting page is “BrowserStack - Google Search”:
describe('Google\'s Search Functionality', function() {
it('can find search results', function() {
browser.driver.get('https://google.com/ncr').then(function() {
browser.driver.findElement(by.name('q')).sendKeys('BrowserStack\n').then(function() {
expect(browser.driver.getTitle()).toEqual('BrowserStack - Google Search');
});
});
});
});
Integrate your tests with BrowserStack
In the sample repository, you can find conf/parallel.conf.js
file which is responsible for configuring your test to run on BrowserStack. The useful sections of the file are shown below which enable the tests to run on BrowserStack:
exports.config = {
'specs': [ '../specs/single.js' ],
'browserstackUser': process.env.BROWSERSTACK_USERNAME || 'BROWSERSTACK_USERNAME',
'browserstackKey': process.env.BROWSERSTACK_ACCESS_KEY || 'BROWSERSTACK_ACCESS_KEY',
'commonCapabilities': {
'build': 'browserstack-build-1',
'name': 'parallel_test',
'browserstack.debug': 'true',
'browserName': 'Chrome'
},
'multiCapabilities': [{
'browserName': 'Chrome'
},{
'browserName': 'Safari'
},{
'browserName': 'Firefox'
},{
'browserName': 'IE'
}],
// Code to mark the status of test on BrowserStack based on test assertions
onComplete: function (passed) {
if (!passed) {
browser.executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "At least 1 assertion has failed"}}');
}
if (passed) {
browser.executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed","reason": "All assertions passed"}}');
}
}
};
// Code to support common capabilities
exports.config.multiCapabilities.forEach(function(caps){
for(var i in exports.config.commonCapabilities) caps[i] = caps[i] || exports.config.commonCapabilities[i];
});
Mark tests as passed or failed
BrowserStack does not know whether your test’s assertions have passed or failed because only the framework knows whether the assertions have passed.
In the parallel.conf.js
file that is used to run your Protractor tests, the following snippet is used to mark tests as passed or failed depending on the assertion status of your tests:
onComplete: function (passed) {
if (!passed) {
browser.executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "At least 1 assertion has failed"}}');
}
if (passed) {
browser.executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed","reason": "All assertions passed"}}');
}
}
The above onComplete
function is invoked after every Protractor test that is executed. Based on the status of the assertions, a javascript executor is fired which marks the status of your test on BrowserStack.
Marking test as pass/fail is also possible using our REST API at any point in the test or also after the test has concluded. You can read more about marking test using REST API and use it if it fits your use case.
Debug your app
BrowserStack provides a range of debugging tools to help you quickly identify and fix bugs you discover through your automated tests. Learn more about how to debug tests on BrowserStack using the Automate Dashboard.
Text logs
Text Logs are a comprehensive record of your test. They are used to identify all the steps executed in the test and troubleshoot errors for the failed step. Text Logs are accessible from the Automate dashboard or via our REST API.
Visual logs
Visual Logs automatically capture the screenshots generated at every Selenium command run through your Protractor tests. Visual logs help with debugging the exact step and the page where failure occurred. They also help identify any layout or design related issues with your web pages on different browsers.
Visual Logs are disabled by default. In order to enable Visual Logs you will need to set browserstack.debug
capability to true
.
'capabilities': {
'browserstack.debug': 'true'
}
Video recording
Every test run on the BrowserStack Selenium grid is recorded exactly as it is executed on our remote machine. This feature is particularly helpful whenever a browser test fails. You can access videos from Automate Dashboard for each session. You can also download the videos from the Dashboard or retrieve a link to download the video using our REST API.
browserstack.video
capability to false
.
Console logs
Console Logs capture the browser’s console output at various steps of the test to troubleshoot javascript issues. You can retrieve Console Logs using an URL that you can get from our REST API. You will also be able to download logs from Automate Dashboard.
Console Logs are enabled with log level set to ‘errors’ by default. To set different log levels, you need to use the capability browserstack.console
with values disable
, errors
, warnings
, info
or verbose
, as shown below:
'capabilities': {
'browserstack.console': 'errors' // You can choose the log-level here
}
Network logs
Network Logs capture the browser’s performance data such as network traffic, latency, HTTP requests and responses in the HAR format. You can download network logs using a link that you can get from our REST API or from the Automate Dashboard. You can visualize HAR files using the HAR Viewer.
Network Logs are disabled by default. To enable Network Logs use the capability browserstack.networkLogs
with the value true
, as shown below:
'capabilities': {
'browserstack.networkLogs': 'true'
}
In addition to these logs BrowserStack also provides Raw logs, Selenium logs, Appium logs and Interactive session. You can find the complete details to enable all the debugging options.
Next steps
Once you have successfully run your first test on BrowserStack, you might want to do one of the following:
- Test privately hosted websites
- Run multiple tests in parallel
- Generate a list of capabilities that you want to use in tests
- Find information about your Projects, Builds and Sessions using our REST APIs
- Set up your CI/CD: Jenkins, Bamboo, TeamCity, Azure, CircleCI, BitBucket, TravisCI, GitHub Actions
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!