且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用页面对象深入访问嵌套元素3层

更新时间:2023-12-02 10:47:34

假定嵌套始终相同,而不是通过每个祖先使用:scar_first_name_error映射,您可以相对于其父元素定义每个元素(或祖先).

Assuming that the nesting is always the same, rather than having the :scar_first_name_error map through each ancestor, you could define each element with respect to its parent (or ancestor).

让我们假设HTML是:

Let us assume the HTML is:

<html>
  <body>
    <div class="validate-method">
      <div class="service-info">
        <div class="input-group">
          <div class="input-container input-left-half round">
            text
          </div>
        </div>
      </div>
    </div>
  </body>
</html>

您可以将页面定义为:

class MyPage
  include PageObject

  div(:serv_info) { validate_method_element.div_element(:class => "service-info") }
  div(:validate_method, :class => "validate-method")
  div(:scar_input_group) { serv_info_element.div_element(:class => "input-group") }
  div(:scar_first_name_error) { scar_input_group_element.div_element(:class => "input-container input-left-half round") }
end

请注意,:serv_info是相对于其父级:validate_method定义的,:scar_input_group是相对于其父级:serv_info定义的,等等.

Notice that the :serv_info is defined with respect to its parent :validate_method, :scar_input_group is defined with respect ot its parent :serv_info, etc.

使用此页面对象,我们可以看到可以获取下部元素的文本:

With this page object, we can see we can get the lower element's text:

page = MyPage.new(browser)
p page.scar_first_name_error
#=> "text"