“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
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.
- Any testing you do, learn the technology that underpins it
- Work with developers and use their technical knowledge to help