1

I'm using Coded UI Test to test a web application.

I have a class Locator that I use to stash the specifics needed for CUIT to find a control. To operate on a control, a page object specifies the locator, not the control, and lower-level functions find the control and perform the operation.

Right now, my class has:

  • Locator name.
  • One or more attrName/attrValue pairs that can locate the HTML element for the control.
  • An operator (Contains or EqualTo) that specifies the matching needed.

The problem: Some of the controls I need to operate on don't have enough unique attributes to allow them to be found. (Yes, I know the developers should improve their HTML, but I don't control that.) I have been using a locator to find a nearby element, then "walking" in the DOM to get to the element I want. I hate having this DOM-walking code in my page object, even factored into a function.

A possible solution: I'm considering enhancing class Locator so that it can have either the attrName/attrValue pairs or a reference to a function that has the DOM-walking code. One advantage of this is that the page objects would always use a locator object. Another is that when the HTML is improved, the locator could change from DOM-walking code to attrName/attrValue pairs, which would be transparent to the page object.

Still, I think this may be over-complicated.

Is there a better solution to this problem?

Burdette Lamar
  • 229
  • 2
  • 13

1 Answers1

1

Not sure specifically how your locator works, but could you find the closest parent to that object, let's say an HTML Div with an id of "parent", and then count the tag instances underneath? For example:

HtmlDiv id="parent">
    HtmlHyperlink>text1</ 
    HtmlHyperlink>text2</

Would require the following code:

public HtmlHyperlink text2Link
{
    get
    {
        HtmlDiv parentDiv = new HtmlDiv(browser);
        parentDiv.SearchProperties["id"] = "parent";
        HtmlHyperlink target = new HtmlHyperlink(parentDiv);
        target.SearchProperties["TagInstance"] = "2";
    }
 }

This would find the 2nd hyperlink under the parent object. (Tag instances are not zero based).

Then, you'd just interact with your object as needed:

Mouse.Click(text2Link);

for example.

Ryan Cox
  • 938
  • 1
  • 7
  • 22
  • Does it automatically find the element? Don't we need to run .FindMatchingControls()? – varagrawal Apr 03 '15 at 14:29
  • If you're just passing a reference to the object, then no. I'll edit for clarity. – Ryan Cox Apr 06 '15 at 12:05
  • You would use FindMatchingControls() if you had to create a collection of elements based on loose or generic search properties. Then, you could iterate through that collection. It's not necessary to call that function if you only want to interact with one well-defined control, as we have above. – Ryan Cox Apr 06 '15 at 12:10