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#
- You’re here→Setting Up the Development Environment for Your Selenium Automation Framework
- How to Inspect Web Elements & Methods to Locate Them with Chrome Devtools
- How to Find Web Elements with Selenium WebDriver
Class 2 – How to Create a Test Automation Framework Architecture with Selenium WebDriver 3
- How to Create a Cross Browser Compatible Testing Framework
- Steps to Develop a Report Module in a Testing Framework
- Implementing a Report Module in an Automated Framework
- How to Write a Functional Test with a Basic Selenium Automation Framework
Class 3 – Utilizing Test Automation Framework with Advanced Capabilities
- Page Object Pattern: Advantages & Implementation
- Read Data From CSV File in C#
- How to Create a Test Suite in Selenium WebDriver and C#
- How to Use Data Driven with Selenium Test Suite
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
-
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.
-
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:
-
Chrome Driver– Google’s Chrome browser.
-
IE Driver – Microsoft Windows’ Internet Explorer browser.
-
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 = “https://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 = "https://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!
You never mentioned what kind of project to open to set this up. Could you please start a step or two earlier.
Hey Iamkaramia,
You need to open a Unit Test Project:
https://docs.microsoft.com/en-us/visualstudio/test/getting-started-with-unit-testing
You also did not mention how to install the NUnitTestAdapter, which is essential in order to be able to see the tests in Test Explorer.
You can install it afterwards with the extension manager, or add it as a NuGet package.
https://github.com/nunit/docs/wiki/Adapter-Installation
Hi Asya,
Thanks for this tutorial. It was very helpful! I have a question for you though. Pagefactory is going to be deprecated very soon. Can you please show us how to modify the existing code in your tutorial based on that? I am asking about these specific lines:
private static T getPages() where T : new ()
{
var page = new T();
PageFactory.InitElements(Browsers.getDriver, page);
return page;
}
Hi Asya Galinsky ,
Any video tutorial, then do share it.
From this tutorial not able to create any framework.
Thanks!!
Hi All,
If you install NUNIT 3 , [TestFixtureSetUp]and [TestFixtureTearDown] will not work for you (they are deprecated) , you should change them respectively to [SetUp] and [TearDown] as per nunit update (https://github.com/nunit/docs/wiki/SetUp-and-TearDown-Changes).
Also you need to install NUnitTestAdapter (use Nuget Package) to run and see your test running in visual studio.
Thank you.
Hi,
I’m getting the CS5001 – doesn’t contain Static ‘Main’ method while trying to run this.
Also, getting CS0103 – The name ‘Pages’ does not exist in the current context, on the Assert.IsTrue line.
Does anyone still monitor this that could help?