C# 2013 Express Web WebDriver Automation. Introduction tutorial

Similar documents
Ranorex Tutorial. About this Document... 1 Ranorex Installation and Setup... 1 Tutorial Test SimpleCalculator using Ranorex... 2

Content Module. ActiveModeler Avantage. Managing Enterprise Documentation. Version 1.2, 6 May KAISHA-Tec What does the Content Module do?

Registration Guide for the UNESCAP Event Portal

Configure Eclipse with Selenium Webdriver

Managing Your Website with Convert Community. My MU Health and My MU Health Nursing

Chapter 1 Getting Started

Microsoft Word - Templates

Learning and Development. UWE Staff Profiles (USP) User Guide

$DXM DBX Dictionary Maintenance Utility

PrintStream Interface User Guide

Introduction to Ardora

Graphic Selenium Testing Tool

Step 4 Part F - How to Download a Video on YouTube and Delete a Video

Siteforce Pilot: Best Practices

Social Media Testing with Selenium

FITECH FITNESS TECHNOLOGY

Content Publisher User Guide

Step 1 Download and Install KompoZer Step by step instructions to build your first web page using KompoZer web editor.

Login Process and Password Reset

Publisher Onboarding Kit

DOCUMENTUM D2. User Guide

Bringing Together One ASP.NET

Browser Guide for PeopleSoft

The Centrify browser extension

FUNCTIONAL BEST PRACTICES ORACLE USER PRODUCTIVITY KIT


Selenium Webdriver Github

Campaign Manager for Sitecore CMS 6.3

How to Edit Your Website

Using Internet Archive: A guide created by the Digital POWRR Project

Deposit Wizard TellerScan Installation Guide

Browser Support Internet Explorer

How to Edit Your Website

Web Browser Application Troubleshooting Guide. Table of Contents

CHAPTER 1 COPYRIGHTED MATERIAL. Finding Your Way in the Inventor Interface

DB2 for z/os Stored Procedure support in Data Server Manager

Adding a RSS Feed Custom Widget to your Homepage

Class 1 Introduction to Selenium, Software Test Life Cycle.

UNIVERSITY OF YORK. Service Builder. User Guide. Michael Weeks 4/1/2011

Creating Word Outlines from Compendium on a Mac

C1 CMS User Guide Orckestra, Europe Nygårdsvej 16 DK-2100 Copenhagen Phone

How to make an EZ-Robot Tutorial

Getting started with Convertigo Mobilizer

Function. Description

Improving Productivity with Parameters

If you do not have administrator privileges on your computer and are unable to install Java, please reach out to the ITC Help Desk at

A short guide to learning more technology This week s topic: Windows 10 Tips

Behat Drupal Integration Documentation

Page Topic 02 Log In to KidKare 02 Using the Navigation Menu 02 Change the Language

Creating a Technical Writing Online Portfolio with Wikispaces.com

Mend for Eclipse quick start guide local analysis

BT Web Hosting. Features and functionality

FIREFOX MENU REFERENCE This menu reference is available in a prettier format at

Browser Configuration Reference

Version 4.0 February 2011

Step 7 How to convert a YouTube Video to Music As I mentioned in the YouTube Introduction, you can convert a Video to a MP3 file using Free Video To

Learning vrealize Orchestrator in action V M U G L A B

PRACTICE-LABS User Guide

Robix Software, Part I: Quick Start Tutorial

Delete Cookies Windows 7 Internet Explorer 10

Briefing Session Guide. Sending Message Basics.

The Auslan System Sign Editor User Manual

Getting Started with Web Services

Browser Settings. Updated 4/30/ SSF

Let s begin by naming the first folder you create Pictures.

BCI.com Sitecore Publishing Guide. November 2017

Module Road Map. 7. Version Control with Subversion Introduction Terminology

Dealer Instructions for the Product Configurator

Virtual Platform Checklist for WebEx Training Center

Introduction to Moodle

AADL Graphical Editor Design

FrontBase An Omnis Studio Application

IHS > Decision Support Tool. IHS Enerdeq Browser Version 2.9 Release Notes

Community Edition. Web User Interface 3.X. User Guide

How to Use Serif WebPlus 10

MBChB Student E-portfolio Guide Year /18

Cross-Browser Functional Testing Best Practices

Contact: Systems Alliance, Inc. Executive Plaza III McCormick Road, Suite 1203 Hunt Valley, Maryland Phone: / 877.

Recommended Browser Settings

NuGet Package Manager Console PowerShell Reference

Instructions for Editing in the new System DNN9 the Upgrade from DNN8

Chrome if I want to. What that should do, is have my specifications run against four different instances of Chrome, in parallel.

SCHULICH MEDICINE & DENTISTRY Website Updates August 30, Administrative Web Editor Guide v6

APP-J: WHAT IS APPLICATION JUKEBOX?

From the Insert Tab (1), highlight Picture (2) drop down and finally choose From Computer to insert a new image

Getting Started with ExcelMVC

DEKAFLOW Access Upgrade FAQ & Troubleshooting. Frequently Asked Questions Troubleshooting Guide Installation Issues Help

Getting Started Guide

CONFIGURING SAFE V4.0 IN THE IBM COLLABORATIVE LIFECYCLE MANAGEMENT

SLG for School Site Administrators. Applicable to Onwards

Saving Internet Searches and Resources

webdriver selenium 08FE064A22BF82F5A04B63153DCF68BB Webdriver Selenium 1 / 6

Installing and Configuring Worldox/Web Mobile

Developer Documentation. edirectory 11.2 Page Editor DevDocs

Transitioning Teacher Websites

You are Unable to Log into MBEF

KODAK INSITE. Using InSite to Upload Files. Customer InSite Instructions. Supported Browsers & Recommendations:

Editing Webpages in N/Vu

Azon Master Class. By Ryan Stevenson Guidebook #7 Site Construction 2/3

Learning Objectives of CP-SAT v 1.31 (C#)

Transcription:

C# 2013 Express Web WebDriver Automation Introduction tutorial John Sumner 2015

Contents Introduction... 3 Section 1. Introduction to C# 2013 Express for Web... 3 Installation of Microsoft Visual C# 2013 Express for Web... 3 Checklist:... 3 Creating the Test Automation Project... 3 Installing NuGet Packages.... 4 Just a quick note on NuGet.... 4 Selenium WebDriver NuGet Installation... 4 NUnit Installation... 4 Adding a Post Build Step... 5 Ready to code! Implement a Basic Text Fixture... 5 Section 2. Chrome and IE Driver Expansion... 6 Obtaining the drivers... 6 ChromeDriver... 6 Amending the Code... 6 Section 3. Driver with App.config... 7 To add an application configuration file to your C# project... 7 Section 4. Create the Driver Class... 10 A New Class of Driver...... 10 Section 5. Creating the Multi Test Project... 13 Creating the Folder Structure.... 13 Creating the webdriver.cs... 14 Creating the exampletest.cs... 14 Creating the testfixture.cs... 14 Now for the code...... 14 Section 6. Conclusions.... 18

Introduction This tutorial shows the implementation of Selenium using C# Express 2013 for Web available from Microsoft. This set of tutorials is ideal for first time testing engineers starting down web automation, companies who are investigating automation that wish to keep costs to a minimum or even individuals who are looking into personal web automation setups. I have used only tools which are free to download and use in the following sections with the idea that people can see the automation without having to pay for tools or setup. I hope that this setup of tutorials is useful in providing an insight into the world of web automation. Section 1. Introduction to C# 2013 Express for Web Selenium WebDriver is quickly becoming the industry standard testing tool for web applications and sites. Its power is only matched by its flexibility. As such many testing engineers are looking at ways to increase their exposure of Selenium and its becoming more common to expand this beyond the typical Java Environments. One of these is C#. There are many different tutorials out there that combine into solution that runs that if you have the right tools can function. This blog is intended on showing you how to construct a C# environment without any paid tools to complete a solution that allows testing engineers move forwards with Selenium Testing through the use of C#. When completed you will have automated your first website test! Please note that this is a beginners tutorial designed for individuals that do not have access to full versions of Microsoft Visual Studio, and therefore some steps are manual. Company investments in such automation will usually have Microsoft Visual Studio, for which another tutorial will be available later. Installation of Microsoft Visual C# 2013 Express for Web The first component is the IDE (Integrated Development Environment). Since C# is a development language from then the IDE that can be installed is from their Express Range. The Express solutions are free registered editions. For the purpose of this blog I am going to be focusing on Microsoft Visual C# 2013 Express for Web. This is available here. Please note you will have to select the Express 2013 for Web download, and a Microsoft Account is required. Proceed with the installation of the Microsoft Visual C# 2013 Express for Web IDE and the first step of the Automation Suite is completed. For this introduction session, its required you have Mozilla Firefox installed. Whilst Selenium is compatible with other browsers they require drivers which will be covered later in the tutorial. Checklist: Microsoft Visual C# 2013 Express for Web Mozilla Firefox Creating the Test Automation Project With all of the tools installed, its now time to start the creation of the Project. Its worthwhile noting that since we are creating elements of a Project that are manual based on the tools we use, that you might want to consider using this opportunity to create a template project for re-use later, saving you repeating all the steps time and time again.

Start Microsoft Visual C# 2013 Express for Web. You should be presented with the Main screen Once the main screen is loaded at the left you have a New Project option available from the File Menu, Select this. Selecting this will load the Projects that you are allowed to create. On the left you have a set of templates that are installed. One of these sections is Visual C#. Select this to load the templates available in the centre panel. Select Class Library and provide an appropriate name for the Project and press OK. My project name is Selenium.webDriver.Introduction. If you use this it might save you some namespace changes later in the solution. Save the Project. This will save the entire solution in a single run. Solutions are better if later you wish to have multiple projects within a single solution. Installing NuGet Packages. Just a quick note on NuGet. "NuGet is the package manager for the Microsoft development platform including.net. The NuGet client tools provide the ability to produce and consume packages. The NuGet Gallery is the central package repository used by all package authors and consumers." - https://www.nuget.org/. NuGet will allow us to install the Selenium WebDriver and NUnit packages required to create Automation Tests. Selenium WebDriver NuGet Installation 1. Ensure your automation Project is open in your IDE 2. Select Tools -> NuGet Package Manager -> Manage Nuget Packages for Solution 3. In the browser box that appears select Online from the left list 4. In the drop down that appears, ensure nuget.org is selected 5. In the search in the top left type "Selenium WebDriver" and wait for the list to update 6. Select Selenium WebDriver from the list ( check by looking at the Id in the right column which should be Selenium.WebDriver) 7. Once the package has downloaded a dialog will appear asking which projects to include. Leave all ticked and press OK 8. Once installed there should be a tick appear next to the package in the centre list. 9. Next select Selenium WebDriver Support Classes (Id: Selenium.Support) 10. Install with the options as previous if requested NUnit Installation Assuming that you have completed steps 1-5 in the previous list you can install NUnit by : 1. In the search in the top left type "NUnit" and wait for the list to update 2. Select NUnit from the list ( check by looking at the Id in the right column which should be NUnit) 3. Once the package has downloaded a dialog will appear asking which projects to include. Leave all ticked and press OK

4. Once installed there should be a tick appear next to the package in the centre list. 5. Next select NUnit.Runners (Id: NUnit.Runners) 6. Install with the options as previous if requested Adding a Post Build Step Now that we have the correct references in place another ideal feature to have is that NUnit runs its Testing GUI when we build. In order to complete this, we can add a build step that allows us to call the application and pass it our built file, so that all we have to do is start the test. Open the Solution Explorer inside Microsoft Visual C# 2010 Express Edition Right click on the Project and select Properties Select Build Events In the Post-build event command line enter the following "$(SolutionDir)packages\NUnit.Runners.2.6.4\tools\nunit.exe" "$(TargetDir)$(TargetFileName)" /run Once completed, ensure that you save the project. It s worth noting that the application location will change when new versions appear. Please be aware if you upgrade NUnit that this step may require alteration at a future point. This is also the case if the version of NUnit has incremented (at the moment the NuGet version is 2.6.4 as indicated in the path). Ready to code! Implement a Basic Text Fixture Now that all of the components are in place you are ready to code. Inside the IDE if you have not already load the Class1.cs file. This is done by double click, or selecting the tab at the top of the editor if the file is already open. Since this is an introduction to the system I have provided a sample piece of code that will load the Google website using Firefox, check the title is valid and close the driver. This simple test is to verify if you have completed all of the steps correctly. Here is the source code: using System; using NUnit.Framework; using OpenQA.Selenium; using OpenQA.Selenium.Firefox; namespace selenium_examples [TestFixture] public class GoogleTests private IWebDriver _driver; [TestFixtureSetUp] public void FixtureSetup() [SetUp] public void TestSetUp() _driver.navigate().gotourl("http://www.google.co.uk"); [TestFixtureTearDown] public void FixtureTearDown() if (_driver!= null) _driver.close();

if (_driver!= null) _driver.quit(); [Test] public void GooglePageTitle() Assert.AreEqual("Google", _driver.title); Simply copy the above code into your Class1.cs and then press F6 (or select build solution from the Debug menu) If all is correctly in-place the build should be successful, NUnit will start and the test automatically run. Since the NUnit GUI remains open you will be able to check the result of the test. Section 2. Chrome and IE Driver Expansion Obtaining the drivers As with Selenium WebDriver and NUnit, NuGet offers us the capability to install the drivers for Chrome and IE. However these are not offered directly by a single source, therefore there can be some variation on the packages. The two highlighted here are ones recommended from myself and various sources, but however may not always be available with the latest versions. ChromeDriver 1. Ensure your automation Project is open in your IDE 2. Select Tools -> Nuget Package Manager -> Manage Nuget Packages for Solution 3. In the browser box that appears select Online from the left list 4. In the dropdown that appears, ensure nuget.org is selected 5. In the search in the top left type "Chromedriver" and wait for the list to update 6. Select Selenium.WebDriver.ChromeDriver from the list ( check by looking at the Id in the right column which should be Selenium.WebDriver.ChromeDriver) 7. Once the package has downloaded a dialog will appear asking which projects to include. Leave all ticked and press OK 8. 1. In the search in the top left type "IEDriverServer" and wait for the list to update 9. Once installed there should be a tick appear next to the package in the center list. 10. Next select Selenium.WebDriver.IEDriver (Id: Selenium.WebDriver.IEDriver) 11. Install with the options as previous if requested Once these are installed, you now have the capability to interact with Chrome and Internet Explorer. If you are missing Chrome it might be worth installing it to test the results. Amending the Code I will use the example from my previous blog to show how to amend the driver for use with other browsers. Please note these will be code snippets and not the full code file Example Driver Code for Chrome using OpenQA.Selenium; using OpenQA.Selenium.Chrome; // <--- This is the reference declaration

namespace selenium_examples [TestFixture] public class GoogleTests_Chrome private IWebDriver _driver;... [TestFixtureSetUp] public void FixtureSetup() _driver = new ChromeDriver(); // <--- This is the driver to use As you can see there are two areas to edit to use each driver. The first is the reference declaration such that it gives you the correct driver method to use. The second is the call to the driver itself. In the above example the Chrome system has been used. Here are the same examples for Internet Explorer using OpenQA.Selenium; using OpenQA.Selenium.IE; // <--- This is the reference declaration namespace selenium_examples [TestFixture] public class GoogleTests_IE private IWebDriver _driver;... [TestFixtureSetUp] public void FixtureSetup() _driver = new InternetExplorerDriver(); // <--- This is the driver to use As you can see the simple changes to these allow the use of other Selenium driver services without changing any of the testing code. When considering testing automation projects one of the primary considerations is the project setup for the actual testing. In this scenario you can create 3 Separate.cs files to run the same test three times over three different browsers. In a future blog we will show how to move this setting to the app.config so that it can be configured outside of code for use in build and continuous integration systems Section 3. Driver with App.config In order to create configurable automation suites, one of the features of C# is Application Configuration. This is done through a file, which can store values in XML that can be used within the application. For Selenium this means that you can potentially setup your system to be controlled through an application configuration to allow systems to alter any of the start-up variables. The most common for Selenium based projects are the driver and the website to visit. To add an application configuration file to your C# project 1.On the Project menu, click "projectname" properties ( this is the bottom option named after your project) From the options on the left of the window, select Settings

2.On the right, it will say " This project does not contain a default settings file. Click here to create one". Click this. 3. In the new table, set the Name as browser, change the Scope to Application and enter Chrome in the Value field. A file named app.config is added to your project. Once the file is created, you can open the file by double clicking on app.config in the Solution Explorer. This will load the file, and you are looking for the section that looks like the following: <setting name="browser" serializeas="string"> <value>chrome</value> </setting> This means that the configuration has been loaded correctly. Save the project at this point. Once this is completed, the code can then be implemented to allow us to use the App.Config File. Here is the Google Example from the previous Blogs continued to allow the use of the App.config. Here is an example of the code : using System; using NUnit.Framework; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; //<-- Required for the driver switch using OpenQA.Selenium.IE; //<-- Required for the driver switch using OpenQA.Selenium.Firefox;//<-- Required for the driver switch using Selenium.webDriver.Introduction.Properties; //<-- Change This to your name namespace selenium_examples [TestFixture] public class GoogleTests_Chrome private IWebDriver _driver; [TestFixtureSetUp] public void FixtureSetup() //The following switch takes the browser key introduced in App.Config //using ConfigurationManager to access the key in the XML File var browsername = Settings.Default.browser; switch (browsername) case "Chrome": //For each driver there is the driver declaration //then some examples of driver properties that can be set //to customise the environment for automation running. //These properties are all controlled through driver.manage _driver = new ChromeDriver(); case "Firefox": case "IE": _driver = new InternetExplorerDriver();

default: // We also include a default should the configuration file not be // present or something happens to mean that the configuration // cannot be read. In this example there is no protection for a // failed read since the switch statement will fail, only that if // there is a mistake in the file, like incorrect spelling, it // will default to the firefox browser. Console.WriteLine("Defaulting to Firefox"); [SetUp] public void TestSetUp() _driver.navigate().gotourl("http://www.google.co.uk"); [TestFixtureTearDown] public void FixtureTearDown() if (_driver!= null) _driver.quit(); [Test] public void GooglePageTitle() Assert.AreEqual("Google", _driver.title); Once copied and replaced with your existing code it s important to change the following : using Selenium.webDriver.Introduction.Properties; //<-- Change This to your name If for example your project ( not solution) is called Automation, then the line would become using Automation.Properties; //<-- Change This to your name Once completed the line: var browsername = Settings.Default.browser; should not have a red line under, indicating there is a problem. If your solution and project are the same name ( both have the same name in the solution explorer on the right, then this will work). If you are still struggling, In the solution explorer drop down the Properties for the project, and load the Settings.Designer.cs file and use the text between the namespace and. In my example this is : namespace Selenium.webDriver.Introduction.Properties Once you have completed this Save your project and Run it to ensure the Chrome browser is launched as part of the test. As a note, remember to Close the NUnit manager that is loaded or you cannot continue writing code. As you can see from the above example the key inside the app.config file will accept three values. These are:

Chrome Firefox IE If one of these values is incorrect or not entered then the system will default to Firefox. Its worth noting that there are duplicate entries in the switch code which could be refactored out. In future blogs they may well be but for the moment I thought it was important to show that the switch allows the running of independent code lines as well as identical lines. This means that the values could be changed based on browser type running, for example you may wish not to delete the cookies or not maximize the window for Internet explorer. This code allows for that flexibility. Section 4. Create the Driver Class The main focus of this section is around coding using C# rather than the use of Selenium WebDriver. Many testing engineers will feel that they should not be required to code in C# in order to automate, and whilst this is true, the power of C# will allow you to structure the WebDriver in such a way that it will become easier to create, implement and run automation tests. A New Class of Driver... Whilst all of the code is at the end of this section is in a single file, I will provide first some of the elements that are included. The first is the introduction of a second class. This class is to allow the containment of the driver and its indexer such that all of the direct driver manipulation can be done through the use of this class. In future this will include stopping the driver, directing it at a URL, and managing the driver properties. In this section, it simply allows the creation of the driver outside of the test class such that testing engineers can begin to move away from including the driver code in every test and start to construct larger, more defined tests Here is the code for the new driver class and its implemented indexer: using System; using NUnit.Framework; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using OpenQA.Selenium.IE; using OpenQA.Selenium.Firefox; using Selenium.webDriver.Introduction.Properties namespace Selenium_Automation [TestFixture] public class GoogleTests : seleniumdriver [TestFixtureSetUp] public void FixtureSetup() // The Fixture Setup has been removed to allow a central // configuration and use of the selenium webdriver to //allow multiple tests to be run from a single project [SetUp] public void TestSetUp() _webdriver.navigate().gotourl("http://www.google.co.uk"); [TestFixtureTearDown] public void FixtureTearDown() if (_webdriver!= null) _webdriver.quit();

[Test] public void GooglePageTitle() Assert.AreEqual("Google", _webdriver.title); public class seleniumdriver private static IWebDriver _driver; protected static IWebDriver _webdriver get if (_driver == null) var browsername = Settings.Default.browser; switch (browsername) case "Chrome": _driver = new ChromeDriver(); case "Firefox": case "IE": _driver = new InternetExplorerDriver(); default: Console.WriteLine("Defaulting to Firefox"); return _driver; Please remember to update the namespace using Selenium.webDriver.Introduction.Properties; with your own if you have used a different name. As you can see from the above code, the driver setup is moved to a separate C# class. With the code above it means that the driver can be used simply by using the _webdriver method, which will initialise the driver when required using the App.Config key mentioned in a previous page. However this still has some limitations. For example the missing App.config key will prevent the driver running. Also we have a large amount of replicated code inside the driver configuration that for these examples setup the driver for use. As such there are several more basic improvements that can be made to this file so that the driver is better protected at launch and runs our automation correctly. Here is my finished example of the driver class that will allow us to start implementation of not only a single test but multiple C# tests within a single project without the use of additional tools.

using System; using NUnit.Framework; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using OpenQA.Selenium.IE; using OpenQA.Selenium.Firefox; using Selenium.webDriver.Introduction.Properties; namespace Selenium_Automation [TestFixture] // By extending the class to include the seleniumdriver class we can interact with _webdriver public class GoogleTests : seleniumdriver [TestFixtureSetUp] public void FixtureSetup() [SetUp] public void TestSetUp() _webdriver.navigate().gotourl("http://www.google.co.uk"); [TestFixtureTearDown] public void FixtureTearDown() if (_webdriver!= null) _webdriver.quit(); [Test] public void GooglePageTitle() Assert.AreEqual("Google", _webdriver.title); public class seleniumdriver private static IWebDriver _driver; protected static IWebDriver _webdriver get if (_driver == null) var browsername = Settings.Default.browser; if (!String.IsNullOrEmpty(browserName)) switch (browsername) case "Chrome": _driver = new ChromeDriver(); case "Firefox": case "IE": _driver = new InternetExplorerDriver(); default: //this ensures that if there is a mistake with the //config key then the driver will still function by //defaulting to firefox Console.WriteLine("App.config key error."); Console.WriteLine("Defaulting to Firefox");

else //This default mode fires when there is no configuration //available for the use of setup. This means that the driver //will always run. Console.WriteLine("* * * * DEFAULTMODE * * * *"); Console.WriteLine("App.config key not present."); return _driver; //Here is a method for controlling the driver configuration. This allows central //control of the driver configuration and at the same time removes replicated //code from the codebase. internal static void ConfigureDriver() Please remember to update the namespace using Selenium.webDriver.Introduction.Properties; with your own if you have used a different name. Now that the additional code is in place inside our.cs file we have two distinct areas. The First part of the file covers the test, whilst the second, covers the actual control of the selenium driver. This starts to give us a clearer picture between the actual test and the resources used to conduct the test. In the next post it will be demonstrated how to move all of the driver code out of the test file into its own so that we can start to construct test scripts for use with automation. Section 5. Creating the Multi Test Project All of the sections up until this point have primarily been concerned with a single test inside a single project with no real structure behind it. Inside this section, the project will be extended to take advantage of more C# and some NUnit changes to allow the project to become a multiple test project. Creating the Folder Structure. The first step is to create a folder structure for a better consolidation of files. This means we can setup the files relevant topic folders such that we know where to look for items in future. Here are the instructions for the folder: Open the Solution Explorer inside Microsoft Visual C# 2013 Express for Web ( View - > Other Windows -> Solution Explorer ) Right click on the Project and select Add -> New Folder Name the folder "Selenium" Right click on the Project and select Add -> New Folder Name the folder "Setup" Right click on the Project and select Add -> New Folder Name the folder "Tests" These folders will be the base for our new files. Now that the folders are in place, the first step is to move your driver code into a new file. To complete this we need to create a new class file. We

could create a new project and start over, but I think the transition will allow for more understanding. Creating the webdriver.cs The first step is to create the files itself. This is done by : Select the Selenium Folder in the Solution Explorer On the Project menu, click Add New Item. The Add New Item dialog box appears. Select the Class File template Enter the filename webdriver.cs in the filename area click Add. A file named webdriver.cs is added to your project in the selenium folder. This file can then be opened by double clicking the file. This will load it into the editor as normal, allowing us to create the appropriate driver class. Creating the exampletest.cs As with the above we want to create a standard test, such that we have them contained in the tests folder. This means we require a new class file for the tests. Using the same instructions as above to create the previous file, in the Tests folder we created, create a new file called exampletest.cs. Again once this is completed load this file into the editor. Creating the testfixture.cs As well as controlling the tests with a single driver, we are going to move some of the NUnit template so that multiple tests can be run with a single configuration, and that the driver shuts down correctly. This will be explained later when the code is demonstrated. Using the instructions above, in the Setup folder create a new class file called testfixture.cs Now for the code... Since we currently have all of the code in a single file, the intention is to split this between the three files now created. The first is the driver. Since we are running the same session the namespace will remain the same, as will the class name for the driver. I have tried to show below what will happen to your existing class1.cs: using System; using System.Configuration; using NUnit.Framework; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using OpenQA.Selenium.IE; using OpenQA.Selenium.Firefox; namespace Selenium_Automation // - - - - - - - - - - exampletest.cs - - - - - - - - - - - // This will be the last remaining part of the actual test // code, allowing the files to solely concentrate on the // testing structure within the file instead of configuration // and setup [TestFixture] public class GoogleTests : seleniumdriver [TestFixtureSetUp] public void FixtureSetup()

[SetUp] public void TestSetUp() _webdriver.navigate().gotourl("http://www.google.co.uk"); [Test] public void GooglePageTitle() Assert.AreEqual("Google", _webdriver.title); // - - - - - - - - - - exampletest.cs - - - - - - - - - - - // - - - - - - - - - - testfixture.cs - - - - - - - - - - - // This will become the code for testfixture.cs //However the context will change as we want to control the //driver at project level, not test level. [TestFixtureTearDown] public void FixtureTearDown() if (_webdriver!= null) _webdriver.quit(); // - - - - - - - - - - testfixture.cs - - - - - - - - - - - // - - - - - - - - - - webdriver.cs - - - - - - - - - - - // This will become the code for webdriver.cs, which will // function the same as before, but alow the tests to have code //related to the test themselves rather than having driver details //inside public class seleniumdriver private static IWebDriver _driver; protected static IWebDriver _webdriver get if (_driver == null) string driverconfig = ConfigurationManager.AppSettings["browser"]; if (!String.IsNullOrEmpty(driverConfig)) switch (ConfigurationManager.AppSettings["browser"]) case "Chrome": _driver = new ChromeDriver(); case "Firefox": case "IE": _driver = new InternetExplorerDriver(); default: Console.WriteLine("App.config key error."); Console.WriteLine("Defaulting to Firefox"); else Console.WriteLine("* * * * DEFAULTMODE * * * *"); Console.WriteLine("App.config key not present."); return _driver;

internal static void ConfigureDriver() // - - - - - - - - - - webdriver.cs - - - - - - - - - - - As you can see there is code required from the main file to each of its three new siblings. Once this is completed, the three files will act as one for the test creation and setup. Instead of attempting to describe the changes, references and requirements of the three new files, I thought it would be easier to place each of them available in code windows so that you can review them and copy the code as needed. In each of the referenced files here you must have the appropriate code. webdriver.cs using System; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using OpenQA.Selenium.IE; using OpenQA.Selenium.Firefox; using Selenium.webDriver.Introduction.Properties; namespace Selenium_Automation public class seleniumdriver private static IWebDriver _driver; protected static IWebDriver _webdriver get if (_driver == null) var browsername = Settings.Default.browser; if (!String.IsNullOrEmpty(browserName)) switch (browsername) case "Chrome": _driver = new ChromeDriver(); case "Firefox": case "IE": _driver = new InternetExplorerDriver(); default: Console.WriteLine("App.config key error."); Console.WriteLine("Defaulting to Firefox"); else Console.WriteLine("* * * * DEFAULTMODE * * * *"); Console.WriteLine("App.config key not present.");

return _driver; internal static void ConfigureDriver() Please remember to update the namespace using Selenium.webDriver.Introduction.Properties; with your own if you have used a different name. textfixture.cs using NUnit.Framework; namespace Selenium_Automation //Note the Tag implementation here. This is to indicate //that these are test project parameters and not test //ones. Think of this as a test suite setup file. [SetUpFixture] public class Test_Configuration : seleniumdriver //This is what is required to setup before starting //the individual test files. This could include the //command to visit the site if you never plan to change //however its common to leave that in the test so that you //always start the test at the starting point. [SetUp] public void FixtureSetup() //This is the last item to complete once all tests are completed //this differs from the TestFixtureTearDown in that those run at //the end of each test, these are at the end of all tests. The //driver.quit is moved to here, as we wish to reuse the driver //during multiple test setups. [TearDown] public void FixtureTearDown() if (_webdriver!= null) _webdriver.quit(); exampletest.cs using System; using System.Configuration; using NUnit.Framework; namespace Selenium_Automation [TestFixture] // The Class name here always reflects what is shown in the results // and as such should be named appropriately public class GoogleTest : seleniumdriver //The setup is used to make preparations before the test begins //this instance includes a visit to the base page as we may //decide to switch sites between tests, or at least reset the

//same site to ensure the tests are performed from the same //starting point. [SetUp] public void TestSetUp() _webdriver.navigate().gotourl("http://www.google.co.uk"); [Test] //As shown in previous examples the method for the test is what //is displayed in the test results, so as with the class name //this should hold a naming convention that is appropriate to //the testing being performed. public void GooglePageTitle() Assert.AreEqual("Google", _webdriver.title); As you can see I have tried to explain in more detail how the files work in tandem. Make sure you take note of the namespace (as it is the same for all classes) and that the webdriver is extended into the testfixture and testexample classes ( GoogleTests : seleniumdriver at the class name indicates that GoogleTests Extends the seleniumdriver). This allows the driver references to be used. As you can see, in particular with the exampletest.cs, it is a clean template which can be used many times. This will allow you to create multiple tests. Once all of your new files are created. DELETE the Class1.cs file from the project (building with it in will cause conflicts and test duplication) then build the solution using F5 or F6. You should find that the solution builds and the test runs as normal. Section 6. Conclusions. So now we have a project capable of testing multiple tests through NUnit through C#. However there are a couple of steps that were skipped during the setup of this, mainly related to namespace standards. In this Section, I have explained the namespaces to better represent their locations within the project. This will be the last in this series of sections intended on getting testing engineers started with Selenium and C# so this last section is about finalizing your project ready for use. Namespaces... During the coding of the project, I have consistently used the same Namespace (Selenium_Automation). However you will notice as you start to create more and more files inside the folders they alter the namespace. So for example a new class created in the Selenium folder would have the namespace ProjectName.Selenium and as such may cause confusion down the line. Since I have split out the setup for the NUnit tests and the tests themselves, moving them into a sub namespace will create another line in the NUnit GUI and not allow the tests to run. As such Namespace naming conventions have not currently been used, and I rely on a single Selenium_Automation. When creating additional files, it s important to remove any appended namespace tags if you wish to allow the system to run correctly. As a completion for this set of information related to the use of C# and Selenium, I have included a link for all of the files, such that if they are ever needed for project files. This is located here.

This is the last section of the basic tutorials for getting started with Selenium and C# 2013 as from this point it s about coding the tests, further driver interaction etc... All advanced tutorials will assume that you have this level of project in place.