Setting text in Chromedriver is rather slow

A colleague contacted me today to see if there’s a way to increase the speed of a text_field.set operation in watir-webdriver when using Chrome on Mac OSX. I hadn’t noticed it before but Chrome seems to enter character by character into text fields which is noticeably slower than Firefox which seems to ‘set’ the field at once. Jari Bakken suggested I try Chrome native events which I didn’t even realize existed on Mac OSX.

So, I took Jari’s suggestion and I created a benchmark of a .set operation conducted ten times in a row on Firefox, and Chrome with and without native events. The results looked like:

                        user       system     total        real
set firefox             0.160000   0.020000   0.180000  ( 8.205473)
set chrome non-native   0.100000   0.010000   0.110000  (44.980334)
set chrome native       0.100000   0.010000   0.110000  (31.501647)

You can see that Chrome is drastically slower than Firefox in setting text fields, but native events does seem to provide a roughly 25% speed improvement. I think I will stick to running my watir-webdriver tests against Firefox unless specifically needed.

Sample Code for Benchmarks

Setting text using Chrome with native events enabled

require 'selenium-webdriver'
require 'watir-webdriver'
require 'bench'

d = Selenium::WebDriver.for :chrome, native_events: true
b = Watir::Browser.new d
b.goto 'bing.com'

benchmark 'set chrome native' do
  b.text_field(name: 'q').set 'ghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghgh'
end

run 10
b.close

Setting text using Chrome with native events disabled

require 'selenium-webdriver'
require 'watir-webdriver'
require 'bench'

b = Watir::Browser.new :chrome
b.goto 'bing.com'

benchmark 'set chrome non-native' do
  b.text_field(name: 'q').set 'ghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghgh'
end

run 10

b.close

Setting text using Firefox (native events aren’t available on OSX)

require 'selenium-webdriver'
require 'watir-webdriver'
require 'bench'

b = Watir::Browser.new :firefox
b.goto 'bing.com'

benchmark 'set firefox' do
  b.text_field(name: 'q').set 'ghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghgh'
end

run 10

b.close

Author: Alister Scott

Alister is an Excellence Wrangler for Automattic.

2 thoughts on “Setting text in Chromedriver is rather slow”

  1. The thing I’ve found is that while often we just want the tests to run fast, in some cases the character by character stuff is really desired, such as with controls that do dynamic searching as you start typing in characters. (think of the process of adding tags to a StackOverflow posting as a good example of this) When testing that something is there in the search results, stuff that just ‘sets’ the field sometimes seems to never trigger the searching, while character by character does. So the difference you describe above may be something that might cause a test to pass on chrome but fail on firefox if the underlaying driver called by .set in watir is handling the setting of the field value differently.

    In the original watir (and I need to see if this is still the case) a while back we used to have a sort of secret trick that could be used to set the entire value at once instead of emulating character by character typing (almost akin to manually using a paste operation) which was to use .value=’mytext’ instead of .set ‘mytext’. does that same use of .value exist in watir-webdriver? (I’d answer it myself, but am working from a brand new system and getting rvm/ruby/watir/etc installed is on the todo list)

    It does make me wonder if maybe we should propose a ‘paste’ method that would emulate a user doing a paste action (load the supplied value into the clip-board and issue a control/command – V keystroke) which might potentially be a lot faster than character by character input.

    Like

  2. I know the “trick”. In the original Watir, the #set method on a text field entered the string character by character. But you could also use (for example):

    browser.text_field(:id => ‘some_id’).text = ‘my_string’

    to set it all at once. That was the major speedup trick.

    The original Watir was great in this respect. You could also do

    element.html=

    element.text=

    and so on. You could change almost anything in the DOM, including JavaScript, on the fly. I miss that ability. It can be VERY handy sometimes and I have frequently bemoaned the loss of that ability.

    I *really, really, really* don’t like this “we won’t keep that functionality because that’s not a genuine user experience” BS. The fact is that despite its foibles, the original Watir had many advantages over the modern versions, and I made use of them extensively. To those people I say: don’t tell me how I can use a tool. My use case is not the same as yours.

    Like

Comments are closed.