Time for action – writing an SWTBot test

The first step is to install SWTBot from the Eclipse update site. These examples were tested with http://download.eclipse.org/technology/swtbot/releases/latest/ version 2.3.0, but check out the book's errata for up-to-date information.

  1. Go to Help | Install New Software and enter the SWTBot update site.
  2. Select everything except the GEF feature:
    Time for action – writing an SWTBot test
  3. Click on Next to install.
  4. Restart Eclipse when prompted.
  5. Add the following bundle dependencies to the plug-in manifest for the com.packtpub.e4.junit.plugin project:
    1. org.eclipse.swtbot.junit4_x
    2. org.eclipse.swtbot.forms.finder
    3. org.eclipse.swtbot.eclipse.finder
    4. org.eclipse.ui
  6. Create a class called UITest in the com.packtpub.e4.junit.plugin package.
  7. Add a class annotation @RunWith(SWTBotJunit4ClassRunner.class).
  8. Create a method called testUI and with an annotation @Test.
  9. Inside the testUI method, create an instance of SWTWorkbenchBot.
  10. Iterate through the bot's shells and assert that the one that is visible has a title containing Eclipse. The code looks like:
    package com.packtpub.e4.junit.plugin;
    import static org.junit.Assert.assertTrue;
    import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
    import org.eclipse.swtbot.swt.finder.junit. SWTBotJunit4ClassRunner;
    import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    @RunWith(SWTBotJunit4ClassRunner.class)
    public class UITest {
      @Test
      public void testUI() {
        SWTWorkbenchBot bot = new SWTWorkbenchBot();
        SWTBotShell[] shells = bot.shells();
        boolean found = false;
        for (int i = 0; i < shells.length && !found; i++) {
          if (shells[i].isVisible()) {
            if (shells[i].getText().contains("Eclipse")) {
              found = true;
            }
          }
        }
        assertTrue(found);
      }
    }
  11. Run the test by right-clicking on the project and selecting Run As | SWTBot Test.
  12. Verify that the JUnit tests have passed:
    Time for action – writing an SWTBot test

What just happened?

SWTBot is a UI testing mechanism that allows the state of the user interface to be driven programmatically. In this test, a new SWTWorkbenchBot was created to interact with the Eclipse workbench (for pure SWT applications, the SWTBot class exists).

The bot iterates through the available shells once the workspace has been opened. Although more than one shell is returned in the list, not all of them will be visible. The shell's title can be obtained through the getText method, which returns Java – Eclipse SDK if the Eclipse SDK package opens on the Java perspective by default—but this value may differ depending on what perspective and which Eclipse package is being used.

The application run is similar to an Eclipse product launch; combinations of plug-ins, start-up properties, and product or application choices can be made via the Run or Debug configurations menu. As with ordinary JUnit tests, the launch can be invoked in Debug mode and the breakpoints set.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.35.75