Nightwatch + Browserstack
Tags: testing, javascript, nightwatchjs, vue
Published: 2019-11-24 13:04:50

---
title: Nightwatch + Browserstack
published: true
date: 2018-10-18 17:56:34 UTC
tags: testing,javascript,nightwatchjs,vuejs
canonical_url: 
---

### Vue cli 2: Nightwatch + Browserstack

Recently, I had to configure the Nightwatch test we have on our Vue.js project to be able to run on Browserstack automated testing platform. This had to work for different environments. We have two main environments: test and prods.

The configuration we wanted to achieve was to be able to run the e2e test on:

- localhost using local selenium and Chromedriver.
- test/prod URLs using local selenium and Chromedriver
- test/prod URLs uring Browserstack

The final files are at the end of this article which you can paste from.

### Initial setup

Our application is using Vue.js and was generated with vue cli 2 using the webpack template. We have a runner.js file, which is used to set up webpack, to be able to run the test on localhost. We will have to update this runner.js file and the nightwatch config.

### Configuring Browserstack to only run on test/prod environments

Browserstack has [a nice document](https://www.browserstack.com/automate/nightwatch) on how to run Nightwatch test suite with them. The only problem is they assume you want to run on Browserstack all the time and do not show examples to have it configurable. What if you want to use a local version of Selenium and Chrome? It also gets more complex when you have to use [Browserstack local](https://www.npmjs.com/package/browserstack-local) to be able to access internal URLs, which was our case.

In order to run Browserstack local, you need to use a specific setup, which is set in a different runner file in their documentation.

#### 1. Runner.js updates

In the runner, to use Browserstack local, we made it configurable using the command line node environment variable. We are expecting a variable we called RUNNER to be present, to specify which runner to use: local or Browserstack.

If the test needs to be run on Browserstack, we set up the runner to use Browserstack local. If not, we use the default configuration to set up the server, which is the same code provided by the Browserstack documentation.

![](https://cdn-images-1.medium.com/max/1024/1*s-EoAc7HQIkAXdI4lreaWg.png)<figcaption>Using Browserstack local when running the test on Browserstack</figcaption>

#### 2. Nightwatch config updates

Our config by default is using the local selenium. If the node environment variable RUNNERis provided to execute test on browserstack, we are overriding those settings to use Browserstack selenium (similar to the Browserstack documentation).

![](https://cdn-images-1.medium.com/max/1024/1*pWI24Jnwl42e5xvNMNfxVA.png)<figcaption>Overriding Nightwatch config to use Browserstack selenium</figcaption>

#### 3. Update test status in Browserstack through API

By default, when executing the test, Browserstack won’t be able to show if a test failed. The only way to update this is by making a request to [Browserstack API.](https://www.browserstack.com/automate/rest-api#rest-api-sessions)

The request needs the session ID, which is available in the test or in the global afterEach executed at the end of each test suite. In the nightwatch config file, you can set this in globals property. If the test failed, we make a request to set them to fail. We also update the name to show of the session to display which environment the test was executed on: test or prod.

![](https://cdn-images-1.medium.com/max/1024/1*hn7BbaMflgsG4EWBxLHA4A.png)<figcaption>Setting test session in Browserstack as failure or success</figcaption>

### Final files

{% gist https://gist.github.com/atrunelle/870c36052d29797f8c565f23c4a0df80.js %}

{% gist https://gist.github.com/atrunelle/193a3ee34395df647d9fff3c084520f6 %}

Thanks for reading, I hope this is helpful. I am planning on looking at how to do the same using Vue CLI 3 and write an article too. I will add a link here when it happens.

Update: here is the article with the Vue cli 3: [https://medium.com/@digitaledawn/vue-cli-3-nightwatch-browserstack-76c61a0fbb79](https://medium.com/@digitaledawn/vue-cli-3-nightwatch-browserstack-76c61a0fbb79)