我无法访问嵌套的div。
这是我的情景。我需要访问内部div中的总数。有人可以在这里说清楚。
<div id="RequestSend">
<div class="title">
Requests Sent
</div>
<div class="total">
10
</div>
</div>
<div id="RequestReceived">
<div class="title">
Requests Received
</div>
<div class="total">
20
</div>
</div>
我尝试了以下内容,但没有成功。
Approach 1:
prof.rb
=======
div(:total_count, {:class => 'total'})
div(:request_send, {:id => 'RequestSend'})
prof_spec.rb
=============
page.request_send_element.total_count.should eq 10
Output:
NoMethodError: undefined method `total_count' for #<Selenium::WebDriver::Element:0x....>
Approach 2:
prof.rb
=======
divs(:total_count, {:class => 'total'})
prof_spec.rb
============
total_count[0] # for first
total_count[1] # for second
Please note I am a new user to page object.
答案 0 :(得分:3)
解决方案1 - 阻止
最直接的方法是在定义访问者时使用块。这允许您指定元素的路径 - 即允许您将搜索范围限定为特定元素。
最简单的方法是将一堆div_element
1种方法链接在一起:
div(:request_send_total){
div_element(:id => 'RequestSend').div_element(:class => 'total')
}
div(:request_received_total){
div_element(:id => 'RequestReceived').div_element(:class => 'total')
}
或者,如果您可能需要在RequestSend / RequestReceived div中查找不同的内容,我会专门为每个div创建一个访问器。然后,total的访问者将调用父元素:
div(:request_send, :id =>'RequestSend')
div(:request_send_total){
request_send_element.div_element(:class => 'total')
}
div(:request_received, :id =>'RequestReceived')
div(:request_received_total){
request_received_element.div_element(:class => 'total')
}
在这两种情况下,您的网页API都是:
page.request_send_total
#=> 10
page.request_received_total
#=> 20
解决方案2 - 小部件
更复杂的实现,但更好的页面API,将使用小部件。基本上这就像使用自己的特定方法创建元素类型一样。
您可以创建一个请求小部件,如:
class Request < PageObject::Elements::Div
def total
div_element(:class => 'total').text
end
end
PageObject.register_widget :request, Request, :div
您的页面对象将使用已注册的request
访问者:
request('request_send', :id => 'RequestSend')
request('request_received', :id => 'RequestReceived')
最后,您将获得总值:
page.request_send_element.total
#=> 10
page.request_received_element.total
#=> 20