Selenium IDE focus event doesn't fire unless window is active
I added this code to my web page:
<input id='argh' type='text' onfocus='this.value=7'>
Unless the browser window is active, the focus event does not seem to fire (using the 'focus' command) from Selenium:
# [info] Executing: |waitForElementPresent | argh | |
# [info] Executing: |focus | argh | |
# [info] Executing: |verifyValue | argh | 7 |
# [error] Actual value '' did not match '7'
It s开发者_StackOverflow中文版ucceeds just fine if the window is showing during testing. Furthermore, if I activate the browser window after the above failure, #argh is focused and fills with a seven, then I go back to Selenium and rerun the line, it succeeds. (Probably obvious, but...)
What am I doing wrong? How will I be able to test things like AJAX dropdown menus from Selenium without the browser showing?
My setup is Selenium IDE 1.0.2 on Firefox 3.5.3 on win32.
Does it make any difference if you use the fireEvent command?
fireEvent | argh | focus
Firstly the focus event DOES fire when using Selenium. However, as the browser Selenium is controlling typically does not have the focus within the Operating System, the browser will NOT consider any elements to have focus until the browser regains the focus.
I have been pulling my hair out over this, so worked up a solution to this problem. See http://blog.mattheworiordan.com/post/9308775285/testing-focus-with-jquery-and-selenium-or for a full explanation of the problem and a solution to this.
If you can't be arsed to read the lengthy explanation, simply include https://gist.github.com/1166821 BEFORE you include JQuery, and use $(':focus') to find the element that has focus, or .is(':focus') to check if an element has focus.
精彩评论