Skip to main content
Jump to: navigation, search

Linux Tools Project/SWTBot Workarounds

< Linux Tools Project
Revision as of 13:55, 3 July 2014 by Aferrazz.redhat.com (Talk | contribs) (Adding minor scenarios)

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

Tree Item Expansion Failure

Description

Sometimes expanding a SWTBotTree (or SWTBotTreeItem) with the expandNode method will have no effect, causing tests that rely on (initially hidden) tree nodes to fail or throw errors. Note that this problem can happen in tests even if previous runs have succeeded and no changes to the code have been made since.

This problem is not due to concurrency or race conditions, so sleeping or using a wait condition to wait for a node to appear will not work. It is also unlikely to happen during local test runs, making it difficult to debug.

Symptoms

  • Tests that haven't been modified & were successful in previous builds are failing or throwing errors in later builds, with messages like "assertation failed:" or "could not find node with text: <name>"
  • The failed tests succeed when run locally, or when the offending build is retriggered or rebased, but fail again sometime later

Workaround

If the Shell containing the desired parent tree supports a text search, you should use that to search for the nodes you want to reveal & select rather than expanding nodes directly.

For instance, if you want to create use SWTBot to create a C Project with the "New Project" wizard, you should get SWTBot to type "C Project" in the wizard's search bar (shell.bot().text()), which will reliably expand the "C/C++" node and reveal its child "C Project" node.

As expanding nodes with this method happens asynchronously relative to SWTBot, you will need a wait condition to halt execution until the node is ready. Below is a DefaultCondition extension that performs this task.

private static class NodeAvailableAndSelect extends DefaultCondition {

	private SWTBotTree tree;
	private String parent;
	private String node;

	/**
	 * Wait for a tree node (with a known parent) to become visible, and select it
	 * when it does. Note that this wait condition should only be used after having
	 * made an attempt to reveal the node.
	 * @param tree The SWTBotTree that contains the node to select.
	 * @param parent The text of the parent node that contains the node to select.
	 * @param node The text of the node to select.
	 */
	NodeAvailableAndSelect(SWTBotTree tree, String parent, String node){
		this.tree = tree;
		this.node = node;
		this.parent = parent;
	}

	@Override
	public boolean test() {
		try {
			SWTBotTreeItem parentNode = tree.getTreeItem(parent);
			parentNode.getNode(node).select();
			return true;
		} catch (WidgetNotFoundException e) {
			return false;
		}
	}

	@Override
	public String getFailureMessage() {
		return "Timed out waiting for " + node; //$NON-NLS-1$
	}
}

Below is a demonstration of how to use these methods to create a new general project with SWTBot.

@Test
public void testCreateProject() {
	// Open the "New Project" wizard, which features a tree & a text search bar
	bot.menu("File").menu("New").menu("Project...").click();
	bot.shell("New Project").setFocus();

	// Want to create a general project, so search for the "Project" node
	bot.text().setText("Project");

	// Wait for the node to be revealed, knowing that its parent is the "General" node
	bot.waitUntil(new NodeAvailableAndSelect(bot.tree(), "General", "Project"));

	// Performing the wait condition selected the node, so we can now navigate through the wizard
	bot.button("Next >").click();
	bot.textWithLabel("Project name:").setText("My New Project");
	bot.button("Finish").click();
}

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:

/**
 * 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);
		}
	});
}

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.

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