Automated Tester


This content shows Simple View

July 2015

Selenium Builder Actions

There are some complex actions which may not be obvious how to do in Selenium but by leveraging the use of the builder actions we can add more user interactions into our scripts where needed.

using OpenQA.Selenium.Interactions;

So what can we do with this? Lets have a quick look at what is contained within and then have a look at an example.

  • Click
  • Double Click
  • Click and Hold
  • Right Click (Context Click)
  • Drag and Drop
  • Drag and Drop to Offset
  • Key Down
  • Key Up
  • Move By Offset
  • Move To Element
  • Perform
  • Build
  • Release
  • SendKeys

We can see that there is a lot of advanced user functionality that you don’t find in the usual Selenium bindings. Let’s look an example using KeyUp and KeyDown.

Lets try and open a new tab with the builder actions when we click a particular link.

First we’ll new up the actions builder, hold down the CTRL key whilst clicking and then release the CTRL key.

Actions builder = new Actions(Context.Driver);
var linkToClick = elementToCheck.FindElement(By.LinkText("Continue"));


We instruct the builder to do our bidding and follow this up with .Perform to build/ execute the action. This particular piece of code (although not Mac friendly, in C# .Net their is a ‘Command’ key listed under Keys) We are depressing CTRL and clicking the link, building and performing it and then releasing the key press and then building and performing that.

This opens the desired link in a new tab, you can argue this isn’t the best way of doing so but it will do for demo purposes.

The actions builder is particularly useful for hovering the mouse over certain elements on the page with MoveToElement or performing unconventional clicking actions such as double and right clicks. Keep them in mind if you come across difficult functionality.

Checking link status in C#

Sometimes you need to test an application that has external links or have the need to check links and it doesn’t make a whole lot of sense to physically visit these places and verify that something has loaded on it because it is way out of your control and that site could change at any given moment, making your tests flaky.

One way to circumvent this is to check the link status code of a given link by sending a http request and returning the status code. We can then parse this to a string and make an assertion based against it. The code below does all the heavy lifting.

        public HttpStatusCode GetHeaders(string url)
            HttpStatusCode result = default (HttpStatusCode );

            var request = HttpWebRequest.Create(url);
            request.Method = "HEAD";
            using ( var response = request.GetResponse() as HttpWebResponse)
                if (response != null)
                    result = response.StatusCode;

            return result;

So in our step definition all we need to do is assert the returned status code against what we are expecting it to be whether it be a 200 OK, 404, 500 etc.

We can now call this and make an assertion on the retrieved link status in our step definition files.

        var expectedStatus = VerifyPages.HttpStatusOk;
        var facebookLink = UrlConsts.Facebook;
        var facebookStatus = Page.GetHeaders(facebookLink).ToString();
        Assert.AreEqual(expectedStatus, facebookStatus);

Browser Profiles with Selenium

I decided to write this article after someone asked about browser profiles with Selenium in the IRC channel.

With Selenium we can specify a particular browser profile when instantiating our driver so if you happen to have specific browser setup requirements such as Firefox with certain plugins installed we can tell the driver to use this.


To begin with we will want to setup up our profile to be used. In this example we will use Firefox and use the Firefox profile manager.

To access this run “firefox -p” from Run.


Which will load up the profile manager, here we will create a new profile and call it ‘QAToolsProfile’ – for this example we will choose the save folder as ‘Browser Profile’ on the desktop.



Next, start up firefox and add in the tools required for your new profile, I am going to add a tool called REST Client.



Right then! We now have a customised browser profile to call in our code.

Calling the Profile

As you might expect this is relatively easy as we just need to pass in the location of the profile which we dumped on the desktop:

private static void SetupFirefoxDriver()
string profileDir;

profileDir = @"C:\Users\\Desktop\BrowserProfile";

FirefoxProfile profile = new FirefoxProfile(profileDir);
Driver = new FirefoxDriver(profile);

If we now say to a test to navigate to “chrome://restclient/content/restclient.html” we can automate the browser plugin. In this instance we can perform some API checks with RESTClient’s user interface. If we did not specify the profile then Firefox would load up with no extra frills.


You can find more information on Browser Profiles with Firefox here.