Importing Config Files with C# NUnit tests

How to use an NUnit Configuaration file to pass values to a test at runtime?

  1. Create your XML Configuration file using the format below. Add your Key,Value pairs in appSettings section
  2. Use the correct file name for your config file
  3. Add a reference to System.Configuration and add a corresponding using statement
  4. Use the ConfigurationManager object to access your parameters in your test code

(1) Basic Config File is in this format:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="TestURL" value="http://www.example.com/"/>
<add key="Browser" value="Firefox"/>
</appSettings>
</configuration>

(2) Config File Naming Scheme
Your config file has to be in the same folder as the test dll, and it must be the same name with .config suffixed. For example if you test dll is called MySeleniumTest.dll your config file must be called MySeleniumTest.dll.config, and it must be located in the same folder.

(3) Add a Reference to System.Configuration
In your Visual Studio project, add a reference to System.Configuration, as well as a corresponding using statement:

using System.Configuration;

(4) Use ConfigurationManager object
Here is an example of how you can access your config parameters in your NUnit tests:

testURL = ConfigurationManager.AppSettings["TestURL"];
browser = ConfigurationManager.AppSettings["Browser"];

Continue reading

Advertisements

Is your JUnit Assert class now deprecated?

Is your JUnit Assert class now deprecated? Well here’s how to fix it. JUnit have moved things around, and as a result the Assert class is now in a different location as and from JUnit 4.11

But fear not, rectifying this is a simple one-liner. Simply change your import statement from

import junit.framework.Assert; 

to this:

 import org.junit.Assert; 

Simples!

Setting up Maven on a Mac

When I started researching about Maven (What is it? Why do I need it? What does it do?) I came across a number of webpages that said that Maven came pre-installed on older version Macs. I have Mavericks on my iMac so I wasnt included in the group that came pre-installed with Maven. There are a set of instructions on the Apache Maven download page (http://maven.apache.org/download.cgi) and a useful YouTube video following these instructions (from Evil Tester Videos, displayed below after the jump.).

Essentially the commands to enter are as follows:

export M2_HOME=/usr/local/apache-maven/apache-maven-3.2.3
export M2=$M2_HOME/bin
export PATH=$M2:$PATH
export JAVA_HOME=/usr/libexec/java_home
export PATH=${JAVA_HOME}/bin:$PATH 

The only problem is these instructions create the environment variables needed for the current session only, they dont tell you how to make these changes permanent. If you reboot or even opena new Terminal window these changes will be lost. These changes must be written to a profile file. There a number of ways to name your profile file and these inclde “.profile”, “.bash-profile” and “.bashrc”. The files that begin with a dot are hidden system files. On a number of webapges the instructions for this step were along the lines of “add these to your .profile” etc. with no indication of how to add the commands to the secret hidden system file. You cant open it with apps like Sublime Text as it cant see the file. (it is hidden after all). A few more sites recommended using vi or emacs. I haven’t used vi or emacs in over 15 years and I wasnt too fond of them then, I certainly wasnt going to reacquaint myself with their unintuitive interfaces if I could help it.

Finally I found the simple answer:

open .profile

Type that into the command line (from your home directory) and it will open the .profile file in your default application, which in my case is TextEditor. I added the above commands to the .profile file and saved it.

But Maven still wasn’t working. the JAVA_HOME variable wasn’t correct. I searched for and found the solution here. I updated the .profile with this and finally, Maven was working! Here are the fixed commands to add to your .profile:

export M2_HOME=/usr/local/apache-maven/apache-maven-3.2.3
export M2=$M2_HOME/bin
export PATH=$M2:$PATH
export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=${JAVA_HOME}/bin:$PATH 

Continue reading

Basic C# NUnit Test Setup

This post will outline the basic structure of how to setup your C# NUnit test. First of all here is the code of the basic empty C# NUnit test file. I’ll explain all of the different parts below.

namespace MyWebDriverTest
{
    [TestFixture]
    public class Class1
    {
        [Test]
        public void TestCase01()
        {

        }
    }
}

The two attributes [TestFixture] and [Test] are used by the NUnit Framework to indicate that there are tests to be executed. The [TestFixture] attributes is used by NUnit to indicate that the class contains test code. This class must be public and must also have a default constructor. The [Test] attribute indicates that the method is a test method. Test methods have to return a void and also must have no parameters.

Everything else is standard C# code for defining a class and a method.

This is the basic setup for a C# NUnit WebDriver test, and we will use this a s starting point for more in-depth tests in later posts.

Adding using statements

As well as adding a reference to the project for Selenium WebDriver and NUnit, you must also add a “using” statement for the sections you are going to use in your test. A good start is to add the following using statements to your file:

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using NUnit.Framework;

Note: If you have more than one CS file in your project, you will need a using statement(s) for each file as required.

This may seem as common sense to the more seasoned programmer but for those starting off at the beginning it is a useful reminder. In the example above I have added a using statement for the standard Selenium WebDriver and NUnit classes and also for the Firefox browser. There are many other possibilities, but I will cover those as we need them.

This screenshot shows where the using statements are usually placed in a C# file:
Using Statements

All Angles Covered

Well a lot of angles covered anyway. As I have a Mac at home and a Win8 PC in work I will be posting examples and tips from both setups. Hopefully it will be a fairly balanced approach. In work I’ll be using WebDriver with the C# programming language, Visual Studio 2013 and NUnit. From my Home setup I will be using Java programming language and the JUnit test framework to write the WebDriver tests, but I am so far undecided on the IDE and build process. For IDE’s Eclipse and NetBeans are free and Intellij is a paid-for application. And the build process can use Maven or Ant. From what I have read online it appears that Maven is the easier one to use to get up and running so I will start with that.

So far I have found the beginning process a lot easier with the C# Visual Studio NUnit approach than with the Java based approach. I think the biggest help for this was my access to Pluralsight training videos, which luckily for me, the trainers all used C#, Visual Studio and NUNit for their test examples in the training courses. I have yet to find a similar resource from the world of Java/JUnit/etc which is part of the reason for writing this blog.