Code Customization In VSTS CodedUi Testing

How to do custom clicking in any particular checkbox in a web table based on specific field value within the one row in the complete table.

Before explaining or walking through the solution statement, I would prefer to explain to you the business scenario we often come across in our day to day automation testing experience during the course of functional testing automation.

Problem Statement:
We have a Table and within table a certain number of rows having certain number of columns. Suppose 5 rows and 3 columns. The first column has an html control in it of type Checkbox, Second column is having a particular field name, suppose product name in it and the third column is having the quantity of the item that has to be purchased. We have to check that particular row’s checkbox which has a particular product name in its product name column field.

Solution Approach:
We can break the problem statement into Five solution components –
1.  Fetch the row count in that particular table.
2.  Figure out the relation between the control id of the checkbox on the row number within the table.
3.  Fetch the row number in the table that has the corresponding Product name in the second column.
4.  Get the control id of the corresponding Checkbox generated based on Step 2 relationship establishment
5.  Set the checked status of the checkbox to True.

Before going through the codes below don ot forget to add the required objects by the below method by using the Add objects to the Ui Control map feature of the UITest Recording menu.


public void CodedUITestMethodCustomization()
{
// To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.

#region Variable Declaration
HtmlControl uiTable = this.UIMap.HierarchyWindow.HierarchyFrame.HierarchyTable;
HtmlCell uiTablecell = this.UIMap.HierarchyWindow.HierarchyFrame. HierarchyTable.uiTableCell;
string strProdTobDel= "Groceries";
string str1;
HtmlCheckBox chkbox = this.UIMap.HierarchyWindow.HierarchyFrame.uiTableCell CheckBox;
// To convert str1 into integer and then increment by one for checkbox recognition as inthis application the checkbox number is one more than the product control id‘s number
int intstr1;
string strCheckBoxId;
#endregion

#region  Get the rowcounts of the table
string RowCount = uiTable.GetProperty(HtmlTable.PropertyNames.RowCount).ToString();
MessageBox.Show(RowCount);
#endregion

#region Search for the Row and store in str1 having the Product to be deleted
uiTableCell.SearchProperties.Remove(HtmlCell.PropertyNames.Id);
uiTableCell.SearchProperties.Remove(HtmlCell.PropertyNames.Name);
uiTableCell.SearchProperties.Remove(HtmlCell.PropertyNames.MaxDepth);
uiTableCell.SearchProperties.Remove(HtmlCell.PropertyNames.ControlDefinition);
uiTableCell.SearchProperties.Remove(HtmlCell.PropertyNames.RowIndex);
uiTableCell.SearchProperties.Remove(HtmlCell.PropertyNames.ColumnIndex);
uiTableCell.SearchProperties.Remove(HtmlCell.PropertyNames.TagInstance);
uiTableCell.SearchProperties.Remove(HtmlCell.PropertyNames.Class);
uiTableCell.SearchProperties.Remove(HtmlCell.PropertyNames.InnerText);
uiTableCell.SearchProperties[HtmlCell.PropertyNames.InnerText] = strProdTobDel;
str1 = uiTableCell.GetProperty(HtmlCell.PropertyNames.RowIndex).ToString();
MessageBox.Show(str1);
#endregion

#region Click Corresponding Checkbox To The User That Is Fetched From Datasheet
strCheckBoxId.SearchProperties.Remove(HtmlCheckBox.PropertyNames.Name);
strCheckBoxId.SearchProperties.Remove(HtmlCheckBox.PropertyNames.LabeledBy);
strCheckBoxId.SearchProperties.Remove(HtmlCheckBox.PropertyNames.Title);
strCheckBoxId.SearchProperties.Remove(HtmlCheckBox.PropertyNames.Class);
strCheckBoxId.SearchProperties.Remove(HtmlCheckBox.PropertyNames.ControlDefinition);
strCheckBoxId.SearchProperties.Remove(HtmlCheckBox.PropertyNames.TagInstance);
strCheckBoxId.SearchProperties.Remove(HtmlCheckBox.PropertyNames.Value);
           
// Increment str by one due to bad coding discipline and resulting mismatch in column and row increments
intstr1 = Convert.ToInt16(str1);
intstr1 = intstr1 + 1;
strCheckBoxId = "ctl00_ContentMain_grdCreateCustRepoUsersModerator_ctl0" + intstr1 + "_chkUser";
MessageBox.Show(strCheckBoxId);
strCheckBoxId.SearchProperties[HtmlCell.PropertyNames.Id] = strCheckBoxId;
strCheckBoxId.Checked = true;
#endregion

}

 
For more on Automation Testing using CodedUitesting in VSTS :Log On To
Automation Testing Using Coded Ui Testing In VSTS

Post a Comment

Previous Post Next Post