有没有办法将一组动态选项传递给浏览器中的textarea元素?
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),但没有答案。
答案 0 :(得分: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'
})