如何在PageObject中使用css获取元素

Fab*_*e31 2 watir page-object-gem

在我添加测试的项目中,我有一些复杂的元素“获取”。
我的 textarea 的 html :

<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>
Run Code Online (Sandbox Code Playgroud)

目前,我使用这一行来设置值

@browser.element(:css => ".quote textarea").send_keys "test"
Run Code Online (Sandbox Code Playgroud)

在 PageObject 中,我应该声明元素并使用它:

# declaration at the top
element(:quote_text, :css => ".quote textarea") 
# use it where I need
self.quote_text = "text"
Run Code Online (Sandbox Code Playgroud)

但是当我使用时出现此错误:

undefined method `quote_text=' for #<PublishPage:0x33843c0> (NoMethodError)
Run Code Online (Sandbox Code Playgroud)

我哪里错了?

Jus*_* Ko 5

当你这样做时element(:quote_text, :css => ".quote textarea"),它只会生成方法(见文档):

quote_text #Returns the text of the element
quote_text_element #Returns the element
quote_text? #Returns if the element exists
Run Code Online (Sandbox Code Playgroud)

由于您知道它是一个文本区域并且想要文本区域方法,您应该将其声明为:

text_area(:quote_text, :css => ".quote textarea") 
Run Code Online (Sandbox Code Playgroud)

这将为您提供您quote_text=期望的方法(请参阅doc)。

更新- 由于 Watir-Webdriver 仅支持元素类中的 css 选择器(请参阅第 124 期),您还需要更改定位器。两个替代声明quote_text似乎有效:

#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') 
Run Code Online (Sandbox Code Playgroud)