Archive

Archive for June, 2014

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

June 29, 2014 25 comments

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
%d bloggers like this: