Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / NUnit

Passing a Browser as a Parameter to the NUnit Console Runner

0.00/5 (No votes)
22 Jun 2017CPOL2 min read 8K  
To provide a solution which helps to pass a necessary browser to Selenium WebDriver tests through the NUnit

Whenever we decide to execute Selenium tests based on the NUnit, we always have several options to choose from (depends on a project, infrastructure and framework): to run tests from Visual Studio or any other IDE (1), to run tests locally from the command line (2), to run tests on a continuous integration system (3). The last two are almost the same because we need to call NUnit console test runner for both cases. The difference is only that for a continuous integration solution, we need to setup an additional build step and run NUnit console from there. But it's not a big deal. For example, here how it looks for TeamCity CI. Jenkins CI has almost the same approach, by the way.

The command line approach can bring some additional benefits. For example, we can easily pass a required browser to our Selenium WebDriver tests and thus manage to run different builds with tests on different browsers. Let's see how it can be done through the NUnit 3 console test runner.

Challenge

To provide a solution which helps to pass a necessary browser to Selenium WebDriver tests through the NUnit.

Remember, not so long ago, I posted the article about the NUnit TestContext properties? Except the properties described in that post, TestContext provides access to its static Parameters member – the object of the TestParameters class. The TestParameters class itself holds any named parameters supplied to a test run and provides methods for accessing these parameters. Here is how it looks like in the code:

C#
var browser = TestContext.Parameters.Get("Browser");

/* or if we want to have the default browser
when the parameter is not passed to the runner at all, then: */

var browser = TestContext.Parameters.Get("Browser", "firefox");

Apparently, we need to pass the named Browser parameter with some value to the nunit3-console. The simplest example of the command with passing a parameter looks like:

nunit3-console.exe --params:Browser=chrome OurSeleniumTests.dll

The command above assumes that we have the nunit3-console binary location added to the PATH system variable. Otherwise, we need to specify the absolute path to the nunit3-console.exe. Don't forget also to specify the absolute path to the DLL with the Selenium tests, if it's not in the same folder with the nunit3-console binary.

Okay, the preparation is done! Let's create a test class with some dummy test case and see how it can look like. For example:

C#
[TestFixture]
public class GitHubTests
{
    private IWebDriver Driver;
    private const string PAGE_TITLE = 
    "The world's leading software development platform · GitHub";
    private const string PAGE_URL = @"https://github.com/";

    [OneTimeSetUp]
    public void SetUpClass()
    {
        var browser = TestContext.Parameters.Get("Browser", "firefox");
        Driver = BrowserProvider.CreateBrowser(browser);
    }

    [Test]
    public void CheckGitHubPageTitle()
    {
        Driver.Url = PAGE_URL;
        Assert.AreEqual(Driver.Title, PAGE_TITLE);
    }

    [OneTimeTearDown]
    public void TearDownClass()
    {            
        Driver.Close();
    }
}

As displayed in the code above, we can obtain the passed parameter pretty easily and then handle as it pleases. NUnit parameters are strings by design, so please take that into account while handling them in your code. For example, I created the very simple fabric which takes a browser string and returns an appropriate browser driver – BrowserProvider.CreateBrowser(browser). But you are free to do whatever you want there and implement your own fabrics or use third-party frameworks.

That's all for now! Explore NUnit and try new solutions. Happy testing!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)