Simple C# data driven unit test with csv

In this blog, I wanted to address the problem how we can do data driven testing with csv files in C# unit tests.

We can do looping of for different data sets for sequential data. (Each iteration of test for each row in the data in csv file).

 private TestContext testContext;
 public TestContext TestContext {

 get { return testContext; }
 set { testContext = value; }

 }

For the unit tests in MS Test Frame work, we need to define the testing context,

and the context will return the current context.

Lets see how we can loop taking the example of Calculator class.

Lets assume Calculator class having all methods corresponding to do user actions.

 public UITest Calculator = new UITest();

Lets see how we can use data source, we can do by following code.

 [TestMethod]
 [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV",
 @"|DataDirectory|\data\data.csv", "data#csv", DataAccessMethod.Sequential)]

We can use DataSoruce tag for the data source such as CSV or excel. Its better to use CSV files.

Most often we get error for missing reference. See that you have “System.Data” reference in the project setup.

data

Lets look at the actual implementation and the data structure in the project explorer.

explorer

The data csv contains following for illustration.

datacsv

Following is the test written.

TheTest

We can use Test Explorer to execute it which will produce results for each row.

There by we can get insight of which one got failed.TestResults

Advertisement

Closer look at TestStak.White for windows Application Automation

In the context of UI testing of windows based application, White is well known and free framework.

In the visual studio 2015 we can install White with following nuget command.

Install-Package TestStack.White -Version 0.13.3

Once it is installed, we can use the ui finders and methods corresponding .Net Applications.

Let’s see how we can use the methods of TestStack.White to perform our Calculator testing.

Highlighted command Launches the application exe just like we do from run.

App.GetWindow is the method to get the parent window and it is start point of the whole testing flow as we go through we use window.Find methods to find controls in the parent window.

Another highlighted line in the below image is WaitWhileBusy.

10

There are several methods to do the synchronization in the elements and the properties, which we will be discussing in detail.

White framework extensively uses the finders in the UIA interface of the Microsoft.

Let’s examine following function to get the calculator button by text.

11

(figure 2)

There are different search criteria’s to find the element in the window.

12

As looked highlighted above we are using control type to find collection of all buttons and filter them by the control text.

There are several control types TestStack.White supports. Following is snapshot of general .net controls.

missed

We can also use one finder in conjunction with another finder with “AndBy” commands like below.

missed2

“Window.Get” will get exactly one UI element and if we want collection of elements we can use

“Window.GetMultiple”

Most of the times GetMultiple will be useful where we want to see what is the collection size, and look at all elements and filter them by our needs.

In the (figure 2) there is mention of ExtensionClass, it is implemented for getting objects with wait.

The regular Get Methods does not include synchronization, in the below you can see how we can get elements with wait.

Below is the example where we are checking for length of the Items and periodically checking if length is zero until timeout is reached. We can also include exception handling and return the proper exception if length is zero.

missed3

By using these finders created following functions for clicking buttons and entering text items, selecting listboxes etc. Once all methods are done, its just matter of calling them to execute the tests.

Following is the functions created for calculator functionalities diagram for VS.

code map

 

BDD with Specflow

Let us discuss how we can implement BDD tests we have written with Speckflow tool in C#. If you are familiar with visual studio, you can install specflow with following.
In vs 2015 nuget comes by default and following is the command to install the specflow

Install-Package SpecFlow -Version 2.3.1

Once you install this you can use specflow with either nunit tests or MS unit test framework.
If you wish to use MS Unit Test framework, lets create unit test project.

1

Let us first see how we can implement the methods with Specflow tool we just installed.

Once we get the project structure in the explorer, let’s just add a speflow feature file with following.

Right click on project from project explorer (invoke project explorer if you are not seeing it from view menu). In the Add menu select new item and select specflow feature file.

2

You can directly type the scenarios that you have written in the feature file from my earlier discussion.

Intro to ShiftLeft and BDD in Automation Testing Context

3

The advantage of specflow is that it can generate the steps from what we have written in the feature file. Right click on the feature file editable area and select generate step definitions.

4

Once you select the generate step definitions, it will generate step file with all the definitions like below as raw file. Initially it shows “ScenarioContext.Current.Pending()” indicates that this must be replaced with the definitions of the steps.

5

Specflow Provides the [BeforeScenario] and [AfterScenario] in the context of scenario. We can implement the methods corresponds to it.

Lets assume we have UITest class where we have written all methods corresponding to Calculator.

6

78

Since we have written mock methods in the speflow lets look at how we can implement these steps in the UITest Class.

Code Repository Can be found at SpecFlow Example

Intro to ShiftLeft and BDD in Automation Testing Context

In recent times the shift left is happening trend in the software DevOps.

DevOps is basically a ladder between development and testing which enables continues integration and continues development.

In the context of DevOps shift left testing is getting more focus as the strategy of shift left is to test early and deploy early. (shifting testing left side of the “requirements to development to production roll out” pipe line).

That means the automation testing should happen as and when build is getting generated. The cycle times of test execution must be low so that build can be generated quickly. To do this seamlessly with conventional automated testing tools is a challenge as these tools typically have different UI and not integrated with development environment.

So the ideal solution is to have IDE(Integrated Development Environment) or a plugin for development environments. Be it Microsoft Visual Studio, be it Java Eclipse or be it any other development environment, Test Automation can be performed right from IDE right from the same development environment integrating with favorite unit testing frameworks.

This will be one shop stop for both development and testing.

Selenium is already being great tool for Test Automation right from integrated build environments for C# and Java for Web Applications.

So where will BDD fit, BDD is approach for faster testing and provides transparency in testing “what you write as requirement (feature) is what you test”. Requirements are written as features and features are implemented and tested.

It starts with Features, which all features we want to include. And each features contain multiple scenarios and each scenario can have flavors with different data sets.

Application -> Features – > Secenarios -> DataSets

Let us try to write the feature and scenarios for Calculator application in general to get to better understand, and after that we can see how these features can implemented.

Lets first create feature file as calc.feature  and write following.

 

bdd1

Then lets choose how many types of scenarios we want to cover for this feature and tag them separately.

@Basicoperations

@Advancedoperations

Let us take in each of these operations lets write basic scenarios with keywords Given, When and Then.

Given describes or sets the context of the scenario under stated environment. The best practice is to check the system state. Its like prerequisite for the given scenario.

When describes the state change parameters. Like user actions to move the system from one state to another state.

Then describes the consequence or result caused by the state change. Its like if when is the cause Then is the effect.

And is key word will be used to add multiple whens or multiple Thens.

But is used in the same context as then and mostly used for negative testing.

Here is how a scenario looks like.scenario example

The tag @Basicoperations is like category of the test.

Scenario Outline can contain multiple examples, It is always better to write as Scenario Outline instead of just scenario so that we can specifically mention the parameters and looping of the test data is also possible.

We have several ways of writing the same scenario. Its better to write the scenario as re-usable blocks so that the steps can be re-used.

Let us take another example to see how we can re-use the same steps.

In the below scenarios same steps are used as above scenario and just the operation is being changed.

bdd2

Let us look at @Advancedoperation description and figure out how we can write the detailed re-usable steps for more advanced operations.

advanced

In the above we can see multiple and statements and some of the “And” statements are similar. The above scenario enters data in below form. the multiple And statements enter the values in the respective text boxes.

form

This is very small example of how we can write BDD tests, I will try to explain how this can be implemented in next blog.

Page Object Model – Framework

Most of the times, many project demands start of best fit automation framework from scratch. Page Object Model (POM) is most popular design pattern for the Selenium automation.

In this post, lets try to figure out the details. Before diving in its better to look at this post to get insight of what we are talking here.

Page Object Model – Approach

Lets us look at the following different packages for the purpose of the understanding.

pages contains the test pages for the site, where as each page contains the objects corresponding to that page.

In any UI testing we typically do two things with objects, either we retrieve properties for it or we perform actions on it.

eclipsstructure

In the start section we write necessary code to start the browser. Driver session creation, extent test creation, logger creation etc.

The tests section contains the test classes we are intended to write.

The utilities section contains the common actions class and the reusable action classes.

expandedpackages

Also, its better use config properties for the entire test setup along with the test data sheets. Also maintain drivers in different folder for better readability.

projectStructure

As shown above utilities package contains the excel manipulations such as get and set data operations.

Lets deep dive into start class and what it is doing.

Start class has all declarations and webdriver creation depending the config settings.

Create config file with browser type and other details so that we can use them in the code.

config

Start class will have the entire code for reading the config file and depending on the browser type mentioned in the config it creates the web driver session.

If we look at the declarations, the entire project is using extent reports as it will generate beautiful reports for the test execution.

Also, all other variables are declared as static where as driver is non static. The report logger is also static, since once we create start instance driver will be recreated but not the report logger. This will help to run the same test in multiple iterations keeping the same report.

If we look at the declarations, we are declaring the xldriver, extent report and logger properties as static as they dont need to be re initialized for loop execution. (Static variables are created once, they are not recreated for each instance of class)

start2

the launch_browser method returns the pagefactory object with created driver. This will flow to subsequent pages.

return.PNG

Will see why it is returning the page factory object.

If we look at the pagefactory class, it has definitions of all pages.

We are using pagefactory constructor to send the driver to all the sub subsequent classes.

And, the home and other pages will inherit this class so that they can get the latest driver created by the start class.

pagefactory

Each page class is contains the deification of elements and initialization of elements.

We will use PageFactory class (Selenium specific class) along with ajaxelemetlocator since it has advantage of creating element only when it is being used, we can also define the time duration for the locating element. We are also using reference class object for the common functions.

The reference, and the home class gets the driver created in the start class.

home1.PNG

Also the each class in the page returns the same page for better creation of tests.

home2

And finally coming to writing test cases.

Starting with method name, we are using the data sheet name as test method name.

So to avoid hard coding of the methodname passing to excel setExcelSheet method , we are programatically getting method name and assigning it.

Since xldriver and set_logger method is static, we dont have create start class object to use them.

And looking at the loop, we are getting rows in the excel sheet and running the required actions for each row. That means creating driver doing all actions and logging out.

We can restrict this for certain actions if we dont want to quit the browser, by keeping the creating of start object outside the for loop.

And by keeping (.) at the launch_browser(), we get access to the methods in pagefactory class, and since all other classes are extending pagefactory class, all of them gets access to pagefactory class methods. Thats the reason for writing page navigation methods in the pagefactory class.

This way we can write single line test cases, this will ease the reading of test case and writing and debugging them.

TestCase

the output for the test looks like below.

reports

This is gist of implement page object model with one sample testcase.

In this post we have not included the excel data manulations, hash maps and extent report creation.

We will try to address them in subseqnet posts.

Thank you.