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.
- BrowserStack Username and Access key, which you can find in your account profile. If you have not created an account yet, you can sign up for a Free Trial or purchase a plan.
- Git installed on your machine.
- .NET installed on your machine.
Run your first Local test
Configure your C# tests for Local Testing using the following steps:
- Clone the sample csharp-selenium-browserstack repository using the following command:
git clone -b selenium-4 https://github.com/browserstack/nunit-browserstack.git
- 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>
- 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");
- Verify that the
browserstack.local
capability is set totrue
in thecsharp-selenium-browserstack/LocalTest.cs
file:capability.AddAdditionalCapability("browserstack.local", "true");
- Run the C# test in your IDE. You can also run the test by using the Run functionality of Visual Studio.
- View the test result on your Automate Dashboard.
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.
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:
- Run multiple tests in parallel to speed up builds
- Test local websites that resides behind a proxy
- Manage Local Testing connections
- Set up your CI/CD: Jenkins, Bamboo, TeamCity, Azure, CircleCI, BitBucket, TravisCI, GitHub Actions, GoCD
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
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!