Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Linux Tools Project/SWTBot Workarounds"

(Tree Item Expansion Failure)
(Radio Button Selection Problems)
 
Line 4: Line 4:
  
 
Support for SWTBot itself is available [https://wiki.eclipse.org/SWTBot/Support here].
 
Support for SWTBot itself is available [https://wiki.eclipse.org/SWTBot/Support here].
 
= Radio Button Selection Problems =
 
 
== Description ==
 
 
SWTBot is unable to properly select a radio button if another button in its group is already selected. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=344484 for details.
 
 
== Symptoms ==
 
 
* Tests that click on a non-selected radio button behave as if it were never clicked
 
* Tests that step through clicking each radio button in a group only succeed with tests on the button that was clicked first
 
 
== Workaround ==
 
 
In order to select a radio button, no other button in the group can be selected. If the index of a pre-selected button is known, you must first de-select it by calling this helper method:
 
 
<pre>
 
/**
 
* Deselects a radio button.
 
* Workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=344484
 
* @param currSelection The index of the radiobutton to deselect
 
*/
 
private void deselectDefaultSelection(final int currSelection) {
 
UIThreadRunnable.syncExec(new VoidResult() {
 
@Override
 
public void run() {
 
@SuppressWarnings("unchecked")
 
Matcher<Widget> matcher = allOf(widgetOfType(Button.class), withStyle(SWT.RADIO, "SWT.RADIO"));
 
Button b = (Button) bot.widget(matcher, currSelection);
 
b.setSelection(false);
 
}
 
});
 
}
 
</pre>
 
 
After calling this method, clicking on a radio button will work as expected. Make sure to call this method whenever you want to change the selected radio button.
 
  
 
= Main Menu Items Not Found =
 
= Main Menu Items Not Found =

Latest revision as of 06:00, 13 January 2022

{{#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