public ICondition createTreeRowCountCondition (final int count) { return new ICondition () { @Override public boolean test() throws Exception { return geVisibleRowCount () == count; } @Override public void init(SWTBot bot) { } @Override public String getFailureMessage() { return "Expected row coutn not found "; } }; }
public void copy(ICondition condition) { CopyAction button = new CopyAction(); button.click(); editor.click(50, 50); if (condition == null) { condition = new org.eclipse.swtbot.swt.finder.waits.ICondition() { @Override public boolean test() throws Exception { PasteAction button = new PasteAction(); return button.isEnabled(); } @Override public void init(SWTBot bot) { } @Override public String getFailureMessage() { return "Paste button not enabled"; } }; } bot.waitUntil(condition); }
/** * Given a tree that contains an entry with <code>itemName</code> and a direct child with a name * matching <code>subchildName</code>, return its tree item. * * This method is useful when there is the possibility of a tree having two similarly-named * top-level nodes. * * @param mainTree the tree * @param itemName the name of a top-level node in the tree * @param subchildName the name of a direct child of the top-level node (used to uniquely select * the appropriate tree item for the given top-level node name) * @return the tree item corresponding to the top-level node with <code>itemName</code>that has a * direct child with <code>subchildName</code>. If there are multiple tree items that * satisfy this criteria, then the first one (in the UI) will be returned * * @throws WidgetNotFoundException if no such node can be found */ public static SWTBotTreeItem getUniqueTreeItem(final SWTBot bot, final SWTBotTree mainTree, String itemName, String subchildName) { for (SWTBotTreeItem item : mainTree.getAllItems()) { if (itemName.equals(item.getText())) { try { item.expand(); waitUntilTreeHasText(bot, item); if (item.getNode(subchildName) != null) { return item; } } catch (WidgetNotFoundException ex) { // Ignore } } } throw new WidgetNotFoundException( "The " + itemName + " node with a child of " + subchildName + " must exist in the tree."); }
private static boolean waitUntilTreeHasItemImpl(SWTBot bot, final TreeItem tree, final String nodeText) { try { bot.waitUntil(new DefaultCondition() { @Override public String getFailureMessage() { return "Could not find node with text " + nodeText; } @Override public boolean test() throws Exception { return getTreeItem(tree, nodeText) != null; } }); } catch (TimeoutException ex) { return false; } return true; }
private static boolean waitUntilTreeHasTextImpl(SWTBot bot, final TreeItem tree) { try { bot.waitUntil(new DefaultCondition() { @Override public String getFailureMessage() { return "Not all of the nodes in the tree have text."; } @Override public boolean test() throws Exception { return treeItemHasText(tree); } }); } catch (TimeoutException ex) { return false; } return true; }
/** * Blocks the caller until the tree item has the given item text. * * @param tree the tree item to search * @param nodeText the item text to look for * @throws org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException if the item could not * be found within the timeout period */ private static void waitUntilTreeItemHasItem(SWTBot bot, final SWTBotTreeItem tree, final String nodeText) { // Attempt #1 if (!waitUntilTreeHasItemImpl(bot, tree.widget, nodeText)) { // Attempt #2: Something went wrong, try to cautiously reopen it. bot.sleep(1000); // There isn't a method to collapse, so double-click instead tree.doubleClick(); bot.waitUntil(new TreeCollapsedCondition(tree.widget)); bot.sleep(1000); tree.expand(); bot.waitUntil(new TreeExpandedCondition(tree.widget)); if (!waitUntilTreeHasItemImpl(bot, tree.widget, nodeText)) { printTree(tree.widget); throw new TimeoutException( String.format("Timed out waiting for %s, giving up...", nodeText)); } } }
/** * Takes a screenshot. * * @param bot * the {@link SWTBot} factory instance to use, must not be {@code null} * @param prefix * base name of file, may be {@code null} only if useCaller is {@code true} * @param useCaller * if {@code true}, append the name of the calling method to the prefix */ public static void captureScreenshot(final SWTBot bot, final String prefix, final boolean useCaller) { Assert.isNotNull(bot, ARGUMENT_BOT); if (prefix == null && !useCaller) { throw new IllegalArgumentException(); } final StringBuilder sBuilder = new StringBuilder("screenshots/"); if (prefix != null) { sBuilder.append(prefix); } if (useCaller) { sBuilder.append(Reflect.getCallingMethod()); } sBuilder.append(".jpeg"); bot.captureScreenshot(sBuilder.toString()); }
/** * Attempts to expand all nodes along the path specified by the node array parameter. * The method is copied from SWTBotTree with an additional check if the node is already expanded. * * @param bot * tree bot, must not be {@code null} * @param nodes * node path to expand, must not be {@code null} or empty * @return the last tree item that was expanded, or {@code null} if no item was found */ public static SWTBotTreeItem expandNode(final SWTBotTree bot, final String... nodes) { Assert.isNotNull(bot, ARGUMENT_BOT); Assert.isNotNull(nodes, ARGUMENT_NODES); assertArgumentIsNotEmpty(nodes, ARGUMENT_NODES); new SWTBot().waitUntil(widgetIsEnabled(bot)); SWTBotTreeItem item = bot.getTreeItem(nodes[0]); if (!item.isExpanded()) { item.expand(); } final List<String> asList = new ArrayList<String>(Arrays.asList(nodes)); asList.remove(0); if (!asList.isEmpty()) { item = expandNode(item, asList.toArray(new String[asList.size()])); } return item; }
@Override public void beforeAllTests() { super.beforeAllTests(); final Display display = getDisplay(); // Creating the shell must be done on the UI thread. display.syncExec(new Runnable() { @Override public void run() { // Create and open the shell. shell = new Shell(display); shell.setLayout(new FillLayout()); shell.open(); } }); // Initialize static or otherwise shared resources here. // Set up the SWTBot for the shell. bot = new SWTBot(shell); return; }
public static void terminateAllLaunchConfigs(SWTBot bot) { SwtBotUtils.print("Terminating Launches"); ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager(); ILaunch[] launches = manager.getLaunches(); if (launches == null || launches.length == 0) { SwtBotUtils.print("No Launches to terminate"); } for (ILaunch launch : launches) { if (!launch.isTerminated()) { try { launch.terminate(); } catch (DebugException e) { SwtBotUtils.printError("Could not terminate launch." + e.getMessage()); } } } SwtBotWorkbenchActions.waitForIdle(bot); SwtBotUtils.print("Terminated Launches"); }
/** * Given a tree that contains an entry with <code>itemName</code> and a direct child with a name * matching <code>subchildName</code>, return its tree item. * * This method is useful when there is the possibility of a tree having two similarly-named * top-level nodes. * * @param mainTree the tree * @param itemName the name of a top-level node in the tree * @param subchildName the name of a direct child of the top-level node (used to uniquely select * the appropriate tree item for the given top-level node name) * @return the tree item corresponding to the top-level node with <code>itemName</code>that has a * direct child with <code>subchildName</code>. If there are multiple tree items that * satisfy this criteria, then the first one (in the UI) will be returned * * @throws IllegalStateException if no such node can be found */ public static SWTBotTreeItem getUniqueTreeItem(final SWTBot bot, final SWTBotTree mainTree, String itemName, String subchildName) { for (SWTBotTreeItem item : mainTree.getAllItems()) { if (itemName.equals(item.getText())) { try { item.expand(); SwtBotTreeActions.waitUntilTreeHasText(bot, item); if (item.getNode(subchildName) != null) { return item; } } catch (WidgetNotFoundException e) { // Ignore } } } throw new IllegalStateException("The '" + itemName + "' node with a child of '" + subchildName + "' must exist in the tree."); }
private static boolean waitUntilTreeHasItemImpl(SWTBot bot, final TreeItem tree, final String nodeText) { try { bot.waitUntil(new DefaultCondition() { @Override public String getFailureMessage() { return "Could not find node with text " + nodeText; } @Override public boolean test() throws Exception { return getTreeItem(tree, nodeText) != null; } }); } catch (TimeoutException e) { return false; } return true; }
private static boolean waitUntilTreeHasTextImpl(SWTBot bot, final TreeItem tree) { try { bot.waitUntil(new DefaultCondition() { @Override public String getFailureMessage() { return "Not all of the nodes in the tree have text."; } @Override public boolean test() throws Exception { return doesTreeItemHaveText(tree); } }); } catch (TimeoutException e) { return false; } return true; }
/** * Blocks the caller until all of the direct children of the tree have text. The assumption is * that the tree does not have any "empty" children. * * TODO: Refactor some of this logic; it follows the same general pattern as * {@link #waitUntilTreeItemHasItem(SWTBot, SWTBotTreeItem, String)}. * * @param tree the tree to search * @throws TimeoutException if all of the direct children of the tree do not have text within the * timeout period */ public static void waitUntilTreeHasText(SWTBot bot, final SWTBotTreeItem tree) throws TimeoutException { // Attempt #1 if (!waitUntilTreeHasTextImpl(bot, tree.widget)) { // Attempt #2: Something went wrong, try to cautiously reopen it. bot.sleep(1000); // There isn't a method to collapse, so double-click instead tree.doubleClick(); bot.waitUntil(new TreeCollapsedCondition(tree.widget)); bot.sleep(1000); tree.expand(); bot.waitUntil(new TreeExpandedCondition(tree.widget)); if (!waitUntilTreeHasTextImpl(bot, tree.widget)) { printTree(tree.widget); throw new TimeoutException( "Timed out waiting for text of the tree's children, giving up..."); } } }
@Test public void canLoginAdmin() throws Exception { SWTBotShell loginShell = BOT.activeShell(); loginShell.activate(); BOT.textWithLabel("Deployment Url").setText(ADDRESS); BOT.textWithLabel("Username").setText(ADMIN_UNAME); BOT.textWithLabel("Password").setText(ADMIN_PWD); BOT.button("Login").click(); BOT.waitWhile(new ICondition() { @Override public boolean test() throws Exception { String title = BOT.activeShell().getText(); return title.equals("Loading Templates"); } @Override public void init(final SWTBot bot) { } @Override public String getFailureMessage() { return "Unable to Login"; } }); }
public GraphModelProperties(SWTBot bot, String project, String folder,String pkg, String filename) { super(); this.bot = bot; this.project = project; this.folder = folder; this.pkg = pkg; this.filename = filename; bot.tree().setFocus(); bot.tree().select("GW4E"); bot.waitUntil(Conditions.waitForWidget(WidgetMatcherFactory.withId(GraphModelPropertyPage.GW4E_LABEL_ID, GraphModelPropertyPage.GW4E_LABEL_ID))); }
/** * Wait until all background tasks are complete. */ public static void waitForProjects(final SWTBot bot, IProject... projects) { Runnable delayTactic = new Runnable() { @Override public void run() { bot.sleep(300); } }; ProjectUtils.waitForProjects(delayTactic, projects); }
private static void openPreferencesDialogViaEvents(SWTBot bot) { Display display = bot.getDisplay(); Event event = new Event(); // Move to the "Apple" menu item (it catches 0, 0) event.type = SWT.MouseMove; event.x = 0; event.y = 0; display.post(event); bot.sleep(OPEN_PREFERENCES_DIALOG_DELAY_MS); // Click event.type = SWT.MouseDown; event.button = 1; display.post(event); bot.sleep(SwtBotTestingUtilities.EVENT_DOWN_UP_DELAY_MS); event.type = SWT.MouseUp; display.post(event); bot.sleep(OPEN_PREFERENCES_DIALOG_DELAY_MS); // Right to the "Eclipse" menu item SwtBotTestingUtilities.sendKeyDownAndUp(bot, SWT.ARROW_RIGHT, '\0'); bot.sleep(OPEN_PREFERENCES_DIALOG_DELAY_MS); // Down two to the "Preferences..." menu item SwtBotTestingUtilities.sendKeyDownAndUp(bot, SWT.ARROW_DOWN, '\0'); bot.sleep(OPEN_PREFERENCES_DIALOG_DELAY_MS); SwtBotTestingUtilities.sendKeyDownAndUp(bot, SWT.ARROW_DOWN, '\0'); bot.sleep(OPEN_PREFERENCES_DIALOG_DELAY_MS); // Press enter SwtBotTestingUtilities.sendKeyDownAndUp(bot, 0, '\r'); bot.sleep(OPEN_PREFERENCES_DIALOG_DELAY_MS); }
/** * Blocks the caller until all of the direct children of the tree have text. The assumption is * that the tree does not have any "empty" children. * * @param tree the tree to search * @throws TimeoutException if any of the direct children of the tree do not have text within the * timeout period */ public static void waitUntilTreeHasText(SWTBot bot, final SWTBotTreeItem tree) throws TimeoutException { // TODO: Refactor some of this logic; it follows the same general pattern as // {@link #waitUntilTreeItemHasItem(SWTBot, SWTBotTreeItem, String)}. // Attempt #1 if (!waitUntilTreeHasTextImpl(bot, tree.widget)) { // Attempt #2: Something went wrong, try to cautiously reopen it. bot.sleep(1000); // There isn't a method to collapse, so double-click instead tree.doubleClick(); bot.waitUntil(new TreeCollapsedCondition(tree.widget)); bot.sleep(1000); tree.expand(); bot.waitUntil(new TreeExpandedCondition(tree.widget)); if (!waitUntilTreeHasTextImpl(bot, tree.widget)) { printTree(tree.widget); throw new TimeoutException( "Timed out waiting for text of the tree's children, giving up..."); } } }
/** Click the button, wait for the window change. */ public static void clickButtonAndWaitForWindowChange(SWTBot bot, final SWTBotButton button) { performAndWaitForWindowChange(bot, new Runnable() { @Override public void run() { button.click(); } }); }
/** Click the button, wait for the window close. */ public static void clickButtonAndWaitForWindowClose(SWTBot bot, final SWTBotButton button) { performAndWaitForWindowClose(bot, new Runnable() { @Override public void run() { button.click(); } }); }
/** * Injects a key or character via down and up events. Only one of {@code keyCode} or * {@code character} must be provided. Use * * @param keyCode the keycode of the key (use {@code 0} if unspecified) * @param character the character to press (use {@code '\0'} if unspecified) */ public static void sendKeyDownAndUp(SWTBot bot, int keyCode, char character) { Event ev = new Event(); ev.keyCode = keyCode; ev.character = character; ev.type = SWT.KeyDown; bot.getDisplay().post(ev); bot.sleep(EVENT_DOWN_UP_DELAY_MS); ev.type = SWT.KeyUp; bot.getDisplay().post(ev); }
/** * Blocks the caller until the given shell is no longer active. */ public static void waitUntilShellIsNotActive(SWTBot bot, final SWTBotShell shell) { bot.waitUntil(new DefaultCondition() { @Override public String getFailureMessage() { return "Shell " + shell.getText() + " did not close"; //$NON-NLS-1$ } @Override public boolean test() throws Exception { return !shell.isActive(); } }); }
/** * Blocks the caller until the given shell is closed. */ public static void waitUntilShellIsClosed(SWTBot bot, final SWTBotShell shell) { bot.waitUntil(new DefaultCondition() { @Override public String getFailureMessage() { return "Shell " + shell.getText() + " did not close"; //$NON-NLS-1$ } @Override public boolean test() throws Exception { return !shell.isOpen(); } }); }
/** * Wait until the given text widget contains the provided string */ public static void waitUntilStyledTextContains(SWTBot bot, final String text, final SWTBotStyledText widget) { bot.waitUntil(new DefaultCondition() { @Override public boolean test() throws Exception { return widget.getText().contains(text); } @Override public String getFailureMessage() { return "Text not found!"; } }); }
/** * Spin until the given project actually exists and is facetd. * * @return the project */ private static IProject waitUntilFacetedProjectExists(SWTBot bot, final IProject project) { bot.waitUntil(new DefaultCondition() { @Override public String getFailureMessage() { return "Project does not exist! " + project; } @Override public boolean test() throws Exception { return project.exists() && FacetedProjectFramework.isFacetedProject(project); } }); return project; }
/** * Creates an SWTBot for the dialog's shell, not for the test shell. */ @Override protected SWTBot getBot() { // Get the shell for the dialog, then return its associated bot. for (SWTBotShell shell : super.getBot().shells()) { if (shell.widget == dialog.getShell()) { return shell.bot(); } } return null; }
/** * Wait until all background tasks are complete. */ public static void waitForIdle(SWTBot bot) { SwtBotUtils.print("\t\tWaiting for idle"); while (!Job.getJobManager().isIdle()) { bot.sleep(500); } SwtBotUtils.print("\t\tNow idle"); }
private static void openPreferencesDialogViaEvents(SWTBot bot) { Display display = bot.getDisplay(); Event ev = new Event(); // Move to the "Apple" menu item (it catches 0, 0) ev.type = SWT.MouseMove; ev.x = 0; ev.y = 0; display.post(ev); bot.sleep(OPEN_PREFERENCES_DIALOG_DELAY_MS); // Click ev.type = SWT.MouseDown; ev.button = 1; display.post(ev); bot.sleep(SwtBotUtils.EVENT_DOWN_UP_DELAY_MS); ev.type = SWT.MouseUp; display.post(ev); bot.sleep(OPEN_PREFERENCES_DIALOG_DELAY_MS); // Right to the "Eclipse" menu item SwtBotUtils.sendKeyDownAndUp(bot, SWT.ARROW_RIGHT, '\0'); bot.sleep(OPEN_PREFERENCES_DIALOG_DELAY_MS); // Down two to the "Preferences..." menu item SwtBotUtils.sendKeyDownAndUp(bot, SWT.ARROW_DOWN, '\0'); bot.sleep(OPEN_PREFERENCES_DIALOG_DELAY_MS); SwtBotUtils.sendKeyDownAndUp(bot, SWT.ARROW_DOWN, '\0'); bot.sleep(OPEN_PREFERENCES_DIALOG_DELAY_MS); // Press enter SwtBotUtils.sendKeyDownAndUp(bot, 0, '\r'); bot.sleep(OPEN_PREFERENCES_DIALOG_DELAY_MS); }
public static void clickButtonAndWaitForWindowChange(SWTBot bot, final SWTBotButton button) { performAndWaitForWindowChange(bot, new Runnable() { @Override public void run() { button.click(); } }); }
/** * Injects a key or character via down and up events. * * @param keyCode the keycode of the key (only this or character have to be provided.) * @param character the character to press (only this or keyCode have to be provided.) */ public static void sendKeyDownAndUp(SWTBot bot, int keyCode, char character) { Event ev = new Event(); ev.keyCode = keyCode; ev.character = character; ev.type = SWT.KeyDown; bot.getDisplay().post(ev); bot.sleep(EVENT_DOWN_UP_DELAY_MS); ev.type = SWT.KeyUp; bot.getDisplay().post(ev); }
/** * Opens the Debug Configurations... perspective. */ public static void openDebugConfiguration(final SWTBot bot) { SwtBotUtils.performAndWaitForWindowChange(bot, new Runnable() { @Override public void run() { // Be sure the main window has focus setFocusMainShell(bot); bot.menu("Run").menu("Debug Configurations...").click(); } }); bot.activeShell().setFocus(); }
/** * Opens a resource using the Open Resource dialog. */ public static void openResource(final SWTBot bot, String fileName) { SwtBotUtils.performAndWaitForWindowChange(bot, new Runnable() { @Override public void run() { bot.menu("Navigate").menu("Open Resource").click(); } }); bot.text().typeText(fileName); SwtBotUtils.clickButtonAndWaitForWindowChange(bot, bot.button("Open")); }
@Test public void testCreateProfile() { bot.menu("File").menu("New Profile").click(); SWTBot profile = bot.shell("Profile").bot(); assertTrue("Profile Dialog opens with General Tab selected", profile.tabItem("General").isActive()); profile.text(0).setText("SWTBot Profile 1"); profile.text(1).setText("Description"); profile.button("Ok").click(); }
@Before public void setUpBefore() throws Exception { tempConfigDir = new TemporaryFolder(); tempConfigDir.create(); System.setProperty("net.sourceforge.fullsync.configDir", tempConfigDir.getRoot().toString()); System.setProperty("net.sourceforge.fullsync.skipExit", "true"); System.setProperty("net.sourceforge.fullsync.skipHelp", "true"); System.setProperty("net.sourceforge.fullsync.skipWelcomeScreen", "true"); applicationThread = new Thread(() -> Main.main(new String[] { "-v" })); applicationThread.setName("FullSync GUI"); applicationThread.start(); assertTrue("GUI startup failed, Display not found", null != waitForDisplayToAppear(GUI_STARTUP_TIMEOUT)); bot = new SWTBot(); }
@Override public void init(SWTBot bot) { }