Visual Studio Coded UI: Select HTML Element Using CSS Selector
I have a problem with item selection within Microsoft CodedUI. I have a page with items that can be added / removed by checking a checkbox. Checkboxes do not have a unique ID and I am having a problem with selecting something other than the first item when looking for a specific combination of labels / classes. Is there some kind of trick for this that isn't immediately obvious.
+3
source to share
1 answer
There are several different options here:
1. You can select an object by selecting the item associated with it.
<div id="parent">
<label id="checkbox1234">MyCheckBox</label>
<input checked="false"></input>
</div>
... can be chosen as:
HtmlDiv parent = new HtmlDiv(browserWindow);
parent.SearchProperties["innerText"] = "MyCheckBox";
HtmlCheckBox target = new HtmlCheckbox(parent);
target.SearchProperties["TagName"] = "INPUT";
return target;
or
HtmlLabel sibling = new HtmlLabel(browserWindow);
sibling.SearchProperties["id"] = "checkbox1234";
UITestControlCollection col = sibling.GetParent().GetChildren();
foreach (UITestControl control in col)
{
if (control.ControlType.ToString().Equals("HtmlCheckBox"))
{
return (HtmlCheckBox)control;
}
}
- You can use these test utilities created by Gautam Goenka . They did wonders for me by identifying the content of an object and using it to make statements. That said, if you don't have a meaningful way of identifying objects based on content, that won't help either. When all else fails, add some useful identifying properties to your HTML.
+1
source to share