Thursday, May 28, 2009

Selenium with GXT: getting textfields to emit those key events

In my Stamp Editor I have a tree browser which can browse collections, albums and countries. Directly above this, I have a StoreFilterField in which I can type text to filter the tree. I wanted to write a few selenium tests for this functionality to verify the items are found. The problem I ran into is when I told selenium to type the text, the text would appear but the filter would not get invoked. I knew this had something to do with the events being emitted so I tried to figure out what was going on. It turns out, it appears to be a timing issue. A great link which discusses the Javascript events (and browser differences) is here. Since the only selenium browser I am supporting for now is Firefox 3.X, the events I needed to emit to tell the textfield to start filtering was:
  1. keydown
  2. keypress
  3. keyup

The problem was, is even after emitting these events, the filtering was not getting invoked. After playing around in Firebug, I discerned that I needed to "refire" these events after a point of time. I wrapped this into a common method of my selenium solutions such that the code now looked like this:

public void textWithEvents( String locator, String text ) { 
   selenium.typeKeys(locator, text);
   SeleniumHelper.pause(500); // give events time to breathe
   selenium.fireEvent(locator, "keydown");
   selenium.fireEvent(locator, "keypress");
   selenium.fireEvent(locator, "keyup");

Version Info
GXT: 2.0-M1
Selenium: 1.0-beta2