Skip to main content

Test on Internal Networks

Run JestJS tests on your privately hosted websites using BrowserStack Automate

Important: Sample test scripts are available in the jest-js-browserstack repository.

The sample test script in this section is compatible with JSON wire protocol-based client bindings. Check out our W3C-based scripts in the selenium-4 branch of the repository.

Introduction

BrowserStack enables you to run your JestJS 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. Run your first local test

Prerequisites

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

Run your first local test

Configuring your JestJS tests for Local Testing takes just a few simple steps:

  1. Clone the jest-js-browserstack sample repository on GitHub using the following command:

    git clone https://github.com/browserstack/jest-js-browserstack.git
    cd jest-js-browserstack
    
  2. Verify that the browserstack-local dependency is added in the package.json file:

    package.json
      "dependencies": {
         "browserstack-local": "^1.4.8",
         "selenium-webdriver": "3.6.0",
         "jest": "^27.4.7"
       },
    
  3. Install the required dependencies in the sample repository by running the following command in your command-line:

     npm install
    
  4. Update your JestJS config file, jest-js-browserstack/conf/local.conf.js, in your sample repository to set the browserstack.local capability to true:

    conf/local.conf.js
    module.exports = {
        os: "Windows",
        os_version: "10",
        browserName: "Chrome",
        browser_version: "latest",
        project: "BStack Demo",
        build: process.env.BROWSERSTACK_BUILD_NAME || "jest-browserstack",
        name: "local test",
        "browserstack.local": true,
        "browserstack.localIdentifier": localIdentifier,
        "browserstack.selenium_version": "3.6.0",
        "browserstack.user": username,
        "browserstack.key": accessKey,
    };
    

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

    src/local.test.js
        beforeEach(async () => {
          const accessKey = process.env.BROWSERSTACK_ACCESS_KEY || "";
          const localIdentifier = capabilities["browserstack.localIdentifier"];
    
          await new Promise((res) => {
            local.start(
              {
                key: accessKey,
                verbose: true,
                logFile: "./local.log",
                localIdentifier,
              },
              res
            );
          });
    
          driver = await new Builder()
            .usingServer("https://hub.browserstack.com/wd/hub")
            .withCapabilities(capabilities)
            .usingHttpAgent(
              new https.Agent({
                keepAlive: true,
                keepAliveMsecs: 1000000,
              })
            )
            .build();
    
          await driver.manage().window().maximize();
        }, 100000);
    
        test("local test", async () => {
          await driver.get("http://bs-local.com:45691/check");
    
          expect(await driver.findElement(By.css("body")).getText()).toBe(
            "Up and running"
          );
    
          statusFail = false;
        }, 10000000);
    
        afterEach(async () => {
          if (statusFail) {
            await driver.executeScript(
              `browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": ""}}`
            );
          } else {
            await driver.executeScript(
              `browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed","reason": ""}}`
            );
          }
    
          await driver.quit();
    
          await new Promise((res) => {
            local.stop(res);
          });
        })
    
  5. Run your JestJS 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 local 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