Skip to main content

Test on Internal Networks

BrowserStack enables you to run your CodeceptJS 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.

Prerequisites

BrowserStack Username and Access key, which you can find in your account profile. If you have not created an account yet, you can sign up for a Free Trial or purchase a plan.

Run your first Local test

Complete the following steps to run a CodeceptJS test script using Local Testing:

  1. Clone the sample codecept-js-selenium-browserstack repository using the following command:
        git clone https://github.com/browserstack/codecept-js-browserstack
        cd codecept-js-browserstack
    
  2. Run the following command in your command-line to install the required dependencies in the cloned repository:
     npm install
    
  3. Verify that the browserstack-local dependency is added in the package.json file:
     "dependencies": {
       "browserstack-local": "^1.5.1"
     }
    
  4. Set your BrowserStack credentials in the conf.js file as follows:
      const userCredentials = {
      'browserstack.user': process.env.BROWSERSTACK_USERNAME || 'YOUR_USERNAME',
      'browserstack.key': process.env.BROWSERSTACK_ACCESS_KEY || 'YOUR_ACCESS_KEY'
      }
    
  5. Verify that the browserstack.local capability is set to true in the conf.js file as follows:
         exports.localTestCapabilities = {
         "local" : "true",
        }
    
  6. Set your BrowserStack Access Key in the tests/local.js file as follows:
     // set your Access Key
     const bs_local_args = { 'key': process.env.BROWSERSTACK_ACCESS_KEY || 'YOUR_ACCESS_KEY' };
    
  7. Run the CodeceptJS test using the following command:
     npm run local
    
  8. View the test result on your Automate Dashboard.
Protip: Check out our capability builder and select from a range of custom capabilities that BrowserStack supports.

Understand your Local test script

When you run the npm run local command, the local.js file within the tests directory is executed. When the test is triggered, it:

  • Starts Local Testing connection
  • Opens http://bs-local.com:45691/check
  • Checks whether the web page contains the Up and running text
  • Marks the test as passed or failed based on the availability of the text
  • Stops the Local Testing connection.
local.js
    const webdriver = require('selenium-webdriver');
    const { By } = require('selenium-webdriver');
    const assert = require('assert');
    const browserstack = require('browserstack-local');

    // Input capabilities
    const { localTestCapabilities, hubURL } = require('../conf');

    //creates an instance of Local
    const bs_local = new browserstack.Local();

    const bs_local_args = { 'key': process.env.BROWSERSTACK_ACCESS_KEY || "YOUR_ACCESS_KEY" };

    // starts the Local instance with the required arguments
    bs_local.start(bs_local_args, function() {
      console.log("Started BrowserStackLocal");
      runTestWithCaps();
    });

    async function runTestWithCaps () {
      let driver = new webdriver.Builder()
        .usingServer(hubURL)
        .withCapabilities(localTestCapabilities)
        .build();

      try{
        await driver.get("http://bs-local.com:45691/check");

        const body = await driver.wait(
          webdriver.until.elementLocated(By.css('body'))
        , 10000);

        const bodyText =  await driver.wait(
          webdriver.until.elementIsVisible(body , 10000)
        ).getText();

        assert(bodyText == 'Up and running');
        //marking the test as local is up
        await driver.executeScript(
          'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed","reason": "Local test is successful"}}'
        );
      } catch(e) {
        //marking the local test as Failed
        console.log("Error:", e.message)
        await driver.executeScript(
          'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "Couldn\'t connect to local"}}'
        );
      } finally {
        if(driver){
          await driver.quit();
        }
      }

      // stop the Local instance
      bs_local.stop(function() {
          console.log("Stopped BrowserStackLocal");
      });
    }

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
Download Copy