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

Configure your C# tests for Local Testing using the following steps:

  1. Clone the sample csharp-selenium-browserstack repository using the following command:
     git clone -b selenium-4 https://github.com/browserstack/nunit-browserstack.git
    
  2. Open the csharp-selenium-browserstack.csproj file and verify the BrowserStackLocal and Selenium NuGet packages are added to it:
       <ItemGroup>
         <PackageReference Include="Selenium.WebDriver" Version="3.141.0" />
         <PackageReference Include="BrowserStackLocal" Version="2.0.0" />
       </ItemGroup>
    
  3. Set your BrowserStack credentials in the csharp-selenium-browserstack/LocalTest.cs file as follows:
     // set your Username and Access Key in the capabilities
     capability.AddAdditionalCapability("browserstack.user", "YOUR_USERNAME");
     capability.AddAdditionalCapability("browserstack.key", "YOUR_ACCESS_KEY");
    
  4. Verify that the browserstack.local capability is set to true in the csharp-selenium-browserstack/LocalTest.cs file:
     capability.AddAdditionalCapability("browserstack.local", "true");
    
  5. Run the C# test in your IDE. You can also run the test by using the Run functionality of Visual Studio.
  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 C# test script, the LocalTest.cs file 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.
LocalTest.cs
  using System;
  using System.Collections.Generic;
  using BrowserStack;
  using OpenQA.Selenium;
  using OpenQA.Selenium.Remote;
  using OpenQA.Selenium.Support.UI;
  namespace csharp_selenium_browserstack
  {
    class LocalTest
    {
      public static void execute()
      {
        IWebDriver driver;
        OpenQA.Selenium.Safari.SafariOptions capability = new OpenQA.Selenium.Safari.SafariOptions();
        capability.AddAdditionalCapability("browser", "iPhone");
        capability.AddAdditionalCapability("device", "iPhone 11");
        capability.AddAdditionalCapability("realMobile", "true");
        capability.AddAdditionalCapability("browserstack.local", "true");
        capability.AddAdditionalCapability("os_version", "14.0");
        capability.AddAdditionalCapability("name", "BStack-[C_sharp] Sample Test"); // test name
        capability.AddAdditionalCapability("build", "BStack Build Number 1"); // CI/CD job or build name
        capability.AddAdditionalCapability("browserstack.user", "UserName");
        capability.AddAdditionalCapability("browserstack.key", "AccessKey");
        // Creates an instance of Local
        Local local = new Local();

        // You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY".
        List<KeyValuePair<string, string>> bsLocalArgs = new List<KeyValuePair<string, string>>();
        // Starts the Local instance with the required arguments
        bsLocalArgs.Add(new KeyValuePair<string, string>("key", "AccessKey"));

        // Starts the Local instance with the required arguments
        local.start(bsLocalArgs);
        driver = new RemoteWebDriver(new Uri("https://hub-cloud.browserstack.com/wd/hub/"), capability);
        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));

        try
        {
          driver.Navigate().GoToUrl("http://bs-local.com:45691/check");
          // getting body content
          String body_text = driver.FindElement(By.TagName("body")).Text;
          if (body_text == "Up and running")
          {
            ((IJavaScriptExecutor)driver).ExecuteScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"passed\", \"reason\": \"Local test ran successful\"}}");
          }
        }
        catch
        {
          ((IJavaScriptExecutor)driver).ExecuteScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"failed\", \"reason\": \" Issues connecting local\"}}");
        }
        driver.Quit();
        local.stop();
      }
    }
  }

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