Home > Agile, Build Automation, C#, Selenium, Test Automation > Integrating NAnt builder with Visual Studio to run the Selenium Web Driver tests using NUnit

Integrating NAnt builder with Visual Studio to run the Selenium Web Driver tests using NUnit

I was interested to run my Selenium tests using NAnt build tool as it can be integrated with the Continuous Integration tools such as Hudson. NAnt is a free and open source software tool for automating software build processes. It is similar to Apache Ant, but targeted at the .NET environment rather than Java.

REFERENCES:

This blog is referenced from the http://iainhunter.wordpress.com. Thanks to Iain Hunter for posting a great blog.

SOFTWARE REQUIREMENTS:

  • Windows 7 as OS
  • Microsoft Visual Studio 2010 as IDE
  • NAnt as Build tool
  • NUnit as Unit Testing Tool
  • Selenium as a Test Automation Tool
Workspace Structure:

It is always good to configure the workspace in a same way so that it is very easy to maintain the code. Here is the sample workspace:

Installing NAnt:
Add NAnt bin path to system path:
  • Context-click on ‘Computer’ -> ‘Properties -> Advanced System Settings’.
  • Click on the ‘Environment Variables‘ button.
  • Under the System Variables, edit the system variable ‘Path‘ and append ‘;C:\Development\Tools\nant-0.92-beta1\bin‘. Click on the ‘OK’ button as shown in the figure below.
  • Open the Powershell (i.e, Command Prompt). Navigate to ‘C:\Development\Tools\nant-0.92-beta1\bin’ and type nant. You should see a message below and ignore the failure message:
NAnt 0.92 (Build 0.92.4509.0; beta1; 6/05/2012)
Copyright (C) 2001-2012 Gerry Shaw
http://nant.sourceforge.net

Running NAnt from Visual Studio:
  • In Visual Studio, Go to the ‘Tools -> External Tools‘ menu.
  • Click on the ‘Add’ button in the ‘External Tools’ window.
  • Input a Title (say in my case ‘NAnt’).
  • Input the location of NAnt application in the ‘Command’ field (say in my case ‘C:\Development\Tools\nant-0.92-beta1\bin\NAnt.exe’).
  • Input the directory info in ‘Initial directory’ field (say in my case ‘$(ProjectDir)’).
  • Set the Use Output Option checkbox and hit the ‘Apply’ & OK’ buttons. A new option ‘NAnt’ under the ‘Tools’ menu.

Workspace Setup:
  • Create a new empty solution (say SampleSolution) and save it in the location: C:\Development\Work

  • Solution will be opened in Visual Studio containing the solution file.
  • Context-click or right-click the solution file and select ‘Add -> New Project‘.
  • Add projects such as SampleSolution.Web, SampleSolution.Tests and SampleSolution.Services as per .Net convention.
  • Set the location of the project to the /src folder within your solution.
  • Set the ‘No, do not create a unit test project‘ radio button in the ‘Create Unit Test Project‘ pop-up window.

  • Right-click on the solution file again and select Enable NuGet Package Restore option.
  • Click ‘Yes’ in the pop-up dialog asking the user to restore missing NuGet packages during build.
  • Now, the Solution Explorer will look like this:

  • Folder structure will look like this:
  • Test whether the NuGet is working by running the command:install-package TwitterBootstrap
  • Twitter Bootstrap will be installed successfully and solution directory is updated as shown below:
Installing NAnt.Builder:
  • Add a new empty project to the solution (say SampleSolution.Build) and save it in the \src  directory.

  • As we need to build the solution using the NAnt, we need to make some changes in the Build -> Configuration Manager settings.
  • In Visual Studio, Go to ‘Build -> Configuration Manager‘ and uncheck the ‘Build‘ checkbox for both the Active solution configurations – ‘Debug‘ and ‘Release‘ as shown below:

  • Install Nant.Builder from NuGet by running the following command from Package Manager Console: install-package nant.builder -projectname SampleSolution.Build
  • Nant.Builder is installed into your ‘SampleSolution.Build‘ project.
  • Solution Explorer is updated as shown below:

Configuring the Nant.build file:
  • Open the ‘Nant.build‘ file.
  • Set the ‘solution.name‘ property to ‘SampleSolution‘.
  • Set the ‘solution.projects‘ property to ‘SampleSolution.Services,SampleSolution.Tests,SampleSolution.Web‘.
  • Update the ‘version.tag‘ and ‘company.name‘ property as per your need.
  • Under ‘Nunit Test Settings‘, set the ‘run.nunit.tests‘ property to ‘true‘ and ‘tests.project.name‘ property to ‘SampleSolution.Tests‘.
<?xml version="1.0" encoding="utf-8" ?>

<project default="buildRelease" basedir=".">
 <include buildfile="GlobalBuildSettings.xml" />

<!--The name of your solution, please overwrite the default -->
 <property name="solution.name" value="SampleSolution"/>

<!-- Comma seperated list of projects contained in your solution.-->
 <property name="solution.projects" value="SampleSolution.Services,SampleSolution.Tests,SampleSolution.Web" />

<!-- Set the configuration for compilation, typically release, but may be custom -->
 <property name="release.configuration" value="Release" />

<!-- Manually set version, if using CCNet this will be overwritten later -->
 <property name="version.tag" value="1.0.0.0"/>
 <property name="company.name" value="anoopjshetty.wordpress.com" />

<!-- If your projects reside in a different directory from the .sln file specify here, or leave empty if not -->
 <property name="solution.src.dir" value="src" />

<!--Nunit Test Settings-->
 <property name="run.nunit.tests" value="true" />

<!--The name of the project containing your Nunit tests-->
<property name="tests.project.name" value="SampleSolution.Tests" />


Configuring the GlobalBuildSettings.xml file:
  • Open the ‘GlobalBuildSettings.xml‘ file.
  • Set the properties ‘local.buildspace‘, ‘local.workspace‘, ‘local.releasespace‘, ‘nunit-console.exe‘ accordingly as shown below:
<?xml version="1.0" encoding="utf-8" ?>
<!--Set up the properties appropriate for your local environment-->
<project name="GlobalBuildSettings">

 <!--Location where you want source built in your local workspace -->
 <property name="local.buildspace" value="C:\Development\Builds"/>
<!--Location where you want source built in your local workspace -->
 <property name="local.workspace" value="C:\Development\Work"/>

 <!--Location where you want source built in your local workspace -->
 <property name="local.releasespace" value="C:\Development\Releases"/>
<!--Location where Msbuild for .Net 4.0 is installed-->
 <property name="msbuild4.exe" value="C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe"/>

 <!--Location for Azure packager cspack-->
 <property name="cspack.exe" value="C:\Program Files\Windows Azure SDK\v1.6\bin\cspack.exe"/>

 <!--Location for Powershell-->
 <property name="powershell.exe" value="C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe" />

 <!--Location for Nunit Console-->
 <property name="nunit-console.exe" value="C:\Development\Tools\NUnit 2.5.10\bin\net-2.0\nunit-console-x86.exe" />
</project>


Running the NAnt:
  • On the Command Prompt, Navigate to the location: C:\Development\Work\SampleSolution\src\SampleSolution.Build
  • Type ‘nant‘ and hit the ‘Enter‘ key.

BOOM…… BUILD SUCCEEDED

Adding and executing the Selenium WebDriver tests:
  • It’s the time to add the Selenium WebDriver tests to the SampleSolution.Tests project.
  • Context-click or right-click ‘References‘ of ‘SampleSolution.Tests‘ and select  ‘Add Reference‘ to add the Selenium and NUnit frameworks. References area should look like this after adding:
  • Copy and paste the following source code below to the UnitTest1.cs file on searching a text using the Firefox browser:
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
//using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
namespace SampleSolution.Tests
{
 [TestFixture]
 public class UnitTest1
 {
 IWebDriver driver;

 [SetUp]
 public void Setup()
 {
 // Create a new instance of the Firefox driver
 driver = new FirefoxDriver();
 }

 [TearDown]
 public void Teardown()
 {
 driver.Quit();
 }

 [Test]
 public void GoogleSearch()
 {
 //Navigate to the site
 driver.Navigate().GoToUrl("http://www.google.com.au");
 // Find the text input element by its name
 IWebElement query = driver.FindElement(By.Name("q"));
 // Enter something to search for
 query.SendKeys("Selenium");
 // Now submit the form
 query.Submit();
 // Google's search is rendered dynamically with JavaScript.
 // Wait for the page to load, timeout after 5 seconds
 WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
 wait.Until((d) => { return d.Title.StartsWith("selenium"); });
 //Check that the Title is what we are expecting
 Assert.AreEqual("selenium - Google Search", driver.Title);
 }
 }
}
  • On the Command Prompt, Navigate to the location: C:\Development\Work\SampleSolution\src\SampleSolution.Build
  • Type ‘nant‘ and hit the ‘Enter‘ key. Selenium tests are executed in the GUI mode and the test results are updated in the Command Prompt as shown below:

 
Running the same Tests from Visual Studio:
  • Context or right-click on the ‘SampleSolution.Build‘ in the ‘Solution Explorer‘ and select ‘Set as StartUp Project‘ option.
  • Go to ‘Project -> SampleSolution.Build Properties’.
  • Click on the ‘Debug‘ tab and set the ‘Start external program‘ location of Nunit.exe (i.e., C:\Development\Tools\NUnit 2.5.10\bin\net-2.0\nunit-x86.exe in my case)
  • Build and debug the project. All the tests will be executed in the GUI mode.
Advertisements
  1. Chris
    July 5, 2012 at 5:09 am

    Awesome tutorial …Please Post some more

    • July 5, 2012 at 5:20 am

      Thanks! Sure, will post some more interesting stuff 🙂

  2. July 5, 2012 at 10:19 am

    Fantastic, article. Been using MSBuild for a while, haven’t tried Nant yet. Got an excuse to try now. nice work

  3. July 12, 2012 at 4:31 pm

    Very good tutorial in my opinion, is not simple find guides on this arguments, and very clear even for a beginner like me!

    • July 13, 2012 at 12:30 am

      Thanks! Some more topics coming up…. Keep watching 🙂

  4. Iain Hunter
    July 27, 2012 at 10:46 am

    Great post, thanks for using Nant.Builder. Cheers Iain 🙂

    • July 27, 2012 at 11:00 am

      Credit goes to you for creating a great blog on NAnt builder. Thanks! NAnt builder rocks 🙂

  5. Jaswinder
    September 7, 2012 at 2:47 pm

    Great Article! I understand everything but when i tried to run the solution i got this error

    AssemblyInfo file ‘D:\Projects\SampleNant\builds\SampleNant\src\SampleNant.Services\Properties\AssemblyInfo.cs’ could not be generated.
    Could not find a part of the path ‘D:\Projects\SampleNant\builds\SampleNant\src\SampleNant.Services\Properties\AssemblyInfo.cs’.

    • September 9, 2012 at 4:34 am

      It looks like there is something missing in your GlobalBuildSettings.xml and Nant.build file. Can you verify it again?

  6. williams
    October 10, 2012 at 3:14 pm

    Great Article i am getting one error External program failed C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe (return code was 1) any idea for this?

    • October 10, 2012 at 9:07 pm

      Yes, I do remember that I had come across a similar error before. I got it fixed by adding an environment variable (Context-click on ‘Computer’ -> ‘Properties -> Advanced System Settings’)
      ‘EnableNuGetPackageRestore’ and setting it to ‘true’ or ‘1’. Can you give a try?

  7. williams
    October 11, 2012 at 5:53 am

    thanks, it worked for me. One query
    Nant it actually building the solution and coping files to Build Folder ….
    How can we publish MVC Application and move files to released Folder ….

    • October 11, 2012 at 9:31 pm

      You need to make required changes in the GlobalBuildSettings.xml file

  8. Rayo Reddy
    February 25, 2013 at 11:15 pm

    Hello Anoop,thanks for the detailed explanation. I almost succeeded in doing,but I got similar error which Mr.Williams got above 😦
    External Program Failed: C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe (return code was 1) .
    I even added Environment variable which you suggested,still can’t resolve 😦

  9. April 23, 2013 at 6:23 pm

    Hello everyone, it’s my first pay a quick visit at this web page, and article is truly fruitful designed for me, keep up posting such posts.

  10. April 25, 2013 at 3:54 pm

    You are an expert in this topic!

  11. September 19, 2013 at 2:32 pm

    Jaswinder :
    Great Article! I understand everything but when i tried to run the solution i got this error
    AssemblyInfo file ‘D:\Projects\SampleNant\builds\SampleNant\src\SampleNant.Services\Properties\AssemblyInfo.cs’ could not be generated.
    Could not find a part of the path ‘D:\Projects\SampleNant\builds\SampleNant\src\SampleNant.Services\Properties\AssemblyInfo.cs’.

    I’m getting same error ,any solution?
    Thanks Anoop.

    • September 19, 2013 at 11:12 pm

      It is hard to troubleshoot the problem remotely and after looking at your error log I am guessing it is something to do with the GlobalBuildSettings.xml file. Thanks

      • September 20, 2013 at 7:10 am

        I got it fixed.
        Thanks.

  12. September 20, 2013 at 7:22 am

    oops, Now I’m getting this error even after adding Environment Variable.
    External Program Failed: C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe (return code was 1)
    Any Solution to this error.
    Thanks.

    • September 20, 2013 at 2:49 pm

      At last succeeded.
      Happy Build…

  13. July 2, 2014 at 11:11 am

    Hi anoop can u help me how to install nant-0.92. whenever i has been install it is showing some error like NAnt has stopped working.Please what i do now.Can u reply me
    Advance thank you

    rajasekharanmca@gmail.com

  1. July 27, 2012 at 10:45 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: