How To SetUp BrowserStackLocal on Your PC And Your Build Server

If you want a semi-easy way to be able to run some front-end unit tests within your C# project, then this article is for you.  I've talked previously about setting up Selenium with C# and Browserstack automate here.  I've recently started using Browserstack Automate on a project and I bumped into a few problems setting and getting it to run as expected and if this guide prevents someone else having to go through the same pain, then winner winner!

How To Install BrowserStackLocal

Before I talk about how to set everything up, it's probably beneficial if I share with you what it does.  In order for browserstack to be able to test your in-house/non-public facing projects, you'll need to install an additional package called 'BrowserStackLocal'.  BrowserStackLocal basically acts as a proxy between the browserstack server and your local web instance.  This means your unit test code can run and relay your local website back to Browserstack virtual browsers.    The benefit of this approach is that you don't need to install any browsers on your servers.

You install Browserstack local like most things .NET nowadays, via Nuget.  Right-click on your unit test project, select 'Manage Nuget Packages':

Search for 'BrowserStacklocal' and install it.  Browserstacklocal adds an assembly into your packages directory.  

Issues I Encountered With BrowserStackLocal

Out of the box, BrowserStackLocal didn't work as expected for me on the build server, e.g. it couldn't find my local website.  The documentation is a bit light around browserstack local, so I used dotpeek to figure out how it worked, and the start-up code looks like this:

    private static readonly string binaryName = "BrowserStackLocal.exe";
    private static readonly string downloadURL = "https://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal.exe";
    public static readonly string[] basePaths = new string[3]
    {
      Path.Combine(Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%"), ".browserstack"),
      Directory.GetCurrentDirectory(),
      Path.GetTempPath()
    };

So there are a few interesting things going on. First, BrowserStacklocal tried to get the latest version of the proxy software from S3. If you're running behind a firewall /WAF then you need to make sure the request is allowed. Second, the Environment Variables. As a build server was trying to run the tasks, which was running under a system account, the HOMEDRIVE and HOMEPATH wasn't set. I tried using this script:

set HOMEDRIVE=C:
set HOMEPATH=\TEMP
set

If like me, you couldn't open a Firewall port to S3, then you can add the browserstacklocal.exe into your source control and get Browserstack to use that. To point browserstack to a local binary/exe you can use this command.

bs_local_args = { 'binarypath': '/browserstack' }

What the documentation doesn't say is that a) you don't need to put the drive letter in first. If your build is run on the C drive in the example above browserstacklocal will look in c:/browserstack/, if it's run on the e drive it will look in e:/browserstack> If it can't get external access, it will also check some relative paths first. Using process monitor, So instead of specifying a path on your server, you could add 'BrowserStackLocal.exe' within your unit test project, here: '\bin\Release\%HOMEDRIVE%%HOMEPATH%\.browserstack' or to make life easier@ '\bin\Release' In this example this is for a 'release' build. If you need to do a debug, or something else, you'll need to whack the file in the appropriate folder.

Show Me The Code

With the above in mind, lets go over the C# code you'll need to write to enable browserstacklocal with your unit tests:

var capability = DesiredCapabilities.Chrome();
capability.SetCapability("browserstack.user", username);
capability.SetCapability("browserstack.key", accessKey);

this.browserStackLocal = new Local();
var localArgs = new List<KeyValuePair<string, string>>
{
	new KeyValuePair<string, string>("key", "yourkey"),
    new KeyValuePair<string, string>("force", "true"), // USe this to force your bulid server to always kill browserstacklocal.exe if it's already running (get build errors if you don't do this)
    new KeyValuePair<string, string>("binarypath", "/temp"),  // Look in c:/temp for the file
    new KeyValuePair<string, string>("logfile", "/temp/logs.txt"),  // Store the log file in c:/temp/logs.txt
    new KeyValuePair<string, string>("v", "true") // Verbose logging, e.g. log everything"
}
				
this.browserStackLocal.start(localArgs);
driver = new RemoteWebDriver(new Uri("http://hub-cloud.browserstack.com/wd/hub/"), capability);

Warning:  I experienced a lot of issues setting this up.  When I tried to run my tests on the build server I ended up getting a lot of 'page not found' or DNS errors.  The site I was trying to test loaded correctly using a browser on the server.  

BrowserStackLocal Takeaway

Getting browserstack to work on my local PC was pretty painfree. Getting it to work on a locked down build server was a bit of a nightmare. Hopefully, if you follow the advice your life will be easy. If I had to redo this, I'd first make sure the build server can talk to S3 OK. If it can't, add the BrowserStackLocal.exe within your bin -> release folder. After that use the code above and everything should work. Enjoy!

submit to reddit

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top