Skip to main content
Get Automate insights in Microsoft Teams with our new integration! Get Started!.

Test on Internal Networks

Run JUnit 5 tests on your privately hosted websites using BrowserStack Automate.

Note: Check out the junit-browserstack repository on GitHub for the code samples used in this section.

Introduction

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

  • 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.
  • Maven is installed on your machine, Maven environment variables are set, and Maven bin is added to system path, $PATH. Check our the official website to download the latest version of Maven.
  • Git installed on your machine.

Run your first local test

Configure your JUnit 5 tests for Local Testing using the following steps:

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

     git clone https://github.com/browserstack/junit-browserstack.git
     cd junit-browserstack/junit-5
    
  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:

     mvn clean install
    
  3. Verify that the browserstack-local-java dependency is added in the pom.xml file:

    pom.xml
     <!-- Install using maven -->
      <dependency>
       <groupId>com.browserstack</groupId>
       <artifactId>browserstack-local-java</artifactId>
       <version>1.0.6</version>
     </dependency>
    
  4. Update your JUnit 5 config file, caps.json, in your sample repo to set the browserstack.local capability to true:

    caps.json
       {
        "tests": {
        "local": {
         "common_caps": {
           "name": "local-test",
           "build": "junit5-browserstack",
           "browserstack.console": "verbose",
           "browserstack.local": "true",
           "browserstack.debug": "true"
         },
         "platform": {
           "chrome": {
             "browser": "chrome",
             "browser_version": "latest",
             "os": "Windows",
             "os_version": "10"
             }
           }
        }
       }
      }
    

    You can use the following sample test case LocalTest.java in the junit-browserstack/junit-5/src/test/java/tests/ directory to verify if your Local test connection has been set up correctly:

    LocalTest.java
       public class LocalTest {
    
       @WebDriverTest
       void localTest(WebDriver driver) {
           MarkSessionStatus sessionStatus = new MarkSessionStatus();
    
           try{
               driver.get("http://localhost:45691/check");
               String validateContent = driver.findElement(By.cssSelector("body")).getText();
               if(validateContent.contains("Up and running"))
                   sessionStatus.markTestStatus("passed", "Local content   validated!",driver);
               else
                   sessionStatus.markTestStatus("failed", "Local content not validated!",driver);
           }catch (Exception e){
              sessionStatus.markTestStatus("failed", "There was some issue!",driver);
              System.out.println(e.getMessage());
          }
          driver.quit();
          }
        }
    
  5. Run your JUnit 5 test using BrowserStack Local with the following command:

     mvn test -P local
    

Understand the BrowserStack Local class

The SetupLocalTesting.java file in the junit-browserstack/junit-5/src/test/java/utils directory includes the implementation logic that initiates the connection with BrowserStack.

SetupLocalTesting.java
public class SetupLocalTesting {
  private static volatile SetupLocalTesting instance;

  private final Local local = new Local();

  private SetupLocalTesting(Map<String, String> localOptions) {
    try {
      local.start(localOptions);
    } catch (Exception e) {
      throw new RuntimeException("Initialization of BrowserStack Local failed.", e);
    }
  }

  public static void createInstance(Map<String, String> args) {
    if (instance == null) {
      synchronized (SetupLocalTesting.class) {
        if (instance == null) {
          instance = new SetupLocalTesting(args);
          Runtime.getRuntime().addShutdownHook(new Closer(instance.local));
        }
      }
    }
  }

  public static SetupLocalTesting getInstance() {
    return instance;
  }

  private static class Closer extends Thread {
    private final Local local;

    public Closer(Local local) {
      this.local = local;
    }

    @Override
    public void run() {
      try {
        if (local.isRunning()) {
          local.stop();
        }
      } catch (Exception e) {
        System.out.println("Error encountered while stopping BrowserStack Local { }" + e);
      }
    }
  }
}

Next steps

After you have successfully run your local 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