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 it up and getting it to run as expected. In this situation, I was using an Amazon S3 bucket in a very locked down and restricted environment. The purpose of this guide is to try and prevent someone else from having to go through the same pains as I did 🤕🤕🤕

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 proxy allows your unit test code to be able to 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 web servers to run the Selenium tests. You install BrowserstackLocal via Nuget. Right-click on your unit test project, select 'Manage Nuget Packages:

How To SetUp BrowserStackLocal On Your PC And Your Build Server 1

Search for 'BrowserStackLocal' and install it. BrowserStackLocaladds an assembly into your packages directory.

Issues I Encountered With BrowserStackLocal

Out-of-the-box, BrowserStackLocal didn't work as expected, it couldn't find my local website. The documentation is a bit light around BrowserstackLocal, so I used DotPeek to figure out how it worked. The BrowserStackLocal start-up code looks like this:

As you can see 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, you need to make sure the request is allowed. Second, were Windows environment variables not being set. The build server was trying to run the service using a system account, the HOME DRIVE and HOMEPATH wasn't set. I used this script to fix it:

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:

The documentation says that you don't need to put the drive letter 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 finally check some relative paths.

Instead of specifying a path on your server, you could add 'BrowserStackLocal.exe' within your unit test project, here:

Or to make life easier:

This example is for a Visual Studio release build. If you need to do a debug build you'll need to whack the file in the appropriate folder. With the theory covered, let us go over the C# code that you'll need to write to enable BrowserStackLocal with your unit tests:

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. Getting BrowserStack to work on my local PC was pretty pain-free. Getting it to work on a locked-down build server was a bit of a nightmare. Hopefully, if you follow the advice covered in this tutorial life will be easy. If I had to redo this, I would first make sure the build server can talk to S3 first. If it can't, add the BrowserStackLocal.exe within your bin ➡ release folder. After that use, the code above and everything should work. Happy Coding 🤘