I want to assign a unique id to swt widget, and than get back the widget in SWTBot test with this unique id. Is there a way to do it?
In your Composite / UI part you set the data with the default SWT Bot Key or with your custom key
textOne.setData("org.eclipse.swtbot.widget.key", "textId1");
textTwo.setData("com.sample.my.custom.key", "textCustomId2");
In your SWTBot Test you can get the text as follows
// using the default SWTBot Key
botTextOne = bot.textWithId("textId1")
and
//using your custom key
botTextTwo = bot.textWithId("com.sample.my.custom.key", "textCustomId2")
References:
Unfortunately, there is no built-in way to do this.
I think your best option is to use the method Widget#setData(Object)
to set the id.
You can generate a (pseudo) random id using:
UUID id = UUID.randomUUID();
widget.setData(id);
(or use any id generation method you want).
To find your widget, you would have to search through the children of the Shell
(or the Composite
to which you can narrow it down) with any search algorithm you want (DFS, BFS, ...) and then compare the UUID
to the id you are searching for.
for(Control control : shell.getChildren())
{
UUID id = (UUID) control.getData();
if(id.equals(WHATEVER_HERE))
{
System.out.println(control);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With