Skip to main content
Introducing support for Selenium 4 and its BiDi APIs on BrowserStack Automate! Learn More!

Selenium with NodeJS

Your guide to running Selenium Webdriver tests with NodeJS on BrowserStack.

Introduction

BrowserStack gives you instant access to our Selenium Grid of 3000+ real devices and desktop browsers. Running your Selenium tests with NodeJS on BrowserStack is simple. This guide will help you:

  1. Run your first test
  2. Mark tests as passed or failed
  3. 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.
  • Before you can start running your Selenium tests with Node JS, ensure the following libraries are installed:
      npm install -g selenium-webdriver
    

    If you have added any new package and added dependency on your test script, ensure to run npm update.

Run your first test

To get started, let’s run a simple Selenium Webdriver test. The NodeJS script below opens the bstackdemo.com website, adds a product to the cart, verifies whether the product has been added to the cart, and then marks the test as passed or failed based on whether the product is available in the cart.

Protip: Selenium 4 is now supported on BrowserStack. To use the Selenium 4 client bindings, modify your existing test scripts as follows:
  • 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 to 4.0.0.

Step 1 - Select the OS and device/browser combination

Select the OS and Device/Browser combination you’d like to test on using the drop-down menus below. This will automatically update the NodeJS code sample below:

Look for the icon to select a real device.

 
Warning: The driver.quit() statement is required, otherwise the test continues to execute, leading to a timeout.

Step 2 - Run the sample test code

Copy the code sample into your code editor, save it as sample-test.js file, and execute the test from your command line.

Command Line
node sample-test.js
Protip: You can use our capability builder and select from a wide range of custom capabilities that BrowserStack supports.

Step 3 - Verify the results

Once the test has run successfully, it is time to verify the results. The Selenium Webdriver test should have opened bstackdemo.com, added a product to the cart, verified whether the product has been added to the cart, and marked the test as passed or failed based on whether the product is available in the cart.

Note: Your results will be displayed on the command-line interface and on the Automate dashboard, where you can see Text Logs, Screenshots of every Selenium command, and a Video Recording of your entire test. (Read more about viewing test results and debugging failed tests)

Mark tests as passed or failed

BrowserStack does not know whether your test’s assertions have passed or failed because only the test script knows whether the assertions have passed. Therefore, based on the assertions on your script, you have to explicitly inform BrowserStack whether your tests have passed or not and this document will help you in doing that exactly.

It is possible to mark tests as either a pass or a fail and also give a reason for the same, using the following snippet (Refer the above sample script for complete script implementing this snippet):

// For marking test as passed
driver.executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed","reason": "Yaay! my sample test passed"}}');

// For marking test as failed
driver.executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "Oops! my sample test failed"}}');

You can refer the above sample script to learn implementation of this code snippet.

Note: The arguments passed in the JavaScript method for setting the status and the corresponding reason for the test are status and reason.
  • status accepts either passed or failed as the value
  • reason accepts a value in string datatype

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.

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 NodeJS script. 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:

var capabilities = {
  'browserstack.debug' : 'true'
}

Sample Visual Logs from Automate Dashboard: BrowserStack Automate Visual Logs

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.

In addition to these logs BrowserStack also provides Raw logs, Network logs, Console 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:

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

Is this page helping you?

Yes
No

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!

Talk to an Expert
Talk to an Expert