How to do data driven testing in CodedUI (C#) using Excel.

Data Driven Testing, where we driver our test script with different sets of data to validate different functionalities of the application.

CodedUI tool has in-built support to do data driven test from different sources, however I am going to put some light on NPOI libraries which helps to write your own code to develop driven driven test. NPOI is .NET version of POI Java project and it is totally free to use.

Before starting, you should have latest NPOI libraries which you can download from here… https://npoi.codeplex.com/releases

Now you have to include NPOI libraries under your CodedUI Project > References and provide the reference in your C# class file also.

You are all set and now paste below code in your class file…

FileStream file;
private HSSFWorkbook ExcelWorkbook;
private ISheet ExcelSheet;
private ICell Cell;

public class TestDataFromExcel{
[TestMethod]

public void getData(){

try{
file = new FileStream(“D://WorkBook/TestData.xls”, FileMode.Open, FileAccess.Read);
ExcelWorkbook = new HSSFWorkbook(file);
ExcelSheet = ExcelWorkbook.GetSheet(“Data”);
Cell = ExcelSheet.GetRow(0).GetCell(0);
String Username= Cell.StringCellValue;
Console.WriteLine(Username);
} catch (IOException e) {

Console.WriteLine(“Exception caught: {0}”, e);
}

}

}

With above code, we can read any excel file (.xls) after providing Workbook location, Sheet Name, Row Number, and Cell Number.

How to find if object is displaying in web page with CodedUI (C#)

Below code will help to determine that if object is displaying or not…

BrowserWindow browser = BrowserWindow.Launch(new System.Uri(“http:www.google.com”));
UITestControl Object = new UITestControl(browser);
Object.SearchProperties.Add(“Id”, “Object_ID”);
String objStatus = Obj.Exists.ToString();
if (objStatus == “True”)
{
Console.WriteLine(“Next button is available”);
}
else
{
Console.WriteLine(“Next button is not available”);
}

How to read html table cell data with CodedUI (C#)

Below code will help to get the data from table cell…

BrowserWindow browser = BrowserWindow.Launch(new System.Uri(“http://YourWebApplicationURL.com”));

HtmlTable table = new HtmlTable(browser);
//Below line will identify the table object.
table.SearchProperties.Add(“Id”, “Table_ID”);

for (int i = 1; i <= 1; i++)
{
for (int j = 1; j <= 8; j++)
{
HtmlCell cell = new HtmlCell(table);
cell.FilterProperties[HtmlCell.PropertyNames.RowIndex] = i.ToString();
cell.FilterProperties[HtmlCell.PropertyNames.ColumnIndex] = j.ToString();

//Below statement will get the value of row 1 and cell 1

if (i == 1 && j == 1)
{
Cell1 = cell.InnerText.ToString();
}

}
}
if (Cell1.Trim().Equals(“12345678″))
{
Console.WriteLine(“Pass”);
}
else
{
Console.WriteLine(“Fail”);
}

How to get current page URL with CodedUI (C#).

Below code will help to get current page URL by using C# (CodedUI).

BrowserWindow browser = BrowserWindow.Launch(new System.Uri(“YourAppURL.com”));

Microsoft.VisualStudio.TestTools.UITesting.HtmlControls.HtmlDocument PageObject = new Microsoft.VisualStudio.TestTools.UITesting.HtmlControls.HtmlDocument(browser);

String URL = PageObject.PageUrl.ToString();

System.Console.WriteLine(URL);

Dependencies with TestNG

‘TestNG’ is a framework that built with different set of APIs that facilitates many testing needs. It has variety of features that makes ‘TestNG’ more convenient than ‘JUnit’ framework. Among all the features, TestNG has ‘dependencies’ feature that gives the facility to determine the @Test annotation methods order. For the dependencies, TestNG uses ‘dependsOnMethods’ option for @Test annotation.

Hold on, should not confuse with ‘order’ word. I do not have meant that with this feature you could create your own order to run the Test methods.

TestNG dependency feature does not give guarantee on the order on which the methods depended upon will be run, however it guaranteed that TestNG will run all the @Test methods before the method which contains the ‘dependsOnMethods’ option.

In TestNG, we can achieve dependencies in two ways-

  • With Annotation.
  • With XML

It has two types also-

  • Hard Dependency
  • Soft Dependency

Hard dependency says that if depended upon @Test method fails then TestNG will skip the depended @Test method.

Soft dependency says that TestNG will run the depended @Test method if depended upon @Test methods fails also, however depended @Test method should have ‘alwaysRun=true’ option.

Hard Dependency Instance:

import org.testng.annotations.Test;

public class dependsOnMethods {

@Test

public void sub (){

int a = 7-3;

System.out.println(“sub “+a);

}

@Test (dependsOnMethods={“sub”})

public void div (){

int a = 10/2;

System.out.println(“div “+a);

}

@Test

public void add (){

int a = 7+3;

System.out.println(“add “+a);

}

}

[TestNG] Running:

C:\Users\testng-eclipse-224652221\testng-customsuite.xml

add 10

sub 4

div 5

PASSED: add

PASSED: sub

PASSED: div

TestNG runs all the @Test methods before the ‘div’ method.

Soft Dependency Instance:

import org.testng.annotations.Test;

public class dependsOnMethods {

@Test

public void sub (){

int a = 7-3;

System.out.println(“sub “+a);

}

@Test (alwaysRun= true, dependsOnMethods={“sub”})

public void div (){

int a = 10/2;

System.out.println(“div “+a);

}

@Test

public void add (){

int a = 7+3;

System.out.println(“add “+a);

}

}

TestNG will run the ‘div’ method if ‘sub’ methods fails also.

With Annotation Instance:

import org.testng.annotations.Test;

public class dependsOnMethods {

@Test

public void sub (){

int a = 7-3;

System.out.println(“sub “+a);

}

@Test (dependsOnMethods={“sub”})

public void div (){

int a = 10/2;

System.out.println(“div “+a);

}

@Test

public void add (){

int a = 7+3;

System.out.println(“add “+a);

}

}

‘div’ method is depended upon the ‘sub’ method. So whenever TestNG runs this class, all methods will run before the ‘div’ method.

[TestNG] Running:

C:\Users\testng-eclipse-224652221\testng-customsuite.xml

add 10

sub 4

div 5

PASSED: add

PASSED: sub

PASSED: div

With XML Instance:

To achieve dependency with XML, we should have testing.xml file. In testing xml file we can specify our group dependency with tag.

<test name=”regression suite”>

<groups>

<dependencies>

<group name=”sanity”/>

<group name=”functional” depends-on=”Sanity” />

<group name=”integration” depends-on=”Sanity  functional” />

</dependencies>

</groups>

</test>

The <depends-on> attribute contains a space-separated list of groups.

How to configure Selenium WebDriver

To configure the selenium on your system, follow the below given steps-

  • Java should be installed on your system.

Java

Eclipse

Eclipse1

  • Create a java project.

JavaEE

  • Select Java Project > Click on Next button > Enter Gmail in Project Name edit field. > Click on Next button > Click on Finish button.

Eclipse2

  • Now configure the Selenium WebDriver API with your Gmail project.
  • Select the Gmail project and right click > Select Build Path > Click on Configure Build Path…>Click on Add External JARs… button > Browse the extracted Selenium folder and include all Executable Jar file > Click on OK button.

Project

  • Now selenium is configured with your java project.
  • Create a Login class.

Class

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class Login {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

WebDriver browser;

browser = new InternetExplorerDriver ();

browser.get(“http://www.gmail.com&#8221;);

}

}

Script

  • Press ctrl+f11.
  • Now you are ready to write your own code to automate your web application.

How to Launch IE browser with Selenium WebDriver on Windows 7

Steps:

 SEL

  • Add the path of downloaded zip file in Environment Variable.
  • Sub Steps: Start > Computer (Right Click on it) > Click on ‘Properties’ > Click on Advanced system settings > Click on ‘Environment Variable’ button > Click on ‘New’ button which displays under ‘System variables’ > Give Variable name: webdriver.ie.driver > Give full path of downloaded IE Driver Server folder in ‘Variable value’ > Click OK > Click OK > Click OK.

EV

  • Open IE Browser > Tools > Internet Options > Security > Check the Enable Protected Mode (require restarting Internet Explorer) check box.

Now we can open IE browser with the help of Selenium WebDriver.