I have an app that fires an ajax request on a form field to validate its contents when I take focus off the field.
I am also using Cucumber, Webrat and Selenium for my integration tests.
I needed my integration tests to test the Ajax responses and the tests weren’t receiving a response from the Ajax requests.
The Problem
I found that by simply completing the web form, the ajax request was not being fired and my test was therefore failing when i checked for the existance of the Ajax response. It soon became clear that selenium doesn’t really interact with the form in the sense of selecting fields and entering values; It simply enters values. As such, the Ajax request was not firing and my test was failing.
The Solution
The solution is Selenium’s fireEvent method, which you can pass a form field id and the blur method:
selenium_session.fireEvent(”field”, “blur”);
In Webrat, this is ever simpler:
fire_event(”field”,”blur”)
On using this in a Cucumber step, the ajax is fired as the blur command tells the browser to take focus off the field.
Lovely!