页面对象gem:根据对象的可见性识别具有相同属性的对象 [英] Page-object gem: Identifying object with same properties based on their visibility

查看:52
本文介绍了页面对象gem:根据对象的可见性识别具有相同属性的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有以下html的文本框. id是动态的,我无法使用它.我在文本框中填写了一些文本. <input type="text" id="117841" class="NEdit" title="MyText" maxlength="20" style="position: absolute; overflow: hidden; font-style: normal; font-weight: normal; font-family: arial; font-size: 12px; text-transform: uppercase; width: 184px; left: 28px; top: 0px; height: 14px;">

I have a text box with the following html. The id is dynamic and I cannot use it.I fill some text in the text box. <input type="text" id="117841" class="NEdit" title="MyText" maxlength="20" style="position: absolute; overflow: hidden; font-style: normal; font-weight: normal; font-family: arial; font-size: 12px; text-transform: uppercase; width: 184px; left: 28px; top: 0px; height: 14px;">

有一个添加"按钮,当我单击该按钮时,将创建一个新选项卡".在新标签中,除了要创建新的id之外,我将在上面的文本框中具有相同的属性.

There is a Add button and when I click that a New Tab is created. In the new tab, I will have a text box with the same properties above except that a new id is being created.

<input type="text" id="118355" class="NEdit" title="MyText" maxlength="20" style="position: absolute; overflow: hidden; font-style: normal; font-weight: normal; font-family: arial; font-size: 12px; text-transform: uppercase; width: 184px; left: 28px; top: 0px; height: 14px;">

这两个文本框都在同一页上,但是在不同的选项卡中,其中一个是visible,另一个则不可见.选项卡显示为div.选项卡和其中的字段之间没有任何关系.标签显示如下:

Both the text boxes are on the same page but in different tabs with one being visible and the other not visible. The Tabs show as divs. There is no relationship between the tabs and the fields within them. The Tabs show as below:

<div id="117285" class="NTabTabs" style="position: absolute; overflow: visible; left: 0px; top: 0px; width: 1211px; height: 20px;"><div class="NTabTab" id="117285t0" style="cursor: pointer; position: absolute; overflow: hidden; white-space: pre; text-align: center; left: 2px; top: 2px; width: 101px; height: 16px; padding-top: 1px; border-top-width: 1px; border-top-style: solid; border-left-width: 1px; border-left-style: solid; border-right-width: 1px; border-right-style: solid;">Tab#1</div><div class="NTabActiveTab" id="117285t1" style="cursor: pointer; position: absolute; overflow: visible; white-space: pre; text-align: center; left: 104px; top: 0px; width: 65px; height: 19px; padding-top: 1px; border-top-width: 1px; border-top-style: solid; border-left-width: 1px; border-left-style: solid; border-right-width: 1px; border-right-style: solid;">Tab#2</div></div>

如何根据其可见性识别这些文本框?

How do I identify these text boxes based on their visibility?

谢谢!

推荐答案

我认为最好的方法是在两个元素之间找到一些区别属性.例如,HTML中至少必须有一些东西可以使一个可见而不是另一个.

I believe that the best approach is to find some distinguishing property between the two elements. For example, there must at least be something in the HTML that makes one visible and not the other.

但是,如果这不是一个选择,我认为剩下的唯一选择是遍历所有可能的文本字段并采用可见的文本字段.

However, if that is not an option, I think the only option left is to iterate through all of the possible text fields and take the one that is visible.

例如,在下一页中,有两个文本字段,第一个不可见:

For example, in the following page there are two text fields with the first not being visible:

<html>
  <body>
    <div style="display:none;">
      <input type="text" id="1">
    </div>
    <div>
      <input type="text" id="2">
    </div>    
  </body>
</html>

您可以使用文本字段访问器,该访问器遍历文本字段元素并选择第一个可见的元素.

You could use a text field accessor that goes through the text field elements and picks the first visible one.

class MyPage
  include PageObject

  text_field(:field){ text_field_elements.find(&:visible?) }
end

使用页面对象时,您会看到field_element将是第二个文本字段,因为它是可见的字段:

When the page object is used, you will see that the field_element will be the second text field since it is the one that is visible:

page = MyPage.new(browser)
p page.field_element.attribute('id')
#=> "2"

请注意,为使示例简单,访问器的块正在所有文本字段中进行迭代.但是,您可以将其他参数传递给text_field_elements,以更具体地确定要检查的文本字段.例如,以下访问者将找到具有特定标题的第一个可见文本字段:

Note that to keep the example simple, the accessor's block is iterating through all text fields. However, you can pass additional parameters to text_field_elements to be more specific about which text fields to check. For example, the following accessor would find the first visible text field with a specific title:

text_field(:text2){ text_field_elements(:title => 'MyText').find(&:visible) }

这篇关于页面对象gem:根据对象的可见性识别具有相同属性的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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