javaeclipse-pluginswteclipse-rcpswtbot

How to assign unique id to swt widget and get it in swtbot?


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?


Solution

  • 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);
        }
    }