页面对象gem:根据可见性识别具有相同属性的对象

时间:2014-11-11 20:39:35

标签: page-object-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;">

有一个添加按钮,当我点击它时会创建一个新标签。在新标签中,除了正在创建新id之外,我将在文本框中显示相同的属性。

<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。选项卡与其中的字段之间没有关系。标签显示如下:

<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>

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

谢谢!

1 个答案:

答案 0 :(得分:3)

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

但是,如果这不是一个选项,我认为剩下的唯一选择是迭代所有可能的文本字段并采取可见的文本字段。

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

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

您可以使用文本字段访问器来浏览文本字段元素并选择第一个可见元素。

class MyPage
  include PageObject

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

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

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

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

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