2016-12-29 31 views
0

有沒有辦法將一組動態選項傳遞給瀏覽器中的textarea元素?Watir:通過動態選擇器

options = { 
    :type  => 'textarea', 
    :selector => ':id', 
    :field => 'id_of_textarea_field', 
    :value => 'Joe Salesperson' 
}  

browser.textarea(options[:selector] => options[:field]).set '' 

錯誤接收:

invalid attribute: ":id" 

類似的線程在這裏(selecting elements using variables in ruby /watir)上市,但無人接聽。

回答

1
options = { 
    :type  => 'textarea', 
    :selector => :id, 
    :field => 'id_of_textarea_field', 
    :value => 'Joe Salesperson' 
} 

您只需傳遞標識符,不需要引號。


2017-01-03,使用用例進行更新。這是我正在使用的方法和調用的聲明。實際的代碼比我在這裏粘貼的代碼更詳細,包含不同的驗證。在這個例子中,您會注意到一個簡單的開關塊,它根據傳遞的元素類型(特別是文本區域,文本字段和選擇元素)設置適當的信息。

def validateInput(options = {}) 
    success = true 

    begin 
     case options[:type] 
      when 'textarea', 'text_field' 
       Watir::Wait.until{$browser.textarea(options[:selector] => options[:field]).present?} 

       $browser.textarea(options[:selector] => options[:field]).set options[:value] 
      when 'select' 
       $browser.select_list(options[:selector] => options[:field]).select_value options[:value] 
      else 
       puts "in else" 
      end 
     end 
    rescue => e 
     $message.push("Failed to validate '#{options[:field]}' field. #{e}") 
     success = false 
    end 

    return success 
end 


validateInput({ 
    :type  => 'textarea', 
    :selector => :id, 
    :field => 'order_approval_comment_name', 
    :value => 'Joe Salesperson' 
}) 
+0

我想知道通過這種方式的用途是什麼? – Gopal

+0

@Gopal,我已經更新了我的答案。希望這爲你提供更好的清晰度。 –

+0

好的,謝謝。 – Gopal