Skip to content
This repository has been archived by the owner on Nov 29, 2018. It is now read-only.

onchange js event fired in WebElement.clear() and WebElement.sendKeys() #214

Closed
lukeis opened this issue Mar 2, 2016 · 18 comments
Closed

Comments

@lukeis
Copy link
Member

lukeis commented Mar 2, 2016

Originally reported on Google Code with ID 214

Browser: Firefox, perhaps others
Version: Firefox 3.0.11
Operating System: Vista

What steps will reproduce the problem?
1. HTML Page has a text input with an onchange event
2. Changing the input's value either with clear() or sendKeys(...) fires
the onchange javascript event.

What is the expected output? What do you see instead?

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.



What version of the product are you using? On what operating system?

1039 also happens in earlier versions



Please provide any additional information below.

This has been discussed in mailing list item "how to change the value of a
text field when it has an onchange event." and issue 156

Using Keys enum in sendkeys is a good workaround


Reported by jjimjam on 2009-06-26 13:12:35

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Correction:  sendKeys does not trigger an onchange.   You need to click on another
element to trigger it.   This makes it behave just like a browser.  It would be handy
to have the ability to change an Input's text value and for it to automatically and
immediately fire the onchange.  

Reported by jjimjam on 2009-07-09 12:52:02

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016


HTMLUnit behaves in the same way.    

But Keys (in sendkeys) don't work in HTMLUnit.  My full workaround is now:

if (driver instanceof HtmlUnitDriver)
{
  //HTMLUnit: Keys don't work and clear fires the onChange event!
  //hack is to disable js, call clear and then enable it again
  ((HtmlUnitDriver)driver).setJavascriptEnabled(false);
  element.clear();
  ((HtmlUnitDriver)driver).setJavascriptEnabled(true);
  element.sendKeys(newValue);
}
else
{
  //Firefox.    Clear fires the onChange event, 
  //so send a bunch of backspaces to clear out the input value
  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);
}


Reported by jjimjam on 2009-07-09 12:53:26

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Instead of repeatedly typing backspace, can you just select the input's value and then

type some more?

element.sendKeys(Keys.chord(Keys.CONTROL, 'a'), 'foobarbaz');

Reported by jmleyba on 2009-12-17 17:53:31

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

This report is quite old. Is this still a problem in 2.0b1?

Reported by jari.bakken on 2011-01-27 01:43:08

  • Labels added: Component-WebDriver

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Yes, still behaving the same.  I still have to use the workaround described in comment
2.   The bug title and report aren't very accurate though.

I'm trying to get consistent behaviour between HTMLUnit and Firefox drivers

WebElement.sendKeys(text) doesn't fire onchange().   I'm happy with that.
WebElement.clear() fires onchange() if the input had a value.   Is that right?  It
means I can't use it.

org.openqa.selenium.Keys don't work in HTMLUnit  (e.g. BACK_SPACE and TAB).   That
means I have use the above workaround to change the value of a input.

Reported by jjimjam on 2011-01-27 11:48:54

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Hi jjim...@googlemail.com,
                           I ma new to Selenium. I have used Selenium IDE, and facing
many problem, error. Searched in web but could not get clear notes or idea about the
error or Selenium can u give any notes about Selenium.

Thanks & Regards 
Sakthy
(sakthy1123@gmail.com)

Reported by sakthivel2325 on 2011-08-10 06:04:23

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

+1 on this. the Clear() method should not fire any change event until focus is moved
to another element.

Reported by markkemper1 on 2011-09-05 19:25:16

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

.Clear should not trigger onchange event. I belive that if just sendkeys would trigger
events it would do the trick. 

I have login page and when I want to login as different user and call .Clear it triggers
validation that username is not defined and SendKeys do not trigger the validation
again. I have made an workaround that unless you want to empty textbox keeps always
at least 1 character within textbox but it is not clear solution.

Reported by milan.vondra on 2011-11-22 14:39:24

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

I have tries this with Selenium 2.12 (.Net) and Firefox 7.01 and IE 9 same issues with
both browser 

Reported by milan.vondra on 2011-11-22 14:40:57

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Using version 2.17.0 and still HtmlUnit driver trigger onChange for element.clear()
but not for element.sendKeys(Keys.TAB)...

Reported by glaurung.aubrane on 2012-01-27 10:51:23

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

I'm also hitting this issue with 2.18.0 IE8, FF9 & Chrome16 drivers - a real pain.

Reported by richard.dickinson@ca.com on 2012-02-01 09:50:28

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

With Firefox 5.0.1 and Selenium 2.16.1 is not triggering onchange event for me with
I call element.clear() on an input element. Now you know!

Reported by jens.rantil on 2012-02-09 15:02:12

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

I'm still experiencing that WebElement.Clear() fires onchange()
WebDriver 2.21.0.0
Chrome 19.0.1084.46 m
Firefox 12.0

Will this ever be solved?

As a workaround in C# I've made extension method

public static class WebElementExtensions
{
        public static void ClearWithBackspace(this IWebElement element)
        {
            int length = element.GetAttribute("value").Length;
            element.Click();
            element.SendKeys(Keys.End);
            StringBuilder sb = new StringBuilder();
            int i = 0;
            while(i < length)
            {
                sb.Append(Keys.Backspace);
                ++i;
            };
            element.SendKeys(sb.ToString());
        }
}

Reported by Andrzej.Pasterczyk on 2012-05-25 09:20:56

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

web driver 2.23.1 with firefox 12.0.  neither clear(), nor sendKeys() trigger an onchange
event.  Also click() on another input/element after sending keys to an input text box
does NOT trigger an onchange event.

In summary with web driver 2.23.1 and firefox 12 there does not appear to be any way
to trigger an onchange event from a text input element.

Reported by qode.qrash on 2012-06-18 20:04:40

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Closing this as working as intended.

If you use sendKeys() on an element, that element will still have focus when the command
is finished, so the change event will not fire.  You must change focus to another element
for the change event to fire.

Please do not respond to this bug if you do not get the change event after focusing
on another element - that is a separate issue and you should file a separate bug report.

Reported by jmleyba on 2012-06-19 23:34:34

  • Status changed: WorkingAsIntended

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

This problem seems to have (partially) returned.

With WebDriver 2.43.1, calling WebElement.clear() trigger an onchange event (tested
on Firefox 32 and Chrome 37 on Windows 7).
WebElement.sendKeys() does *not* trigger onchange.

Abbreviated test case:

*HTML*

<form>
    Input: <input name="bla" type="text" size="10"
    onchange="this.value=this.value+'X'; console.log('onchange');" />
</form>

*Test*

public void testOnchange() throws Exception {
    driver.get(baseUrl.toString());
    Thread.sleep(5000);
    WebElement input = driver.findElement(By.tagName("input"));
    input.sendKeys("abc");
    Thread.sleep(2000);
    input.clear();
    Thread.sleep(2000);
    assertEquals("", input.getAttribute("value"));
}

Observed result:
First, the text "abc" appears in the field, then the field changes to just "X". Thus
the test fails.

Expected result:
The field should be empty at the end (because nothing triggers an onchange event).

Reported by sebleske on 2014-09-24 12:53:05

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

I'm experiencing this issue as well (selenium-java v 2.43.1).

Application has input field:
<input id="search" type="search" placeholder="Enter URL or IP Address" data-bind="event:
{change: $root.searchQuery}" class="input2" maxlength="254s">

The field has a value of "test.com".

If I use field.clear(), an onchange event is kicked off and the page refreshes without
any change of focus.

Here is the method on my pageObject:
    @FindBy(id = "search")
    WebElement searchField;

    @FindBy(id = "submit2")
    WebElement urlSubmit;

    public <T extends BasePage> T urlLookup(String url) throws UnexpectedPageException
{
        searchField.clear();  //Causes page refresh and invalidates elements.
        searchField.sendKeys(url);
        return clickUrlSubmit();
    }

Reported by PredatorVI on 2014-10-30 01:12:08

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Reported by luke.semerau on 2015-09-17 17:47:53

  • Labels added: Restrict-AddIssueComment-Commit

@lukeis lukeis closed this as completed Mar 2, 2016
@SeleniumHQ SeleniumHQ locked and limited conversation to collaborators Mar 3, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant