Skip to main content

Test on Internal Networks

Run Mocha tests on your privately hosted websites using BrowserStack Automate.

Note: Code samples in this guide can be found in the mocha-browserstack sample repo on GitHub.

Introduction

BrowserStack enables you to run your Mocha automated tests on your internal development environments, on localhost, and from behind a corporate firewall. This feature is called “Local Testing”.

Local Testing establishes a secure connection between your machine and the BrowserStack cloud. Once you set up Local Testing, all URLs work out of the box, including HTTPS URLs and those behind a proxy or firewall.

In this guide, you will learn about:

  1. Prerequisites
  2. Running your first local test

Prerequisites

If you have already run your first test, you can skip the prerequisites.

Run your first local test

Configure your Mocha tests for Local Testing using the following steps:\

  1. Clone the mocha-browserstack sample repo on GitHub using the following command:

    git clone https://github.com/browserstack/mocha-browserstack.git
    cd mocha-browserstack
    
  2. Open the cloned project in the IDE of your choice and install the required dependencies by running the following command in your command line:

     npm install
    
  3. Verify that the browserstack-local dependency is added in the package.json file:

    package.json
     "dependencies": {
     "browserstack-local": "^1.4.8"
      }
    
  4. Update your Mocha config file, mocha-browserstack/conf/local.conf.js, in your sample repo to set the browserstack.local capability to true:

    exports.capabilities = {
     'browserstack.user': 'YOUR_USERNAME',
     'browserstack.key': 'YOUR_ACCESS_KEY',
     'build': 'mocha-browserstack',
     'name': 'single_test',
     'browserstack.debug': 'true',
    
     'browserName': 'chrome',
     'browserstack.local': true
    };
    

    You can use the following sample test case local_test.js in the mocha-browserstack/specs/local_test.js directory to verify if your Local test connection has been set up correctly:

     describe('BrowserStack Local Testing for ' + caps.browserName, function() {
     this.timeout(0);
     var driver, bsLocal;
    
     beforeEach(function(done) {
       bsLocal = new browserstack.Local();
       bsLocal.start({ 'key': caps['browserstack.key'] }, function(error) {
         if (error) done(error);
         driver = buildDriver(caps);
         done();
       });
     });
    
     it('check tunnel is working', function (done) {
       driver.get('http://bs-local.com:45691/check').then(function() {
         driver.getPageSource().then(function(source) {
           assert(source.match(/Up and running/i) != null);
           done();
         });
       });
     });
    
  5. Run your Mocha test using BrowserStack Local with the following command:

     npm run local
    
  6. View your tests on BrowserStack on the BrowserStack Automate Dashboard.
Protip: You can use our capability builder and select from a wide range of custom capabilities that BrowserStack supports.

Next steps

After you have successfully run your first test using BrowserStack Local, you can explore the following sections:

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