Setting Up the Development Environment for Your Test Automation Framework

The following tutorial to create a development environment, is the first step in Creating your own test automation framework using Selenium Webdriver 3, Visual Studio 2015 and NUnit. By completing this step, you will be able to create an automated test with Selenium WebDriver 3 and C#.


Tutorial Overview:

Class 1 – Creating an Automated Test Using Selenium WebDriver 3 and C#

Class 2 – How to Create a Test Automation Framework Architecture with Selenium WebDriver 3

Class 3 – Utilizing Test Automation Framework with Advanced Capabilities


We’ll be starting with setting up the development environment, writing the first test, grasp the principles of test automation and discover the various methods to detect elements. We’ll start off by opening Visual Studio and create a new project – ‘MyFirstTest’.

Setting up the development environment

  1. NUnit – NUnit is a unit testing framework for all .NET languages

NUnit is a unit-testing framework for all .Net languages. Initially ported from JUnit, the current production release, version 3.0, has been completely rewritten with many new features and support for a wide range of .NET platforms.

  1. Selenium WedDriver 3

Selenium is a web application testing framework that allows you to write tests in many programming languages like Java, C#, Perl, PHP, Python and Ruby. Selenium deploys on Windows, Linux, and MAC OS. It is an open-source project, released under the Apache 2.0 license.

Our framework will be supporting the following 3 browser drivers:

  1. Chrome Driver – Google’s Chrome browser.
  2. IE Driver – Microsoft Windows’ Internet Explorer browser.
  3. FireFox Driver – Mozilla’s Firefox browser.

There are various ways to download these resources, we will download them using ‘Manage NuGet Packages’ by a right click on Reference> Manage Nuget Packages.

 

Move on to the ‘Browse’ tab, search for the resources above in the Manage Nuget Packages and download them.  After the installation is complete, the tab will show the following:

Next, we’ll close the NuGet Package Manager and return to our project by clicking on Class 1. We will need to add the NUnit Framework to our project by writing: using NUnit .Framework;

The ‘using’ keyword is serving two main purposes:

  • As a directive, when it is used to create an alias for a namespace or to import types defined in other namespaces.
  • As a statement, when it defines a scope at the end of which an object will be disposed. See using Statement.

Afterwards, at the top of our class we will add the following attribute: [TestFixture]

Which purpose is to mark a class that contains a test and optionally setup or teardown methods.

The next attribute will be: TestFixtureSetUp

This attribute is used inside the [TestFixture] to provide a single set of functions that are performed once, prior to executing any of the tests in the fixture. Now, let’s build this function:

public void startTest() // This method will be fired at the start of the Test

{

}

Next, we’ll need to handle the TestFixtureTearDown attribute, which is used inside a TestFixture to provide a single set of functions that are performed once, after all tests are completed. We can start building this function:

public void endtest() // This method will be fired at the end of the test

{

}

After completing the SetUp and TearDown functions, our environment will look as follows:

using NUnit.Framework;

namespace Test
{
    [TestFixture]
    public class AutomationCore
    {
        // Our Core Test Automation class
        [TestFixtureSetUp]
        public void startTest() // This method will be fired at the start of the test
        {
         
        }
        [TestFixtureTearDown]
        public void endTest() // This method will be fired at the end of the test
        {
           
        }

    }
}

At this stage, we are finally ready to start writing our first basic test by writing a new function and using  the [Test] tag above it. The [Test] attribute marks a specific method inside a class that has already been marked as a TestFixture, as a Test Method. For backwards compatibility with previous versions of Nunit, a test method will also be found if the first 4 letters are “Test” regardless of case.

Now let’s create a test that will open our browser and enter into our blog:

[Test]
public void blogTest()
{
   
}
 

However, before we start with the content of the test, we’ll need to restart our drivers and create a variable: IWebDriver driver which we’ll create when configuring the function.

Add the following commands inside the startTest() function:

1

2

driver = new ChromeDriver();

driver.Url = “http://blog.testproject.io/”;

The first command creates a new instance of our Chrome driver and the second navigates to our blog’s page. Now, we’ll get back to writing the test itself in the function of the test. Until this point, we were taking care of the setup of the test, we will continue to the function, by writing the following command ,which receives the browser’s title and confirms that the word ‘blog’ exists.

Assert.IsTrue(driver.Title.Contains(“blog”));

In case it does exist – the test will pass.

In case it doesn’t exist – the test will fail.

Now, we’ll be building the project and we’ll notice the test appears in Visual Studio’s ‘Test Explorer’ window.

 

 

 

 

 

 

 

 

Finish off by running the test by clicking on ‘Run’. This will be the project’s code:

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using NUnit.Framework;

namespace Test
{
    [TestFixture]
    public class AutomationCore
    {
        IWebDriver driver;

        [TestFixtureSetUp]
        public void startTest()
        {
            driver = new ChromeDriver();
            driver.Url = "http://blog.testproject.io/";
        }

        [TestFixtureTearDown]
        public void endtest()
        {
            driver.Quit();
        }
        [Test]
        public void blogTest()
        {
            Assert.IsTrue(Pages.contactUs.isAt());
        }
    }
}




You have just completed the environment for your test automation framework, move on to the next step:How to Inspect Web Elements & Methods to Locate Them with Chrome Devtools.

-I’d be happy to read all your comments, thoughts and ideas!