如何在PageObject中使用CSS获取元素 [英] How to get element with css in PageObject
问题描述
在我要添加测试的项目中,我有些要获取"的元素很复杂.
我的textarea的html:
In the project on which I add tests, I have some elements complicated to "get".
The html of my textarea :
<div class="quote">
<div class="clearfix">
<div class="icon"></div>
<div class="text">
<span class="yui3-widget yui3-inputwidget yui3-textareawidget yui3-textareawidget-focused" style="">
<span class="yui3-textareawidget-content">
<textarea name="" placeholder=""></textarea>
</span>
</span>
</div>
<div class="author">
(...) other text_field
</div>
</div>
</div>
</div>
目前,我使用此行来设置值
For the moment, I use this line to set value
@browser.element(:css => ".quote textarea").send_keys "test"
在PageObject中,我应该声明元素并使用它:
In PageObject, I should declare the element and use it :
# declaration at the top
element(:quote_text, :css => ".quote textarea")
# use it where I need
self.quote_text = "text"
但是当我使用时出现此错误:
But I get this error when I use :
undefined method `quote_text=' for #<PublishPage:0x33843c0> (NoMethodError)
我在哪里错了?
推荐答案
当您执行element(:quote_text, :css => ".quote textarea")
时,它将仅生成方法(请参见
When you do element(:quote_text, :css => ".quote textarea")
, it will only generate the methods (see doc):
quote_text #Returns the text of the element
quote_text_element #Returns the element
quote_text? #Returns if the element exists
由于您知道这是一个文本区域,并且需要该文本区域方法,因此应将其声明为:
Since you know it is a text area and want the text area methods, you should declare it as:
text_area(:quote_text, :css => ".quote textarea")
This will give you the quote_text=
method you expect (see doc).
更新-由于Watir-Webdriver仅在元素类上支持CSS选择器(请参见问题124 ),您还需要更改定位器. quote_text
的两个替代声明似乎可行:
Update - Since Watir-Webdriver only support css-selectors at the element class (see Issue 124), you will also need to change the locator. Two alternative declarations for quote_text
that seem to work:
#Using a block to locate the element:
text_area(:quote_text){ div_element(:class => "quote").text_area_element }
#Using xpath:
text_area(:quote_text, :xpath => '//div[@class="quote"]//textarea')
这篇关于如何在PageObject中使用CSS获取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!