It takes around 4-5 seconds to set large text to text_field using watir-webdriver. I have also tried value method, but still it's very slow.
I have found a workaround for this on Windows using Clipboard gem and send_keys [:control, "v"], however this does not really work with headless linux. Any suggestion on this?
Inputting large values can be slow because the characters are inputted one at a time. This is to trigger each of the key events.
Assuming your application does not care about the events triggered by inputting the field, you could directly set the value via JavaScript.
Watir 6.8+
Watir now provides a #set!
method to do this:
long_text = "abcde fghijk lmnop qrstuv"
browser.text_field.set!(long_text)
Pre-Watir 6.8
Prior to v6.8 (when this was originally answered), this needed to be done manually via #execute_script
:
long_text = "abcde fghijk lmnop qrstuv"
the_field = browser.text_field
p the_field.value
#=> ""
browser.execute_script("arguments[0].value = '#{long_text}';", the_field)
p the_field.value
#=> "abcde fghijk lmnop qrstuv"
Performance Comparison
Even with this small text, you can see that execute_script
is much faster. A benchmark:
n = 100
Benchmark.bm do |x|
x.report("execute_script:") { n.times { browser.execute_script("arguments[0].value = '#{long_text}';", the_field) } }
x.report("set:") { n.times { the_field.set(long_text) } }
end
The results:
user system total real
execute_script: 0.874000 0.609000 1.483000 ( 6.690669)
set: 2.199000 1.295000 3.494000 ( 22.384238)