Skip to main content

Test on Internal Networks

BrowserStack enables you to run your Java 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 section, you’ll learn:

Prerequisites

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

Run your first Local test

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

  1. Clone the sample java-selenium-browserstack repository using the following command:
     git clone https://github.com/browserstack/java-selenium-browserstack.git
    
  2. Open the pom.xml file of the cloned repository and verify that browserstack-local-java is added as a dependency to it:
     <dependency>
       <groupId>com.browserstack</groupId>
       <artifactId>browserstack-local-java</artifactId>
       <version>1.0.3</version>
     </dependency>
    
  3. Set your BrowserStack credentials in the JavaLocalSample.java file, located in the src/test/java/com/browserstack/app/ directory, as follows:
      // set your Username and Access Key as the variables
      public static final String AUTOMATE_USERNAME = "YOUR_USERNAME";
      public static final String AUTOMATE_ACCESS_KEY = "YOUR_ACCESS_KEY";
      // other commands...
      // set your Access Key
      HashMap<String, String> bsLocalArgs = new HashMap<String, String>();
      bsLocalArgs.put("key", "YOUR_ACCESS_KEY");
    
  4. Verify that the browserstack.local capability is set to true in the JavaLocalSample.java file located in the src/test/java/com/browserstack/app directory:
     caps.setCapability("browserstack.local", "true");
    
  5. Run the Java test using the Run functionality of your IDE. Run the following command if you’re using Maven:
     mvn -Dexec.mainClass="com.browserstack.app.JavaLocalSample" -Dexec.classpathScope=test test-compile exec:java
    
  6. 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 Java test script, the JavaLocalSample.java file within the src/test/java/com/browserstack/app 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.
JavaLocalSample.java
  //Sample test in Java to run Automate session.
  import org.openqa.selenium.By;
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.remote.DesiredCapabilities;
  import org.openqa.selenium.remote.RemoteWebDriver;
  import org.openqa.selenium.JavascriptExecutor;
  import java.net.URL;
  import java.util.HashMap;

  import org.openqa.selenium.support.ui.ExpectedConditions;
  import org.openqa.selenium.support.ui.WebDriverWait;
  import com.browserstack.local.Local;

  public class JavaLocalSample {
    public static final String AUTOMATE_USERNAME = "YOUR_USERNAME";
    public static final String AUTOMATE_ACCESS_KEY = "YOUR_ACCESS_KEY";
  public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_ACCESS_KEY + "@hub-cloud.browserstack.com/wd/hub";
  public static void main(String[] args) throws Exception {
  DesiredCapabilities caps = new DesiredCapabilities();
  caps.setCapability("os", "OS X");
  caps.setCapability("os_version", "Sierra");
  caps.setCapability("browser", "Safari");
  caps.setCapability("browser_version", "10.0");
  caps.setCapability("browserstack.local", "true");
  caps.setCapability("name", "BStack-[Java] Sample Local Test"); // test name
  caps.setCapability("build", "BStack Build Number 1"); // CI/CD job or build name

  //Creates an instance of Local
  Local bsLocal = new Local();

  // You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY".
  HashMap<String, String> bsLocalArgs = new HashMap<String, String>();
  bsLocalArgs.put("key", "YOUR_ACCESS_KEY");

  // Starts the Local instance with the required arguments
  bsLocal.start(bsLocalArgs);

  // Check if BrowserStack local instance is running
  System.out.println(bsLocal.isRunning());

  final WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
  try {
    driver.get("http://bs-local.com:45691/check");
    final WebDriverWait wait = new WebDriverWait(driver, 10);
    // getting name of the product
    String bodyText = wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("body"))).getText();

    if (bodyText.equals("Up and running")) {
      markTestStatus("passed", "Local Test is successful and up and running", driver);
      }
    } catch (Exception e) {
        markTestStatus("failed", "Couldn't connect the local", driver);
      }
    //Stop the Local instance
    bsLocal.stop();
    driver.quit();
  }
  // This method accepts the status, reason and WebDriver instance and marks the test on BrowserStack
  public static void markTestStatus(String status, String reason, WebDriver driver) {
  final JavascriptExecutor jse = (JavascriptExecutor) driver;
  jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \""+ status + "\", \"reason\": \"" + reason + "\"}}");
  }
  }

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