how to change the value of a text field when it has an onchange event.

555 views
Skip to first unread message

jimjam

unread,
Jun 25, 2009, 12:47:48 PM6/25/09
to webdriver
re webdriver issue 156.

The browser user behaviour I am trying to reproduce is changing the
text in a text input. So normally, I can call WebElement.clear() and
then WebElement.sendKeys("...").

The problem comes when the text input has an onchange js event,
because both "clear" and the "sendKeys" trigger the onchange event.
This means, I can't avoid it being called twice.

When I use firefox manually, the onchange event is fired just once,
when I click out of the text field.

What makes the problem even worse, is that sometimes, the application
I'm testing submits the form in the onchange event. I know it's a
horrible design - but that's what I've got.


Has anyone any suggestions? I'm using the firefox driver and
webdriver 964, but also plan to use the HTMLUnit driver (not sure how
that behaves yet). I don't think that earlier versions of webdriver
had this problem, I think it started in version 685


Thanks
Ben




Dante Briones

unread,
Jun 25, 2009, 1:40:48 PM6/25/09
to webd...@googlegroups.com
I haven't tried this, but maybe instead of calling clear(), you could
prefix your call to sendKeys() with a select all (ctrl-a on windows),
then backspace?

--Dante
--
Dante Briones
http://dantebriones.com/
mobile: +1 (415) 283-5462




Ahmed Ashour

unread,
Jun 25, 2009, 1:56:26 PM6/25/09
to webd...@googlegroups.com
Hi Ben,
 
I didn't try, but as odd as it might sound, what about trying sending "\b\b\b\b\b\byour_new_text"?
 
Ahmed
----
Blog: http://asashour.blogspot.com

From: jimjam <jji...@googlemail.com>
To: webdriver <webd...@googlegroups.com>
Sent: Thursday, June 25, 2009 7:47:48 PM
Subject: [webdriver] how to change the value of a text field when it has an onchange event.


re webdriver issue 156.

The browser user behaviour I am trying to reproduce is changing the
text in a text input.  So normally, I can call WebElement.clear() and
then WebElement.sendKeys("....").

Simon Stewart

unread,
Jun 25, 2009, 2:01:58 PM6/25/09
to webd...@googlegroups.com
We don't handle "\b" properly, but using the Keys enum you can send
the same events.

Ben, would you mind opening an issue so that I can keep track of this?
Sounds like we should only be firing the "onchange" event once focus
shifts from the text field.

Regards,

Simon

jimjam

unread,
Jun 26, 2009, 9:21:38 AM6/26/09
to webd...@googlegroups.com
Thanks for your help. The Keys enum approach works well for me.

Keys[] keys = new Keys[element.getValue().length()];
for (int i = 0 ; i < keys.length ; i++)
keys[i] = Keys.BACK_SPACE;

element.sendKeys(Keys.chord(keys) + newValue);



I have opened a new issue - 214. I'm not sure what the best solution
is. The closest match to manual user control is to fire the event
when the input loses focus. The problem with that is it takes a few
lines of code to achieve (something like: el.clear();
el.sendKeys("mytext"); el.sendKeys(TAB);) I guess that's not too
bad. The most important thing for me is that it works the same in
all drivers.


Thanks
Ben
Reply all
Reply to author
Forward
0 new messages