Skip to main content
Jump to: navigation, search

Linux Tools Project/SWTBot Workarounds

{{#eclipseproject:tools.linuxtools}}

Linux Tools
Website
Download
Community
Mailing ListForumsIRCmattermost
Issues
OpenHelp WantedBug Day
Contribute
Browse Source

When creating SWTBot Tests for Linux Tools projects, developers may experience unusual or unexpected test failures. Listed on this page are some common SWTBot issues known to affect Linux Tools SWTBot tests, and workarounds for them until they are directly fixed by SWTBot.

Support for SWTBot itself is available here.

Main Menu Items Not Found

Description

When trying to access a menu item from the main Eclipse shell with bot.menu(String), a WidgetNotFound exception may be thrown. An example case of this is accessing the File menu with bot.menu("File"). The reason this exception occurs is because the main shell must be active whenever a menu item is accessed; otherwise, the menu widget will not be seen by the bot.

Symptoms

  • Menus can be clicked at certain points in the test, but not others
  • A menu is clicked on to open a dialog shell, but after that shell is closed, another attempt to access a menu item fails

A likely cause of these symptoms is the loss of focus of the main shell after certain operations, especially the appearance of new shells.

Workaround

As the main Eclipse shell must be active before a menu item is accessed, the main shell should be captured and stored in a field variable so that it can be brought into focus at any time. Call the following method at the beginning of a test class (preferably at the start of a @BeforeClass method) to safely capture the shell:

public static void getMainShell() {
    SWTWorkbenchBot bot = new SWTWorkbenchBot();
    mainShell = null;  // statically defined elsewhere

    for (int i = 0, attempts = 100; i < attempts; i++) {
        for (SWTBotShell shell : bot.shells()) {
            if (shell.getText().contains("Eclipse Platform")) {
                mainShell = shell;
                shell.setFocus();
                break;
            }
        }
    }
}

Now the main shell can be brought into focus before any call to bot.menu(String). However, doing so may be tedious and error-prone, so use the following helper method instead when clicking on main menu items:

/**
  * Click an item from the main Eclipse menu, with a guarantee that the main
  * shell will be in focus.
  * @param items The names of each item in the path to the target item to click.
  * For example, to click "File->New->Project...", the items would be "File",
  * "New", and "Project...".
  */
public static void clickMainMenu(String... items) {
    if (items.length == 0) {
        return;
    }
    mainShell.setFocus();
    SWTBotMenu menu = bot.menu(items[0]);
    for (int i = 1; i < items.length; i++) {
        menu = menu.menu(items[i]);
    }
    menu.click();
}

Minor Scenarios

  • If finding a view with bot.viewByTitle(String) doesn't work, try bot.viewByPartName(String) instead.
  • When attempting to click on an item from a drop down button menu, you may get a "Widget not found" exception. To avoid this, activate the shell containing the button (with bot.activate(String)) before accessing the drop down menu.

Back to the top