How to handle IE Alert pop-up with CodedUI (C#)

Below code will help to handle IE Alert pop-up-

HtmlButton Submit = new HtmlButton (browser);

Mouse.Click (Submit);

WinWindow pop = new WinWindow(null);
pop.SearchProperties.Add(“Name”, “Message from webpage”, “ClassName”, “#32770″);
pop.TechnologyName = “MSAA”;
pop.DrawHighlight();

WinText text = new WinText(pop);

text.SearchProperties.Add(“Name”, “Some Name”);
String msg = text.GetProperty(“DisplayText”).ToString();

if (msg.Contains(“Data saved.”))

{
Console.WriteLine(“System is displaying ” + msg);
}
else
{
Console.WriteLine(“System is displaying ” + msg);
}

WinButton ok = new WinButton(pop);
ok.TechnologyName = “MSAA”;
ok.SearchProperties.Add(“Name”, “OK”);
Mouse.Click(ok);

How to close browser window in CodedUI (C#)

It is very small action but if you are new with CodedUI and struggling to take off (Opening and Closing the browser) with CodedUI then this post could help you…

To start browser and your application you can use below line of code-

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

Above code will start you browser.

browser.close(); //it will close your browser.

If you have switched to new window and now if you wanted to close only that window then use below code sample-

BrowserWindow newwindow = new BrowserWindow();
newwindow.SearchProperties.Add(“Title”, “Google”);

Mouse.Click(newwindow);

Hope it will help…

How to simulate keyboard input in C# (CodedUI)

In my initial days, I have struggled lot to simulate keyboard inputs. To simulate keyboard inputs in CodedUI as easy as Selenium but sometime it’s hard to find CodedUI solution over the Google. With this post I am trying to break that hardness. So here we go…

if you wanted to send single key…

Keyboard.SendKeys(“{ENTER}”);

or

Keyboard.SendKeys(“{T}”); etc.

If you wanted to send combination keys like ALT+F4.

Keyboard.SendKeys(“{%F4}”);

Code     Key

%          ALT

+           SHIFT

^           CTRL

If you wanted to press particular key 10 times, use below syntax-

//Below syntax will press LEFT ARROW key 10 times.

Keyboard.SendKeys(“{LEFT 10}”);

For more details you can go through this link http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send(v=vs.110).aspx

How to get current date in CodedUI (C#)?

Below code help to get current date-

DateTime currentDate = DateTime.Today;
String DateValue = currentDate.ToString(“mM/d/yyyy”);

If we want to add or subtract days in current date we can use below code-

DateTime currentDate = DateTime.Today.AddDays(Convert.ToInt32(“1″));
String DateValue = currentDate.ToString(“mM/d/yyyy”);

or

DateTime currentDate = DateTime.Today.AddDays(Convert.ToInt32(“-1″));
String DateValue = currentDate.ToString(“mM/d/yyyy”);

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”);
}