Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set value of text field all at once with a webdriver?

I am using Cucumber, watir-webdriver, page-object, and jruby. I am writing a method in a page class that enters a value into a text area. I am using a generated method in the page-object gem that, under the hood, invokes the Watir-Webdriver set method, that in turn invokes send_keys on the element.

Anyway, the problem for me is I am trying to add a VERY LARGE STRING (in order to test max size stuff for a validateable form object). It's 4000 characters and takes a noticeable amount of time to enter.

It would be great if there were a way to just paste the whole string into the text area at once. Is there any way to do this with the technologies I have on hand? These are.. JRuby, watir-webdriver, page-object (which really just delegates to watir-webdriver). I suppose under the hood it's selenium-webdriver that's doing the browser driver interaction in any case.

So far I haven't found a way around ultimately using send_keys which basically sends one key stroke at a time and that's why a huge character string is a pain.

like image 491
Dmitry Sharkov Avatar asked Mar 17 '23 23:03

Dmitry Sharkov


1 Answers

You could directly set the value of the field using execute_script.

Given a page with a textarea:

<html>
  <body>
    <textarea></textarea>
  </body>
</html>

Inputting the textarea with set took 6-9 seconds (with Firefox/Chrome):

input = 'a' * 4000
browser.textarea.set(input)

However, by using execute_script to directly set the value, it only took 0.2 seconds:

input = 'a' * 4000
field = browser.textarea
browser.execute_script('arguments[0].value = arguments[1];', field, input) 
like image 185
Justin Ko Avatar answered Apr 09 '23 23:04

Justin Ko