Friday, September 10, 2010
 
 

Selecting ExtJS Grid Row using Watir

Grid is the most powerful widget in ExtJS and it becomes tricky at times to write test cases using Watir. One of the instance which I faced was selection of any row in grid with watir script.

From the script, I tried to find the DIV element of grid for the row using the class tag and fired the click event

1
$browser.div( :class => "x-grid3-row", :index => 10 ).click

The script immediatley highlighted 10th row in grid but did not get selected (Normally Grid row changes appearance when it gets selected). I tried the other way (firing mousedown event, thinking it would work ) as

1
2
$browser.div( :class => "x-grid3-row", 
                   :index => 10 ).fire_event( "onmousedown" )

Still no luck! It looked like as if grid is not responding to the events.

On debugging the issue, I found that whenever “mousedown” or “click” events are being fired from watir scripts, they were cascading “rowmousedown” or “rowclick” event appropriately to the Grid. But the function which changes the css class of row to be selected was not getting called. Again Why?

Stepping further, found the statement in RowSelectionModel class from where the event is simply ignored (as if nothing had happened)

1
2
3
4
5
Class - RowSelectionModel
handleMouseDown : function(g, rowIndex, e){
   if(e.button !== 0 || this.isLocked()){
      return;
   };

It seems that value of button property was not equal to ZERO when fired from watir script. However when I use mouse, button value is ZERO.

Why ExtJS has this comparison check for button property of Event? My guess is that ExtJS developer wanted to handle only those event which are being generated by clicking mouse. If the event was generated from Mouse, property button of event will definitely have the value as per the W3C standard or Microsoft standard ( which is obviously not the case with the events generated from watir scripts).

The question now, how do we generate mouse event using our watir scripts? Cause from watir library, there is no way to fire mouse event directly.

Solution to the above problem is to add the module as mentioned in Watir Wiki . Include the module in your script and modify your test statement as

1
$browser.div( :class => "x-grid3-row", :index => 10 ).left_click

It works perfectly!!!

for me, this is definitely elegant as I was trying to execute javascript code from watir script to select the row as

1
2
$browser.document.parentWindow.eval( 
      "Ext.getCmp( 'gridId' ).getSelectionModel().selectRow( 10 ) ")

On a side note, I have not heard of many instance where folks are praising Microsoft for implementing feature which is not according to the standard and still makes sense (specially true with Internet Explorer). Read the “Miscellaneous Properties” section in W3C Dom Events page for details of Event’s Button property. From the page

The Microsoft implementation is the only one that makes sense.

Share and Enjoy:
  • Digg
  • Twitter
  • Facebook
  • del.icio.us
  • StumbleUpon
  • Reddit
  • Turn this article into a PDF!
  • E-mail this story to a friend!
  • Slashdot
  • Yahoo! Buzz
  • Yahoo! Bookmarks
  • Google Bookmarks
  • LinkedIn
 

Tags: , , , , , ,

Comments

No comments so far.
 
 

 

Find Me!
View Kunal Kumar's profile on LinkedIn