I think a lot of developers have heard of Selenium, however, from my experience very few teams get around to using Selenium on their projects. Like Unit Testing in general, I think most people have 'frontend' testing on their 'we SHOULD do this list'. At the beginning of a project, adding testing is something someone promises to get around to implementing. Ultimately it never happens. In today's guide, I'm going to walk you through setting up Selenium with C# and NUnit to help you actually action this. I'm going to tell you one simple technique that will make your deployments a lot more secure. At the end I'll share the code required to implement it, so read on 😎😎😎

What's The Point Of Using Selenium With C

As someone who runs their own website, one issue I have whenever I do a deployment is ensuring I do not break things. No matter how simple or complex a change might be, as a one-man band, I don't have the time to physically check that every template/page on this site works every time I release. I run this website and I also deal with multiple clients so my time is limited. A LOT of code has been written to render this page you're reading, this isn't simply some theme but a handcrafted website that probably does a lot more than you might first think. At the time of writing, I think there are over 1000 pages now.

This leaves me with a problem; when I make code changes how do I know that I haven't broken a template, or, a page without spending an hour or two each release checking every page? Selenium can help solve this problem and with some simple tests, it can save you hundreds of hours of having to manually regression test a site. When I release new code, I like to know that it will work. One simple set of Selenium tests that you can write, is to create a test to make sure that key pages on your site don't return a 404 when your build server is compiling your site.

This one simple trick has helped me a number of times find errors that would have otherwise gone live. In my opinion, setting up Selenium is a really good time investment as over the course of your website's lifespan, you can save hours and hours worth of manual testing. If you're working on an e-commerce site, spotting an error on the checkout page can save you a fortune in lost revenue. I've seen companies lose anywhere between £10,000 and £45,000 over a weekend through bad deployments (it wasn't me, in case you're wondering!!!!)

How Do I Write Code Using Selenium With C

Writing Selenium tests is very similar to writing normal unit tests. The majority of your test code will look very similar so the learning curve is low. You can still use NUnit for the setup and tear down code, you still define tests by using applying the [Test] onto a method. The main difference when using Selenium is that you'll need to set up a web driver to either load a browser locally (on your build server), or you can connect to a cloud service and run your code in a virtual browser. If you opt to use local browsers installed on your server via a local web driver, you'll need to install all the browsers you want to test against on your server and keep them up-to-date. This is a pain...

What Is BrowserStack?

I talked briefly about virtual web drivers and browsers. This raises the question, which one should I use? There might be loads of options available, however, my experience has been with BrowserStack. BrowserStack automate is not a free service, however, it's not going to cost you millions of pounds to use it either. If you can afford it, I would recommend getting a BrowserStack license. Browserstack has a cool feature called Automate.

After you have a browser stack atomate account, you can create a web driver class, passing in your BrowserStack username/password. You can use virtual browsers in your application! Simply specify what type of browser you want to use from the Browserstacks list and off you go. No mone software maintenance on your in-prem servers required!

What Does My Build Process Look Like?

One thing that can confuse people is what a build process with Selenium looks like. Selenium will add an extra few steps within your Ci/Cd pipeline. First, you will need to deploy your website with a hostname/URL that the build server can talk to, in order for the Selenium tests to run successfully. Unlike normal unit tests, you will need a fully deployed site hosted in a web server that runs. You cannot simply just run Selenium tests against your compiled assemblies/Dll's. This is kind of a pain if you want to prevent a build from being copied into a server if your tests fail. You cant simply deploy then test. Instead, you may want to deploy to a test box and then run the tests first. Only after a successful deployment will you copying the latest build to the environment you want to use. In effect, you are adding an additional build step!

In most situations, I usually create a hostname similar to autotestsite.website.com that the build server deploys to first. The Selenium tests are run against autotestsite.website.com and if they pass, the build server then deploys the working website to 'website.com

Show Me Some Selenium Code!

Ok, so let's get to the good stuff. If you're new to unit testing, I suggest you read my tutorial on NUnit available here if you get stuck. In these tests, I'm using a web driver that talks to BrowserStack. Below this class, I'll also go over how you would create a normal local web driver:

As you can see, these tests look pretty similar to normal unit tests. You create a web driver and then you can methods like FindElement() and SendKeys() to test the HTML elements you expected to be there, are in fact there! The driver has properties like PageSource, PageTitle etc.. that you can use in order to perform the asserts that you care about. On big projects, I think having automated tests that make sure you haven't broken key pages is a vital part of a good build process. Happy Coding 🤘