Selenium with CodeceptJS + WebdriverIO
Learn how to run your first Selenium Webdriver test with CodeceptJS + WebdriverIO on BrowserStack Automate.
Introduction
BrowserStack gives you instant access to our Selenium Grid of 3000+ real devices and desktop browsers. Running your Selenium tests with CodeceptJS + WebdriverIO on BrowserStack is simple.
In this guide, you will learn about:
- Prerequisites
- Running your first test
- Understanding the details of your test
- Marking tests as passed or failed
- Debugging your app
Prerequisites
- 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.
- Node.js version 12 or higher is installed on your machine.
- Git installed on your machine.
- NPM installed on your machine.
Run your first test
To run your first CodeceptJS + WebdriverIO test on BrowserStack, follow the steps below:
Step 1: Clone the codecept-js-browserstack sample repo on GitHub using:
git clone https://github.com/browserstack/codecept-js-browserstack
cd codecept-js-browserstack
Step 2: Install the required dependencies by running the following command in your command line:
npm install
Step 3: Set your BrowserStack credentials in the codecept-js-browserstack/src/test/parallel/codecept.conf.js
file as follows:
require('dotenv').config()
const BROWSERSTACK_USERNAME = process.env.BROWSERSTACK_USERNAME
const BROWSERSTACK_ACCESS_KEY = process.env.BROWSERSTACK_ACCESS_KEY
exports.config = {
tests: './*_test.js',
output: './output',
helpers: {
WebDriver: {
url: 'https://bstackdemo.com',
user: BROWSERSTACK_USERNAME,
key: BROWSERSTACK_ACCESS_KEY,
browser: 'chrome',
desiredCapabilities: {}
}
},
multiple: {
bstack: {
browsers: [
{
browser: "Safari",
//Mentioned below are the capabilities based on JSON Wire Protocol
desiredCapabilities: {
"os": "OS X",
"os_version": "Catalina",
"browser_version": "latest",
"project": "Codecept + WebdriverIO",
"build": "Parallel_Execution",
"name": "Parallel Test Safari",
"browserstack.debug": "true",
'browserstack.networkLogs': 'true',
},
//For W3C-based scripts, use the following capabilties:
/*
desiredCapabilities: {
"bstack:options" : {
"os": "OS X",
"osVersion": "Catalina",
"projectName": "Codecept + WebdriverIO",
"buildName": "Parallel_Execution",
"sessionName": "Parallel Test Safari",
"debug" : "true",
"networkLogs" : "true",
},
"browserVersion": "latest",
},
*/
},
{
browser: "Firefox",
//Mentioned below are the capabilities based on JSON Wire Protocol
desiredCapabilities: {
"os": "Windows",
"os_version": "10",
"browser_version": "latest",
"project": "Codecept + WebdriverIO",
"build": "Parallel_Execution",
"name": "Parallel Test Firefox",
"browserstack.debug": "true",
'browserstack.networkLogs': 'true',
},
//For W3C-based scripts, use the following capabilties:
/*
desiredCapabilities: {
"bstack:options" : {
"os": "Windows",
"osVersion": "10",
"projectName": "Codecept + WebdriverIO",
"buildName": "Parallel_Execution",
"sessionName": "Parallel Test Firefox",
"debug" : "true",
"networkLogs" : "true",
},
"browserVersion": "latest",
},
*/
},
],
},
},
include: {
I: './steps_file.js'
},
bootstrap: null,
mocha: {},
name: 'CodeceptJS-BrowserStack',
plugins: {
pauseOnFail: {},
retryFailedStep: {
enabled: true
},
tryTo: {
enabled: true
},
screenshotOnFail: {
enabled: true
}
}
}
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"
Step 4: Run your first test using the following command:
npm run parallel
Step 5: View your tests on BrowserStack on the BrowserStack Automate Dashboard.
Understand the details of your test
Learn about how the different components of the test suite work together using the following information.
Understand your first test script
When you run the npm run parallel
command, the parallel_test.js
file within the codecept-js-browserstack/src/test/parallel
directory is executed.
When the test is triggered, it:
- Opens the
www.bstackdemo.com
website. - Adds a product to the cart.
- Verifies whether the product is added to the cart.
- Marks the test as passed or failed based on whether the product is available in the cart.
Feature('Add to Cart');
Scenario('Single Test', async ({ I }) => {
I.amOnPage("https://bstackdemo.com")
let expectedProductName = await I.grabTextFrom('//*[@id="1"]/p')
I.click('//*[@id="1"]/div[4]')
let productName = await I.grabTextFrom('//*[@id="__next"]/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]')
if (productName === expectedProductName) {
I.executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed","reason": "Product matched!"}}')
}
else {
I.executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "Product Did Not Match!"}}')
}
});
Understand the CodeceptJS configuration file
In the sample repository, the configuration file named codecept.conf.js
exists in the src/test/parallel
directory. It is preloaded with default configuration for connecting tests to BrowserStack, and sample capabilities, such as browser name and its version, for running tests.
Ensure that you set your BrowserStack credentials in this file.
require('dotenv').config()
const BROWSERSTACK_USERNAME = process.env.BROWSERSTACK_USERNAME
const BROWSERSTACK_ACCESS_KEY = process.env.BROWSERSTACK_ACCESS_KEY
exports.config = {
tests: './*_test.js',
output: './output',
helpers: {
WebDriver: {
url: 'https://bstackdemo.com',
user: BROWSERSTACK_USERNAME,
key: BROWSERSTACK_ACCESS_KEY,
browser: 'Edge',
//Mentioned below are the capabilities based on JSON Wire Protocol
capabilities: {
"os": "Windows",
"os_version": "10",
"browser_version": "latest",
"project": "Codecept + WebdriverIO",
"build": "Parallel_Execution",
"name": "Parallel Test Edge",
"browserstack.debug": "true",
'browserstack.networkLogs': 'true',
}
// For W3C-based scripts, use the following capabilties:
/*
capabilities: {
"bstack:options" : {
"os": "Windows",
"osVersion": "10",
"projectName": "Codecept + WebdriverIO",
"buildName": "Parallel_Execution",
"sessionName": "Parallel Test Edge",
"debug" : "true",
"networkLogs" : "true",
},
"browserVersion": "latest",
}
*/
}
},
include: {
I: './steps_file.js'
},
bootstrap: null,
mocha: {},
name: 'CodeceptJS-BrowserStack',
plugins: {
pauseOnFail: {},
retryFailedStep: {
enabled: true
},
tryTo: {
enabled: true
},
screenshotOnFail: {
enabled: true
}
}
}
capabilities
section of codecept.conf.js
file.
Integrate your tests with BrowserStack
The capabilities and browser-related information in the configuration file is used to run the tests on BrowserStack. The the tests
key references the parallel_test.js
test file as shown below:
exports.config = {
tests: './*_test.js',
output: './output',
...
Understand the dependencies
The required dependencies to run all the tests in the repository are added to the package.json
file as follows:
"dependencies": {
"browserstack-local": "^1.4.8",
"dotenv": "^16.0.0"
}
Understand the command to run parallel test
The test and configuration file to consider for running parallel test is specified in the package.json
file, as shown below:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"parallel": "npx codeceptjs run -c src/test/parallel/codecept.conf.js --steps",
...
},
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 test file that is used to run your Codecept.js tests, the following snippet is used to mark tests as passed or failed depending on the assertion status of your tests:
if(productName === expectedProductName){
I.executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed","reason": "Product matched!"}}')
}
else{
I.executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "Product Did Not Match!"}}')
}
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 CodeceptJS + WebdriverIO 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.
Note: Video recording increases test execution time slightly. You can disable this feature by setting the 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': <log-level>
}]
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
}]
Next steps
After you have successfully run your first test on BrowserStack, you might want to do one of the following:
- Test on private websites that are hosted on your internal networks
- Run multiple tests in parallel to speed up the build execution
- 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!