My Blog has moved to Github Pages

Saturday, 16 October 2010

GWT Widgets with html id attributes

It isn't immediately obvious how to get your GWT widgets to produce id attributes that you can use from external scripts or testing tools like WebDriver or Selenium.

To make your widgets produce id attributes you need to invoke the ensureDebugId method with an id string, e.g.

Label myLabel = new Label("Say something");
myLabel.ensureDebugId("myLabel");

Now, if you run that you still won't get any id attributes. The clue is in the method name ensureDebugId. You actually need to inherit the Debug module in your module descriptor (.gwt.xml) file.  The module to inherit is: 

<inherits name="com.google.gwt.user.Debug"/>

Now if you view the source of your label you'll see that it has an id attribute, but you might be surprised that it doesn't quite match the id you supplied ("myLabel"). In fact it will say "gwt-debug-myLabel".

1 comment:

  1. better way to setting ids:
    myWidget.getElement().setId("myWidget");

    this way you don't need to inherit Debug module and id would be exactly "myWidget"

    ReplyDelete