如何处理缺乏足够独特属性的HTML控件? [英] How to handle testing an HTML control that lacks sufficiently unique attributes?

查看:112
本文介绍了如何处理缺乏足够独特属性的HTML控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个类 Locator ,它表示:我用来隐藏CUIT找到控件所需的细节。要在控件上操作,页面对象指定定位器而不是控件,而低级函数找到控件并执行操作。



现在,我的课程有:




  • 定位器名称。
  • >
  • 一个或多个可以找到控件HTML元素的attrName / attrValue对。
  • 指定所需匹配的运算符(Contains或EqualTo)。 / li>


问题:我需要操作的某些控件没有足够的唯一属性来允许他们被发现。 (是的,我知道开发人员应该改进他们的HTML,但我不能控制它)。我一直在使用定位器来查找附近的元素,然后在DOM中走来找到我想要的元素。我讨厌在页面对象中使用这个DOM行为代码,甚至将其作为函数的一部分。



可能的解决方案:我正在考虑增强class Locator ,以便它可以包含attrName / attrValue对,它是对具有DOM行走代码的函数的引用。这样做的一个优点是页面对象总是使用一个定位器对象。另一个原因是,当HTML得到改进时,定位器可以从DOM行走代码更改为attrName / attrValue对,这对页面对象是透明的。



我认为这可能过于复杂。



这个问题有更好的解决方法吗?

解决方案

不确定您的定位器是如何工作的,但是您能找到与该对象最接近的父对象,让我们说一个ID为parent的HTML Div,然后计算下面的标记实例?例如:

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

以下代码:

  public HtmlHyperlink text2Link 
{
get
{
HtmlDiv parentDiv =新的HtmlDiv(浏览器);
parentDiv.SearchProperties [id] =parent;
HtmlHyperlink target = new HtmlHyperlink(parentDiv);
target.SearchProperties [TagInstance] =2;
}
}

这将在父对象下找到第2个超链接。 (标记实例不是基于零)。

然后,您只需根据需要与对象进行交互:

  Mouse.Click(text2Link); 

举例。


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?

解决方案

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.

这篇关于如何处理缺乏足够独特属性的HTML控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆