Skip to content

Selenium tests how to

Jonathan Van der Cruysse edited this page May 15, 2018 · 1 revision

This is a practical guide for using and editing the Selenium tests.

Requirements

The Selenium tests are coded in C#. You'll need the following to build and run them. The subsections below detail how to install these dependencies on Debian-based distributions and Mac OS X.

  • A .NET runtime implementation. Windows ships with a .NET runtime by default. For Linux and Mac OS X, I warmly recommend Mono. The installation instructions should be pretty easy to follow.
  • A C# compiler, an MSBuild-compatible build engine and a NuGet client. These are included in Visual Studio on Windows. On Debian-based systems, install the mono-complete, mono-devel and nuget packages. On Mac OS X, install the mono and nuget Homebrew packages.
  • GNU Make (optional) for running the Makefile. You can also just copy relevant commands from the Makefile. Visual Studio users can just stay within the comfort of their IDE.

Setup for Debian-based distros

First, add the mono apt repository by following the instructions here. The exact repository you need to add depends on your operating system version.

Next, run the following commands.

$ sudo apt install mono-complete mono-devel nuget

That's it. You should be able to build the test runner now.

Setup for Mac OS X

Run the following Homebrew commands. Don't have Homebrew? Follow these super-simple instructions.

brew update && brew install mono && brew install nuget

Building the tests

The easiest way to build the tests is to use the Makefile.

# To download Selenium (plus some other dependencies).
$ make nuget
# To build the tests.
$ make

Running the tests

To run the tests, just spell

# Replace $OPTIONS with whatever you want
# (or nothing at all to use the default configuration).
$ ./bin/Debug/selenium-tests.exe $OPTIONS
# Alternatively, there's a Makefile shortcut for this.
$ make TEST_OPTIONS="$OPTIONS" run

If you're at all unsure about the options offered by the test runner, use the -h/--help option. The test runner will also suggest options if you get an option name (slightly) wrong.

$ ./bin/Debug/selenium-tests.exe --help

Adding a test

To add a test, find a relevant test file in the Tests directory and define a new TestCase field. Add it to the All field, which specifies a list of all test cases in the file.

If you can't find a relevant test file, create a new one. This entails the following steps:

  1. Create a new file in the Tests directory and call it whatever you want.
  2. Set the contents of the file to the following template:
using System.Collections.Generic;

namespace SeleniumTests.Tests
{
    /// <summary>
    /// Contains tests that are essentially just sanity checks.
    /// </summary>
    public static class SanityChecks
    {
        public static readonly TestCase CheckTitle =
            new TestCase(
                "Page title must be right",
                (driver, log) =>
                {
                    Assert.AreEqual(driver.Title, "UnSHACLed Editor");
                });

        /// <summary>
        /// A list of all sanity check tests.
        /// </summary>
        public static readonly IEnumerable<TestCase> All =
            new TestCase[]
        {
            CheckTitle
        };
    }
}
  1. Update the TestCases field like so (ExistingTests is just whatever was already there).
private static readonly IEnumerable<TestCase> TestCases = ExistingTests.Concat(SanityChecks.All);
  1. If you're not using a full-fledged IDE like Visual Studio or MonoDevelop, you'll need to add the your new file to SeleniumTests.csproj. Replace SanityChecks.cs with whatever you called your test file:
<ItemGroup>
  ...
  <Compile Include="Tests\OtherTests.cs" />
  <Compile Include="Tests\SanityChecks.cs" /> <-- insert this line
</ItemGroup>