使用页面对象深入访问嵌套元素3层 [英] Accessing a nested element 3 levels deep using page objects
问题描述
使用Page Object模型和gem,我想访问嵌套3层深的元素.我已经成功访问了深度为2个元素的嵌套元素,但是对于3个元素,相同的方法不起作用.
Using the Page Object model and gem I want to access an element that is nested 3 layers deep. I've successfully accessed nested elements that are 2 elements deep but for 3 the same method isn't working.
在我的Page对象中定义的3个元素:
3 elements defined in my Page Object:
div(:serv_info, :class => "service-info")
div(:validate_method, :class => "validate-method")
div(:scar_input_group, :class => "input-group")
所以我试图将这3个元素链接起来,以如下方式访问div
class
input-container input-left-half round
:
So I tried to chain those 3 elements to access the div
class
input-container input-left-half round
like this:
div(:scar_first_name_error){validate_method_element.serv_info_element.scar_input_group_element.div_element(:class => "input-container input-left-half round")}
但是我得到一个错误,即serv_info_element
是一个未定义的方法,这是有道理的,但是是否可以链接我上面所述的3个预定义元素来访问input-container input-left-half round
?
But I got the error that serv_info_element
is an undefined method, which makes sense, but is it possible to chain the 3 predefined elements I stated above to access the input-container input-left-half round
?
我读了这篇文章: https://github.com/cheezy/page- object/wiki/Nested-Elements ,但如果我能帮助的话,不想重复任何代码.
I read this: https://github.com/cheezy/page-object/wiki/Nested-Elements but didn't want to have to repeat any code if I can help it.
推荐答案
假定嵌套始终相同,而不是通过每个祖先使用: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"
这篇关于使用页面对象深入访问嵌套元素3层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!