Programitacally generate unique IDs for components


we need unique IDs for components for testing purpose. We don’t want to call separatly the “setId” for every components because it not efficient, and we want to use always the same unique ID for the same component.

How to start?


I am not sure how somebody else can generate stable id:s for your component - what would it be based on?

  • the caption of a button can change so that is not so good
  • the location in the code where you create the button can change so that is not good
  • the number of components you create before the button can change so that is not good
  • the component hierarchy where the button is added can change so that is not good

I can’t think of any other stable id than one set in code using setId

Now if you do not care about what the id is but just want it to be stable, you can probably auto generate the setId calls into your source code and never change them afterwards. Not sure what a good tool for that would be.

I don’t know how, but we need it for testing purpose. It needs a cache where already generated IDs stored and in next run use it, I don’t know exactly but I need to solve them.

How do You test the UI if You can’t catch the components?


You can write tests that are based on the structure of the UI. If you have a simple login screen, it could have a TextField for the user name, a PasswordField for the password and a Button “Submit” to submit the login. You find the first TextField in the view, that’s the username field. You find the first PasswordField, that’s the password field. You find a Button with the caption “Submit”, that’s the button you want to click. No ids needed in this simple scenario. If you have lots of TextFields, it might make sense to add ids for those, but you certainly don’t need an id for every component.