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

javascript onChange not fired when firefox isn't active window #157

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

javascript onChange not fired when firefox isn't active window #157

lukeis opened this issue Mar 2, 2016 · 43 comments

Comments

@lukeis
Copy link
Member

lukeis commented Mar 2, 2016

Originally reported on Google Code with ID 157


WebDriver release 588, 609, 685.

I have a html text input with an onChange event on it. If the firefox
browser is inactive (for example when I'm debugging in my IDE), the
onChange event is never fired.  I am trying to trigger the event by calling
click() on another WebElement.   It works fine if firefox is the active window.



Reported by jjimjam on 2009-02-18 19:13:49

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

This could well be a problem with firefox itself. I know that it queues "focus"
events up until the firefox instance actually has focus. Need to look into this a
little bit more before resolving the issue, though.

Reported by simon.m.stewart on 2009-03-03 10:42:54

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

We have multiple webdriver based test suites running concurrently on the continuous

integration server and this bug is making our intergration builds unstable. Running

the suites sequentially is not an option as the feedback cycle would be unacceptally

long (>1h). I expect other projects developing ajax-heavy application to face the 
same problem when they reach the critical mass of webdriver based automated tests.

Webdriver is a great tool and we would hate to abandon it just because of this. 

It would be valuable if we could figure out whether this behavior is caused by 
Firefox or by webdriver to get the fixing process started on the right end of the 
problem. We have tried debugging this issue but unfortunately it seems to require 
more know-how that we currently have or can aquire for now. However we are currently

finding a way to cope with this problem without actually solving it.

We are using webdriver 0.6.964 and firefox 3.0.6 on windows XP and firefox 3.0.8 on

Ubuntu.

Reported by jurzinov on 2009-05-22 12:27:36

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

We have similar problem and a fix would be great. We've managed to ease the problem

by forcing focus with something like webDriver.switchTo().window(""); It is an 
annoying help, but might be useful.

Reported by mantti on 2009-05-27 21:16:28

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

I'm seeing this problem in FF3.5/Ubuntu even when the Firefox window has focus. For

now I'm suffixing a tab to force the onchange to be fired:

    sendKeys(Keys.chord(Keys.CONTROL, "a") + Keys.DELETE + value + Keys.TAB)

To get around it.

Reported by stephen.haberman on 2009-11-13 03:23:07

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

You could try using native events with Firefox, which should solve the problem. This

involves creating a new FirefoxProfile instance, enabling native events on it 
(setEnableNativeEvents(true)) and instantiating a FirefoxDriver with this profile.

Note that at the moment it *may* break with multiple windows and constant window 
switching.

Reported by eran.mes on 2009-11-15 19:15:33

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

I need both the combination of native events and Keys.TAB suffix to get onchange to

fire. Just native events it does not and just Keys.TAB it does not.

Reported by stephen.haberman on 2009-11-17 15:36:05

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

This behaviour is expected. The "sendKeys" method leaves the focus in the element. You

need to deliberately move the focus from the element (in this case with the tab key)

before the "change" event will fire.

Reported by simon.m.stewart on 2009-11-19 12:19:57

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Stephen, are you encountering the same behaviour that the original creator of the 
issue encountered, or does Simon's comment an appropriate explanation for you?

Reported by eran.mes on 2009-11-19 19:52:17

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

@eran, you are right, I did not expect the expected behavior on sendKeys (I had known

about needing clear() before sendKeys(), but not another focus()). The browser window

active/inactive is not an issue for me.

Reported by stephen.haberman on 2009-11-19 19:59:27

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

I have a test with two text input elements and an onchange handler bound to the first
one. The test sends keys to 
the first field, then sends keys to the second field. When running in the foreground,
the change handler is 
triggered. When running in the background it is not -- even though the focus has been
moved off the first 
element.

I.e., the workaround in comment 7 does not work for me. I'm using Firefox on Mac OS
Snow Leopard.

Reported by john.firebaugh on 2010-05-21 21:36:33

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

FYI, I tried adding "dom.disable_window_flip" => false to the profile options (corresponding
to the "Allow scripts 
to: Raise or lower windows" preference). It didn't help.

Reported by john.firebaugh on 2010-05-22 01:06:55

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

I am experiencing this issue with FF 3.5.9 on OS X 10.5.8 (build 9L31a) on a MacBook
Pro (Intel) using selenium 2.0a4 and 2.0a5.

When FF has focus, I do not see any issue, but if the application window does not have
focus, the events do not seem to be firing.

Reported by jake.meier on 2010-07-28 00:22:16

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

WebDriver generates the same events whether the browser window has focus or not. I have
encountered similar problems with keyboard event processing in Linux when the window
did not have focus - for Linux, this was solved with the x_ignore_nofocus library.
It won't work on Mac - since Firefox is not displayed over X.
A better solution would be to get the browser to cooperate - and have a preference
that will make it process events regardless of the state of the focus. I'm working
on such a solution but it won't be available for a month, at least.

Reported by eran.mes on 2010-07-28 11:09:47

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

It seems as though we've all agreed that there is a reproducible issue here.  Can we
switch the status of this ticket to indicate that it's in progress, so that future
passers by can see at a glance that it's being worked on?

Reported by jake.meier on 2010-07-28 15:08:37

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Same as comment 12.  OSX 10.6.5 FireFox 3.6.12 selenium 2.0a5-a7.  I've narrowed it
down to elements that fire events themselves.  I have a GWT Textbox with a ValueChangeHandler
that raises events on the EventBus.  Seems that no combination of TAB, click() e.t.c
on any element will cause the browser's onBlur event to fire.  Regular Textbox widgets
that don't raise events themselves work fine.  When focus is given to the window manually,
the test works fine.  Works fine on Windows XP, Vista with Firefox 3.6.2+.

Reported by schmurgon on 2010-11-18 12:26:21

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Marking as started as per comment #14

Reported by antlong on 2011-01-24 18:16:43

  • Status changed: Started

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Still a bug on Mac OS X Snow Leopard. Please fix this!!!!

Reported by moritzp82 on 2011-03-22 11:12:45

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

There seem to be a bunch of interrelated bugs reported here.  For the specific case
of Firefox 3.6 not receiving focus events unless the window is active, see this bug:
http://code.google.com/p/selenium/issues/detail?id=543.

Reported by jpatokal on 2011-06-22 06:13:09

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Still seeing the same problem as comment #16, even with FF 6.0.2 on OS X 10.6.8 (Snow
Leopard). Has there been any resolution on this?

When running in webdriver, the FF window is in the background - when the FF window
in not in focus it seems the onChange() event does not fire and the test fails. Running
with the FF window in the foreground causes the test to pass successfully.

Reported by shreyas on 2011-09-09 03:17:20

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Sounds like the well-known problem with focus that Firefox has. That's an issue with
Firefox itself.

Reported by simon.m.stewart on 2011-09-09 08:30:15

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Reported by barancev on 2011-10-12 18:08:13

  • Labels added: Component-WebDriver, Browser-Firefox

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Simon - Sorry but I can't find any information about this Firefox issue.  Does the Firefox
team agree that it is a Firefox issue?  Do you have a reference to more information
about this problem.

Reported by jjimjam on 2011-10-24 15:43:17

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

I believe the relevant bug is https://bugzilla.mozilla.org/show_bug.cgi?id=566671

Reported by dave.hunt on 2011-11-03 14:21:10

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

we also have this issue on testing with selenium2 grid.
i have found a little workaround for firefox. before typing keys into an input field
send a focus event on it.
example:
selenium().fireEvent("id=your_id_locator", "focus");
selenium().typeKeys("id=your_id_locator", "h");
i have only tested with firefox 8.

Reported by marco.machmer on 2011-12-02 15:31:27

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Is there any sort of workaround when not using the selenium emulation?  I am going all
webdriver because any use of selenium emulation eats all of the alert boxes so you
can't use driver.switchTo().alert() because its not there.  But webdriver doesn't have
a fireEvent() method. 

I would love to be able to run my tests in parallel again.  Until I can figure something
out not only am I running them serially but when I test on my desktop I have to wait
and stare at the screen while the tests run instead of .. well... anything else.  

Reported by jjrussell on 2011-12-18 03:40:23

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

It would be really great if there was some way to fix this. In the mean time, I've resorted
to triggering the events manually with execute_script when necessary. Like so:

browser.execute_script('$("#description").change()')

Reported by fletcher@matchfwd.com on 2012-05-01 20:42:54

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

This is how I perform fireEvent for mouse events in WebDriver:

private String IE_MOUSE_EVENT_SCRIPT_PATTERN="arguments[0].fireEvent('on%s');";

    private String FF_CHROME_MOUSE_EVENT_SCRIPT_PATTERN="var evt = document.createEvent('MouseEvents');
evt.initMouseEvent('%s',true, true, window, 0, 0, 0, 0, 0, false, false, false, false,
0,null); arguments[0].dispatchEvent(evt);";

    /**
     * Executes custom JavaScript
     * 
     * @param javaScript
     * @param args
     * @return
     */
    public Object executeScript(String javaScript, Object... args) {
        return ((JavascriptExecutor) this.getWebDriver())
                .executeScript(javaScript,args);
    }

    /**
     * Executes some Action on provided element
     * 
     * @param javaScriptAction
     * @param element
     * @return
     */
    public Object fireJavaScriptEventForElement(JSActions javaScriptAction,
            Element element) {
        Logger.debug("Firing event '"+javaScriptAction.getFunctionName()+"' for element'"+element.getIdentifyingText()+"'");
        String myScript;
        if (getBrowserType().equals("Microsoft Internet Explorer")){
            myScript=String.format(IE_MOUSE_EVENT_SCRIPT_PATTERN,javaScriptAction.getFunctionName());
        }
        else{
            myScript=String.format(FF_CHROME_MOUSE_EVENT_SCRIPT_PATTERN, javaScriptAction.getFunctionName());
        }
        return executeScript(myScript,element.getWebElement());
        //this.navigate().to(myScript,element.getWebElement());

    }

Reported by Andrei.surely on 2012-05-03 11:14:43

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Where
public String getBrowserType(){
        return executeScript("return navigator.appName.toString()").toString();
    }

Reported by Andrei.surely on 2012-05-03 11:15:51

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Has this or will this ever be fixed? It's really frustrating.

Reported by michael.darmousseh on 2012-09-06 21:29:34

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

https://bugzilla.mozilla.org/show_bug.cgi?id=704583

Reported by ross@cgl.ucsf.edu on 2012-09-17 17:31:32

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Thanks for comment #32.
I tried to figure out how to turn on the test mode with webdriver.

Looks like:

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("focusmanager.testmode",true);
WebDriver webDriver =  new FirefoxDriver(profile);

Source:
http://rostislav-matl.blogspot.com/2011/09/setting-firefox-preferences-via.html

Reported by mondhs on 2012-09-18 06:05:59

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Please note https://bugzilla.mozilla.org/show_bug.cgi?id=774892, which is the reason
this preference is not enable by Selenium yet.

Reported by dave.hunt on 2012-09-18 07:15:49

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

This issue was closed by revision 9e456160fa2f.

Reported by barancev on 2013-04-29 13:28:05

  • Status changed: Fixed

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Reopening, focusmanager.testmode is too fragile...

Reported by barancev on 2013-05-01 18:27:41

  • Status changed: New

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

I am having this issue with:

Browser: Firefox 22.0
Selenium Server: 2.33.0

I am using a tool called Behat that interfaces with Selenium, it's a PHP library, I
don't believe the problem lays there.

When filling a text input with focus on Firefox the change event is fired and my AJAX
request completes and loads options for a select, but when the focus is on another
application, the change event is not triggered and my tests fails.

Reported by danilo.cabello on 2013-06-27 17:40:25

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

I can confirm comment #37's issue with the following configuration:

Linux (RHEL6)
Firefox 17 ESR
Selenium 2.35
Using native events (required for expected focusmanager.testmode=true functionality)
Using focusmanager.testmode

This makes testing apps that depend on the onChange event difficult to debug of course,
since you cannot simultaneously have Firefox and a debugging tool (eg Eclipse) in focus.

Reported by alechenninger on 2013-10-07 16:56:49

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Not only "onchange" event, I also hit this issue for "onfocus" event when run test on
Selenium grid with lots of firefox instances.

Reported by chocean on 2013-10-14 07:04:58

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

I am facing the issue on JavaScript call from radio button from group, have to load
another radio group and have to save the value from the second group. Selenium is not
detecting JavaScript call and not loading the second set. 
Environment
Firefox 24
Windows 7 enterprise
selenium-server-standalone-2.35.0

Anyone who solved the JavaScript call issue please reply
Thanks.

Reported by kaywin.dcosta on 2013-10-17 09:48:11

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

I am facing this issue too for "onclick" event.

Environment
Firefox 25
Windows 7
selenium-server-standalone-2.37.0

Thanks

Reported by minkbear on 2013-10-31 20:47:30

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

I am struck up on this javascript call from my web application during
selenium testing. I need to record this and reproduce with different
scenariosas i already created one project which has to automate using an
ant build. As for this javascript call, all my recordings are failing.
Anywhere should we have to post for fixing this issue ?
Kaywin Charles Dcosta

Reported by kaywin.dcosta on 2013-11-02 06:14:16

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

I am also stuck on this. I tried on the selenium server 2.39.0 hoping for a solution
but still the same result. I am heavily dependent on this. The fact that there isn't
chromedriver for 64bit on mac makes it even worse. When would this be fixed? 

Anand.

Reported by ks.anand80 on 2014-01-14 02:56:42

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

If you have multiple firefoxdriver sessions running, then the way to solve this issue
is to set the "testmode" of the browser to true:

  FirefoxProfile profile = new FirefoxProfile();
  profile.setPreference("focusmanager.testmode", true);
  new FirefoxDriver(profile);

The underlying cause is down to how firefox handles focus and blur events when the
driver window doesn't have OS focus. That's Mozilla's desired behaviour.

Reported by simon.m.stewart on 2014-01-29 16:57:51

  • Status changed: WorkingAsIntended

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

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

  • 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