Page-object gem: Identifying object with same properties based on their visibility

只谈情不闲聊 提交于 2019-12-02 06:41:36

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

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"

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) }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!