I use setId
a lot for automated UI tests within my Vaadin application. For performance reasons, I would like to remove this ID's in production mode. Is there any good way to do so?
You can check if you are currently running in Vaadin Production Mode like this
VaadinService.getCurrent().getDeploymentConfiguration().isProductionMode();
So if you are setting your components id with setId()
method, you can easily set it only when not in production mode, for example:
boolean isProductionMode = VaadinService.getCurrent().getDeploymentConfiguration().isProductionMode();
if(!isProductionMode) {
foo.setID(FOO_ID);
}
But I would consider whether to use this approach at all. To how many components do you assign id for web tests? If the number is not extremely high, the performance hit will be negligible, while your code will be cluttered too much with production mode checks. In many cases code readability and simplicity is more important than a minor performance hit.
Alternatively, you can rewrite many of your component selectors (assuming you are using Vaadin testbench?) using xpath queries which do not depend on component Ids but on some already present information instead - like "location" attribute when using custom layouts, css class, position in parent container etc.