简体   繁体   English

watir-webdriver设置为text_field的值非常慢

[英]watir-webdriver setting value to text_field very slow

It takes around 4-5 seconds to set large text to text_field using watir-webdriver. 使用watir-webdriver将大文本设置为text_field大约需要4-5秒。 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. 我已经在Windows上使用Clipboard gem和send_keys [:control,“ v”]找到了解决方法,但是这对于无头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. 假设您的应用程序不关心由输入字段触发的事件,则可以通过JavaScript直接设置该值。

Watir 6.8+ 瓦迪尔6.8+

Watir now provides a #set! Watir现在提供了#set! method to do this: 方法:

long_text = "abcde fghijk lmnop qrstuv"
browser.text_field.set!(long_text)

Pre-Watir 6.8 瓦蒂尔6.8之前

Prior to v6.8 (when this was originally answered), this needed to be done manually via #execute_script : 在v6.8之前(最初被回答时),需要通过#execute_script手动#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. 即使有这么小的文本,您也可以看到execute_script更快。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM