0

As far as I can tell, there are two ways to set the IDs for web elements from within GWT. I need to perform Selenium automated testing, and would appreciate insight into the relative advantages of each technique.

Option 1: the generally accepted method

uiObject.ensureDebugId("idForMyElement");

This solution is well documented, and officially supported by google. However it comes with some disadvantages:

My team will have to add lines to every package's gwt.xml file like so:

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

Furthermore, the Debug class prepends "gwt-debug" to IDs by default. I realize I can change this behavior, but I don't like adding complexity where I can help it.

Option 2: use a generic setAttribute call

uiObject.getElement().setAttribute("id", "idForMyElement);

This seems cleaner to me- are there any downsides I might be overlooking?

Relevant resources: How can I set id for GWT widgets in UiBinder? https://code.google.com/p/google-web-toolkit/issues/detail?id=4176

Community
  • 1
  • 1
  • What do you want to do with the ids? Also the inherit has just to be added into your .gwt.xml – BeWu Apr 23 '15 at 13:19
  • @user2248239 Automated testing using Selenium – hash_collision Apr 23 '15 at 13:20
  • Well a generic setAttribute call would do the same trick i guess but i don't see such a big advantage except that you won't have to add gwt-debug every time. Even if you take the setAttribute approach you have to prepare the gui elements anyway. And the debug prefix can also be removed with just one line of code anyway. So there is not such a big difference in complexity. – BeWu Apr 23 '15 at 13:25
  • FYI: Here's a small class that will help you find element by their debug id using WebDriver: https://github.com/ArcBees/ArcBees-Common/blob/master/testutils/src/main/java/com/arcbees/test/ByDebugId.java – spg Apr 23 '15 at 14:54
  • Thanks all. @user2248239 I saw an answer elsewhere that suggested I'd have to write my own replacement class, but a one-liner is no big deal. – hash_collision Apr 23 '15 at 21:13

1 Answers1

3

The first option is better because you can compile out debug ids from your production code, making it smaller.

With the second option ids will be there even when you don't need them anymore.

Andrei Volgin
  • 40,755
  • 6
  • 49
  • 58