Skip to main content

Test localhost and staging websites that are not publicly accessible

BrowserStack can integrate with test suites pointing to your localhost URL, staging environment, and even websites behind one or more proxies/firewalls. This is done using BrowserStack Local - a tunneling feature that establishes a secure connection between your machine and the BrowserStack Cloud.

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 -b selenium-4 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.6</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
     // You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY".
     HashMap<String, String> bsLocalArgs = new HashMap<String, String>();
     bsLocalArgs.put("key", AUTOMATE_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:
     HashMap<String, Object> browserstackOptions = new   HashMap<String, Object>();
     browserstackOptions.put("local", "true");
     capabilities.setCapability("bstack:options", browserstackOptions);
    
  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
  package com.browserstack.app;

//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.time.Duration;
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 = System.getenv("BROWSERSTACK_USERNAME") != null ? System.getenv("BROWSERSTACK_USERNAME") : "BROWSERSTACK_USERNAME";
    public static final String AUTOMATE_ACCESS_KEY = System.getenv("BROWSERSTACK_ACCESS_KEY") != null ? System.getenv("BROWSERSTACK_ACCESS_KEY") : "BROWSERSTACK_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 capabilities = new DesiredCapabilities();
        capabilities.setCapability("browserName", "Chrome");
        capabilities.setCapability("browserVersion", "latest");
        HashMap<String, Object> browserstackOptions = new HashMap<String, Object>();
        browserstackOptions.put("os", "OS X");
        browserstackOptions.put("osVersion", "Sierra");
        browserstackOptions.put("local", "true");
        browserstackOptions.put("seleniumVersion", "4.0.0");
        capabilities.setCapability("bstack:options", browserstackOptions);
        capabilities.setCapability("sessionName", "BStack-[Java] Sample Test"); // test name
        capabilities.setCapability("buildName", "BStack Local 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", AUTOMATE_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), capabilities);
        try {
            driver.get("http://bs-local.com:45691/check");
            final WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(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", "Could'nt 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
Download Copy