Saturday, 24 January 2009

How I learned to love Selenium's fireEvent

"I clicked on that, why didn't the click work!"
I recently faced the challenge of using Selenium to automate a web application that stubbornly resisted my attempts to automate it - until I found the fireEvent!


No need to know the name of the application in question, but the basic situation I faced involved a form with a 'save' button.
I used Selenium to fill in the details in the form and click the save button.
When I run selenium I can see the text I enter on the form, and when the 'save' gets a click the little Ajax image for 'saving' appears, but when I check the data saved - nothing got saved.
I struggled with this on my own for a while then paired with someone to help.
Pairing brings a number of benefits:
  • 2 minds instead of one
  • you engage in the 'ah!' moments that only happen when you explain something to others
  • you get a fresh perspective on the problem
So instead of focusing on the 'save' event we had a look at the form input fields.
The input fields had a few events - one having an onblur event. The onblur event code seemed to suggest that it persisted the form data into an in-memory store which the 'save' event then processed. So the problem did not seem to involve the save event - the type command did not trigger an onblur when it 'moved' between fields.
Onblur fires when you lose focus so we first tried clicking on the other fields to make that happen.
But that didn't work.
And then we noticed the Selenium API command fireEvent. This lets you trigger the event for a particular element on a page so we simply fired the 'blur' event for the component 

selSession.fireEvent("edit_field_1", "blur");
And that worked - deep joy.
So lessons learned for me:
  • Selenium has a large API - which I need to spend more time learning the scope and extent of
  • When testing I need to use the Firefox/Firebug 'inspect element' functionality to learn a bit more about the app under test
  • I need to use fireEvent more instead of some of the fudges present in my existing tests - this might help make them more robust.
General lessons:
  • Any testing you do, learn the technology that underpins it
  • Work with developers and use their technical knowledge to help














30 comments:

  1. [...] Selenium: fireEvent() помогает в работе с элементами [...]

    ReplyDelete
  2. Thanks for this tip! I too need to learn the Selenium api.
    thanks again

    ReplyDelete
  3. seleniumbeginner20 August 2009 at 16:57

    This is a great piece of information..Thank you so much! I have been stuck with this problem for more than a day...and today is the day when i started loving selenium's fireEvent :)

    ReplyDelete
  4. Thanks for your post!
    fireEvent (blur) made my day!

    ReplyDelete
  5. I think I love you. Been pulling my hair out on this one. Thank you thank you thank you!

    ReplyDelete
  6. Thank you so much for this post! I was trying to test an ajax autocomplete input field that's triggered by the keyup event. I tried 'type', 'keyup', 'keypress', 'keydown' and combinations of all of those without any luck. I didn't realize that selenium did input without triggering events. I definitely agree that selenium has a large API, and it would take some time to learn to use it effectively. I just wish the docs and community support for it wasn't so crappy.

    ReplyDelete
  7. Thanks man!
    It really works. I've tried to fire a bunch of events on 'save' button, but not on text field itself :)

    Glad you got it working :)

    ReplyDelete
  8. thank you sososososo much!! this solves everything!!

    ReplyDelete
  9. Selenium.fireEvent(id,"blur") helped me.. Thanks..

    ReplyDelete
  10. Ah - but what if you need to supply arguments to the event? I need an onkeydown event that supplies the "k" key being pressed to test some JavaScript on a control....

    ReplyDelete
  11. Alan Richardson23 May 2011 at 23:58

    Ah, thanks for the comment David.

    You identify a situation where I would try and use selenium.keyPress or selenium.keyDown or possibly selenium.keyUp

    Possibly even the keyNative methods.

    I also use the 2Visual Event" bookmarklet http://www.sprymedia.co.uk/article/Visual+Event to help explore other possibilities when building automation.

    ReplyDelete
  12. Thank you very much, it helped me trigger my OnBlur javascript functionality.

    ReplyDelete
  13. Thanks mate... your posting was a great help for me!

    ReplyDelete
  14. Alan Richardson31 May 2011 at 03:36

    You're Welcome Khalil, thanks for leaving the comment.

    ReplyDelete
  15. Thanks so much for the tip. I am finally unstuck on this problem that I've been working on for 2 days.

    ReplyDelete
  16. Alan Richardson28 July 2011 at 01:15

    Happy to hear it helped, thanks for the comment Jennifer

    ReplyDelete
  17. Thanks for this post. I was dealing with almost the exact same issue a text fields's value will be updated on blur of the previous text field. I tried using click and doubleClick to no avail. Thanks!

    ReplyDelete
  18. You're welcome. Thanks for letting me know the post helped.

    ReplyDelete
  19. has some one just copied your content here -
    http://seleniumready.blogspot.com/2010/09/how-to-use-selenium-fire-event.html

    ReplyDelete
  20. Yes, it seems as though they have. Thanks for pointing this out Tarun. I've got my Voodoo dolls on standby.

    ReplyDelete
  21. The fireEvent works wonderfully, but what do I do when Selenium RC does not recognise it. I get the following error when RC encounters FireEvent (I was trying to click save and had fireEvent(id-of-save-button,onMouseOver ) followed by click(id-of-save-button)). I am trying to run a HTML test suite against IE8. It was recorded and tweaked in FF 3.6, using Selenium IDE 1.0.10. Selenium Server version is : Selenium server 2.8.0
    Error Text :
    Command execution failure. Please search the user group at https://groups.google.com/forum/#!forum/selenium-users for error details from the log window. The error message is: Invalid argument.

    Any tips will be very helpful and appreciated.

    Thanks
    RK

    ReplyDelete
  22. Pretty hard to diagnose from this. So some strategies...
    - upgrade Selenium to newer version,

    Try generating code and run it through an IDE - I really don't recommend the HTML Test Suite method of execution.

    - does it work stepping through in debug mode? If so it might be a timing issue

    ReplyDelete
  23. Thanks for your response Alan. The test case was working fine in IDE. I need to run my tests against IE8 therefore I had to run them using selenium server. It did run successfully when I changed the "onMouseOver" to "click" (in the fireEvent command). So instead of 2 steps where one is getting the mouse to move over the button and the other step is clicking it, I just fireEvent the "click". Unfortunately just having the command for "click" does not make the click happen.

    I'm wondering what you mean by the second tip in your response. "Try generating code and run it through an IDE – I really don’t recommend the HTML Test Suite method of execution. ".How can I generate code in IDE ? Isin't that the same as recording and tweaking where necessary. I am not familiar with any of the other languages that Selenium Server supports so I have to use HTML test suites only, for the Selenium Server execution. If selenium server is not very good/ stable about HTML test suites maybe my organisation needs to rethink their QA automation strategy (or I need to learn some other languages).I must say I have been facing more problems than success
    at every step.

    Thanks in advance for your response.
    RK

    ReplyDelete
  24. The IDE can export source code, which you can load into an IDE.

    If you do this you can learn to code and make your tests more robust.

    If you are using HTML suites as your automation strategy then, even without understanding your context, I'd say you need to rethink your approach.

    I show how to move quickly from the Selenium IDE into a programing IDE in this video http://www.youtube.com/user/EviltesterVideos#p/u/2/H0_KBr939mM

    By doing this you can step through the selenium test in a proper programming language, in a proper development environment.

    Automation involves programming, if you want to have a robust automation suite, I think you need to program it.

    This was why I wrote the book "Selenium Simplified" the free preview should get you started with the Eclipse IDE and move you towards programming, if it proves useful then you might want to invest in the full book. http://www.compendiumdev.co.uk/selenium

    A lot of other tutorials exist on the web. I think HTML test suites can be useful, but in very restricted circumstances.

    ReplyDelete
  25. Thanks a lot, Alan. This was very helpful information for me to get started on the right track.

    ReplyDelete
  26. You're welcome. Thank you for taking the time to ask.

    Alan

    ReplyDelete
  27. Thanks a lot for this - this has solved my problem before I'd pulled all my hair out!

    ReplyDelete
  28. Thanks for the feedback Gillian. Glad it helped.

    ReplyDelete
  29. Thanks a lot , Alan.
    I am using only a few api's to automate my UI.
    I needs to explore on all the API.
    Is there a UI where i can use most api's to get real time working knowledge ?

    ReplyDelete
  30. You can try the gwt showcase apps, they can be quite painful to automate.

    https://developers.google.com/web-toolkit/examples/

    ReplyDelete