Skip to main content

Jan 24, 2009 - 2 minute read - Evil Tester Selenium

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

You will need a Github account to comment. Or you can contact me with your comment.

I reserve the right to delete spam comments e.g. if your comment adds no value and its purpose is simply to create a backlink to another site offering training, or courses, or etc.