Paul Cunningham
Paul Cunningham

Reputation: 115

Coded UI Test - Click Control Based On Other Things On Table Row

im making a Coded UI test in Visual Studio 2010 for a web application in C# and i wish to click a button in the 6th column of a table based on the inner text of column 1? is this possible?

So for instance i have a table with Names in column one and other info and then a button in column 6. All auto generated.

Im assuming if i can get the Row number from the cell with say "John Smith" in it i can then press the button for this row in column 6. Any ideas? i have tried googling and looking at what parameters i can pass in but im coming up stumped.

Upvotes: 3

Views: 6671

Answers (3)

Nick Raverty
Nick Raverty

Reputation: 431

I have a couple of extension methods that I use to tackle content in tables(Hand coding, rather than using the recorder) -

This extension method for a table gets the first row in the table that contains the requested text in one of its cells or controls

public static HtmlRow GetRow(this HtmlTable table, string cellContent)
{
  if((UITestControl)table == (UITestControl)null)
    throw new ArgumentNullException("table");

  if(cellContent.Length > 80)
    cellContent = cellContent.Substring(0, 80); //Our table cells only display the first 80 chars

  //Locate the first control in the table that has the inner text that I'm looking for
  HtmlControl searchControl = new HtmlControl(table);
  searchControl.SearchProperties.Add(PropertyNames.InnerText, cellContent);

  //Did we find a control with that inner text?
  if(!searchControl.TryFind())
  {
    //If not, the control might be an HtmlEdit with the text
    HtmlEdit firstTableEdit = new HtmlEdit(table);
    //Get all of the HtmlEdits in the table
    UITestControlCollection matchingEdits = firstTableEdit.FindMatchingControls();
    //Iterate through them, see if any have the correct text
    foreach (UITestControl edit in matchingEdits)
    {
      if(cellContent == ((HtmlEdit)edit).Text)
        searchControl = (HtmlControl)edit;
    }
  }

  //We have(hopefully) found the control in the table with the text we're searching for
  //Now, we spiral up through its parents until we get to an HtmlRow
  while (!(searchControl is HtmlRow))
  {
    searchControl = (HtmlControl)searchControl.GetParent();
  }

  //The control we're left with should be an HtmlRow, and should be an Ancestor of a control that has the correct text
  return (HtmlRow)searchControl;
}

Once you're able to get the correct row, it becomes relatively easy to get the correct control in that row(Or the correct control in a given cell in that row)

In your example, you've got a button in the 6th column of the row. The button probably has some properties associated with it, but even without them if we can correctly limit our search it will still work. Assume our table is named UITableCustomers - Declare a new button and limit it to only the 6th(Index 5) cell in the row containing "John Smith"

Mouse.Click(new HtmlInputButton(UITableCustomers.GetRow("John Smith").Cells[5]));

Obviously, this call is going to fail if the given text doesn't exist in a control in the table.

Upvotes: 2

AdrianHHH
AdrianHHH

Reputation: 14076

Something based on the following may help.

Access the HTML table by copying code from that generated by the Coded UI recorder for an assertion or a click on the cells. You should have something like:

HtmlCell theTable = new HtmlCell(this.UItheWindow.UItheTable.UIItemTable);

Cells of the table can be accessed by adding properties such as:

theTable.FilterProperties[HtmlCell.PropertyNames.RowIndex] = "0";
theTable.FilterProperties[HtmlCell.PropertyNames.ColumnIndex] = "6";
UITestControl cell = theTable.Find();
HtmlCell wantedCell = (HtmlCell)cell;

The above might be used as the body of a method returning the value of wantedCell. The name should now be available as:

wantedCell.InnerText;

Accessing the button to be clicked should follow a similar approach.

Another approach uses the GetChildren method to traverse the table. Start by getting theTable as above. Then have something like

UITestControlCollection children = theTable.GetChildren();

Loop through the children checking row properties. When the required row is found, call the GetChildren of that row and get the loop through them to find the required column. Some points: You may need to loop on columns before looping over rows. You may be able to directly index into the UITestControlCollection for the rows and the columns rather than needing to loop and check values. Depending on exactly how the table was written there may be additional levels between the table and the wanted cells, so you may need to examine children, grand children, great grand children, great great ..., and so on.

Upvotes: 4

that guy over there
that guy over there

Reputation: 76

Your question is not very clear to me but check out the documentation on jQuery.trigger

This method will let you emulate a clickevent. I hope this is what you need.

Upvotes: -1

Related Questions