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(“{T}”); etc.

If you wanted to send combination keys like ALT+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

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


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…

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{

public void getData(){

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

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

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(“”));

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

String URL = PageObject.PageUrl.ToString();