Google Web Toolkit and id

There are at least two points of pain when trying to automate a GWT with Selenium (and Selenium IDE). The easier of the two is that GWT does not assign id’s to its widgets by default which then necessitates the use of structural locators like XPath and CSS which can lead to brittle scripts. And the solution is of course easy, but requires developer assistance.

Method One – setting the id

According to Developer’s Guide – CSS Style the cleanest way to do this is by manipulating the DOM

Button b = new Button();
DOM.setElementAttribute(b.getElement(), "id", "my-button-id")

This is easy to do from a technical perspective, but it also means ‘extra’ work for the developers and getting buy-in from management that ‘Yes, if we are to really be successful in the this whole automation thing, we do need to change the app’ which can be hard.

Method Two – ensureDebugId

This seems like a bit of a kludge, but apparently there is a bit of a performance hit to the first method so they have come up with another way of conditionally including id on elements if you have the com.google.gwt.user.Debug module enabled on your app.

Button b = new Button();
b.ensureDebugId("foo");

A few notes around this

  • It will override an existing id with this one
  • The id it produces will be prefixed with gwt-debug-

It still requires that the application change for automation purposes, but it can be turned off in production so is perhaps more palatable to folks. Of course, it also means you cannot run your Se scripts against production which seems like an artificial limitation to me.

Method Three – setId

It appears that elements have a method to directly set the id without going to the DOM directly.

Button b = new Button();
b.getElement().setId("mycustomid");

The other problem is the oh-so-fun ‘it looks like a button but is not actually a button’ problem which is not as simple to solve. But as far as the locator problem goes, no more whining! :)

Post a Comment

Your email is never published nor shared. Required fields are marked *