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

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.

 

 

 

Synchronization in Automation Testing with selenium.

When you ask selenium tester what is the most common issues he is facing during his automation testing experience, the first thing he would say is Sync problem.

Let’s take a bird eye view on this and will take deep dive on the synchronization issues and how to address them.

Selenium framework has several methods to address the synchronization issues. Mainly they are segregated into two different varieties.

  • Implicit waits
  • Explicit waits

Implicit wait:

This is the most common wait type we use; it is also called as default wait. The page load time out, and the timeouts.implicitwait is the two types of implicit wait. The implicit wait meaning that the driver has to wait until the web element is existed on the dom. It will come out of the waiting loop if element exists beforehand. Example you have given 10 seconds on element x, and it has loaded in 2 seconds web driver knows it and goes to next step without waiting for 10 seconds. It is called as implicit wait.

driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

Explicit wait:

Explicit waits are defined on the particular web element to wait until the property value becomes true.

It will pole every 500 milliseconds to get the property value and verifies it with defined value.

Examples:

//webdriver wait class
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath(menu))));

//for multiple elements
WebDriverWait wait = new WebDriverWait(driver, 30);
List options = driver.findElements(By.xpath(option_to_select));
wait.until(ExpectedConditions.visibilityOfAllElements(options));

//Another example
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.invisibilityOfElementWithText(By.className("jqx-fill-state-normal"), "Loading..."));

Fluent wait:

Fluent waits also called as explicit waits, but only difference being we can define interval to pole and can tell to ignore any exceptions. It will be useful to save resources not to check every 500 milliseconds if element load takes long time.

Wait<WebDriver> fluentwait = new FluentWait<WebDriver>(driver)
                        .withTimeout(20, TimeUnit.SECONDS)
                        .pollingEvery(1, TimeUnit.SECONDS)
                        .ignoring(java.util.NoSuchElementException.class);
fluentwait.until(ExpectedConditions.invisibilityOfElementWithText(By.className("jqx-fill-state-normal"), "Loading..."));

The another type of wait is normally not recommended is static wait or pause the program.

Thread.sleep(500);

So, the gist is the know when to use what. Fluent wait is used when application slower and running in a slow paced machine where we don’t want driver to hit every 500 milliseconds, and you know it will not come till 2 sec or 3 seconds, we can define the polling interval. I am guessing this as it is synonymous to implicit wait where the default polling is 500 ms.

We can use web driver wait almost in all cases where we know element loading is taking place, but the properties are not loaded until some other action is completed, example, state dropdown is enabled only when country drop down is selected, in that case we can use expected conditions on the enabled of the state dropdown after country select.

Default waits or implicit waits are used when we know dom loading is taking some time and not to give wait on existence of each element. Normally this would serve the purpose, but angular applications waits plays key role in test execution failures.

If we use waits wisely and where ever we need depending the application behavior with limited use of static waits will improve the script quality drastically.

Thank you.

Selenium – Overview

Introduction

Hello,

Recently, I met one of my colleague  who is preparing for Automation Test interviews.

As I do take interviews in my organization, I thought why not I throw my two cents on this topic. As the thought goes into action you see me here. I hope it helps people who are preparing for interviews and automation learners.

Every interviewer’s perception and thought process for selecting a candidate will be different. In my view one should know below three areas of the automation testing for that matter in any technology or the software we are learning or planned to learn.

  • Conceptual details :
    • This would cover the concepts of the tool/technology what we are planning to use where does this tool exactly fit in place.
  • Technical details:
    • This will give us complete information of the tool/technology implementation details.
  • Managerial details
    • This is the last area but the most important area, this will give us picture of what are the efforts and risks involving this tool and this implementation.
    • At the end this is the most deciding factor for choosing the technology and tool that we are planned to use.

In this blog I tried to put focus on above three mentioned areas and come up with general questions that an interviewer might ask.

and the details

Conceptual

What are the factors to choose the Selenium vs other tools ? why not UFT or other tools ?

  • Selenium is free library and driver owners are constantly updating the driver exes
  • Selenium is best suited for multiple browser testing with Java/.Net/python etc, if we have skilled resources available we can always utilize them for building the automated tests
  • Selenium can be integrated easily with BDD with Cucumber for business driven development for modern development environment
  • It can be easily integrated with CICD process with Jenkins or with TFS
  • It is not suited for the applications involving interactions with windows UI objects.
  • Auto IT is the free tool combined with Selenium is widely used for limited windows operations and more html DOM operations.
  • UFT tool is very much suited for HP tool set environment like ALM. It is also most widely used tool in the automated testing space, as it is licensed tool and license is costlier Its slowly loosing the market share. It only supports vb where as Java resources much widely found. Distributed testing can be easily done with this combination as test management tool like ALM, it has capabilities to run scripts on multiple host machines.
  • Selenium Grid can be used for distributed testing, if Jenkins is used, we can configure multiple Slave nodes containing different browsers and operating systems.
  • If regression suite size is large it would be good to use frame works like testNG or cucumber for better maintainability.

 What is Jenkins and CICD process how does Selenium fit in this ?

  • CICD is continuous integration and continuous development
  • It is the integrated part of modern fast paced development methodologies like Agile Scrum.
  • CICD , CI part of it is being when build gets generated there will be set of automated tests will run to verify the build stability.
  • Jenkins is the tool which is widely used to build the code in distributed (there are lot of other tools which supports CICD environment) we can add build steps in this tool
    • Post build operation being triggering the automated build
    • Jenkins will receive feedback from the automated tests and it will mark build pass/fail/warning based on the test results.
    • CD part being development of new code/ enhancements based on the failures or new features.

Technical:

What is selenium ?

  • Selenium is code library that has methods developed to interact with modern browsers.
  • The actions on the browsers are performed by driver exes. Selenium methods can talk to browsers through these driver exes.
  • We can send or receive information from driver exes through the selenium methods.

What is xpath ?

  • Xpath is the general location of an web element in the HTLM dom (document object model)
  • This will tell us the location or generalized location of element or elements in the DOM tree
  • This is most widely used and modern browsers supports the xpath search.
  • This is fast and not mostly reliable as the dom structure will dynamically change in the modern web technologies.

What is css selectors ?

  • Css selector is the why selenium identifies the object similar to Xpath
  • Selenium can read the css files and it can identify the objects based on css classes.
  • Css selectors and xpaths are most commonly used methods to identify the object

In web application driver.findement(by.xpath) not recognizing te objects. Application is html based we can see element in the dom, what might be the issue ?

  • The most common reason for this is being that element is in iFrame.
  • We need to switch the driver to that iFrame
    • Before that we need to find all elemnts with iFrame tag get either ids or names of the frames and switch to that frame
    • With Driver.switchto
    • Then if we use find elements we can find the element.

What is TestNG xml ?

  • TestNG xml is the source or driving the test suites build on TestNG framework
  • We can give test classes and test groups information to run the tests.

How can we identify a link in the application ?

  • Link can be identified with by.linktext or by.partiallinktext

What are the different ways to identify elements ?

  • By.TagName
  • BY.Xpath
  • By.cssseector
  • By.classname

How do handle elements with in the elements say for example data in table ?

  • We can iterate the findelements with in the elements like below
  • First find Elements with tag name tr
List <WebElement>rows = Driver.FindElements(By.tagnme(“tr”))
for (WebElement row :rows)
 {
   List<WebElement> dataincolumns = Row.findelements(By.tagname(“td”))
 }
  • This is just illustration that we can  do following
Driver.FindElement(By.tagname(<locator>)).FindElement(By.xpath(<locator>).FindElement….. etc

How can we get the text in the textbox

  • We use element.getAttribute to get the attribute of elements such as value and color
  • Element.getAttribute(value) will give us the text of the element.

What are actions ?

  • Actions is class for mouse interactions
  • Some of the web elements need to be hovered to get the list in the common web page designs.
  • We need actions class to perform those operations.
  • Examples:
    • Hovering
    • Mouse move

what is test framework ?

  • test framework is well defined process for creating, execution reporting the automated tests.
  • We see lot of customized framworks in each organizations and some free framework tools also available
    • Junit
    • TestNG
    • Cucumber
  • The main concepts for designing framework should be
    • Process to easily create tests
    • Process to pass data to tests
    • Process to easily maintain the tests
    • Process to generate required reports

Managerial

How can we report with selenium

  • Reporting is bit of crude in selenium, if we use standard frameworks we can get html report
  • Generally we create expected result and actual result in array lists and at the end of the tests we flush them to excel.

How can we manage the code repository if many people are working

  • Selenium development of tests is same as any development project.
  • We use SVN or TFS code management tools to make regular check ins and check outs.
  • SVN is free , and most widely used code management tool, for Microsoft technologies we use TFS as code management.

What is Maven ?

  • Maven is the repository for Jar files. Jar files are nothing but java libraries which has functions or methods readily available for usage.
  • When building a project with selenium we require to use standard java libraries for different purposes
  • Examples:
    • testNG jar file
    • Apache poi jar file
  • If multiple people are using the project, sharing of these libraries will become challenge.
  • Maven project will remove this dependency by adding all dependent jar files directly from Maven repository.
  • Maven will create local repository and down loads all required jar files in the local system. We need to make sure we add all dependencies in the pom.xml.

These are some of the commonly asked High level overview questions in the Selenium interviews. I see we did not cover everything in very detail as it is just overview.

I will try to cover each area with important aspects in my next blogs. Thank you.

Page Object Model – Approach

In selenium, most frequently we hear page object model.

I have asked people why did they call it as page object model ? Is there any specific reason behind this ?

I got general answer saying “we store all locators in a page as static final strings in a page class”

Is this correct explanation ? are we doing page object model as is ? below is another view point.

In general we call page object model because every method in a page returns the page object instead of string/float/int or any standard return types.  This will provide flexibility in writing test cases easily.

Step 1 : Define Page class

package test.pom;
public class page1 extends pageRep
{  
public page1() 
{
super(); 
} 
public page1 testM1() 
{ 
System.out.println("Method 1 Page 1");  
return this; 
} 
public page1 testM2() 
{ 
System.out.println("Method 2 Page 1");  
return this; 
} 

}

Create another page to illustrate navigation between pages.

package test.pom;
public class page2 extends pageRep 
{
 public page2() 
{ 
super(); 
} 
public page2 test_M1() 
{ 
System.out.println("Method 1 Page 2");  
return this; 
} 
public page2 test_M2() 
{ 
System.out.println("Method 2 Page 2");  
return this; 
} 
}

If you observe clearly, the above classes are returning the page class as object and extending the class page repository. The class page repository is the class where we use to navigate between the pages.

Step 2: Create page repository for navigation between pages

package test.pom;
public class pageRep { 
public pageRep() 
{
 System.out.println("Calling the super class"); 
}
public page1 navigateToPage1() 
{ 
return new page1();
} 
public page2 navigateTopage2() 
{ 
return new page2(); 
} 
}

Step 4 : While creating the test case you can see it will be simple as we can keep “.” to see the methods in the class as each method returns the class object again, you will have visibility of all methods.

Page repository helps us to navigate between pages.

This is the best illustration of the Java extends concept which we can use to write our tests in selenium.

package test.testcases;
import test.pom.pageRep;
public class tests {
public static void main(String[] args) 
{ 
 pageRep start = new pageRep();
 start     
.navigateToPage1().testM1().testM2()
.navigateTopage2().test_M1().test_M2()     
.navigateToPage1().testM1(); 
 }
}

output:

Calling the super class
Calling the super class
Method 1 Page 1
Method 2 Page 1
Calling the super class
Method 1 Page 2
Method 2 Page 2
Calling the super class
Method 1 Page 1