How to close shells left open at the end of SWTBot test execution

aurelien.pupier
Blog Categories: 

When executing UI tests, such as SWTBot tests, when a test fails some popups might stay open and cause subsequent tests to fail. Even worse, the whole test suite execution can take longer than usual because all the following tests might fail after waiting for a timeout (of 5 seconds by default). It will also kill the visibility of the scope of impacted components. In order to avoid this quirk, we are using a custom TestSuite to register a custom JUnit Runlistener *on the *RunNotifier. After all these abstracts words, let’s see in details how to implement it.

Register the listener

You have to extend the Suite class and then to register the listener on the RunNotifier.

  1. public class CloseShellSuite extends Suite {
  2.  
  3. @Override
  4. public void run(final RunNotifier runNotifier) {
  5. final RunListener listener = CleanShellListener.getInstance();
  6. runNotifier.removeListener(listener);
  7. runNotifier.addListener(listener);
  8. super.run(runNotifier);
  9. }
  10.  
  11. protected CloseShellSuite(final Class<?> klass, final Class<?>[] suiteClasses) throws InitializationError {
  12. super(klass, suiteClasses);
  13. }

Implement the RunListener

You need to override the testFinished method. Inside this method, you can access the SWTWorkbenchBot to be able to close all open shells - taking care not to close the main Eclipse shell - and also close all editors in order to get back to a clean environment.

  1. public class CleanShellListener extends RunListener {
  2.  
  3. @Override
  4. public void testFinished(final Description description) throws Exception {
  5. final SWTWorkbenchBot bot = new SWTWorkbenchBot();
  6. try {
  7. closeAllShells(bot, description);
  8. bot.saveAllEditors();
  9. bot.closeAllEditors();
  10. } catch (final Exception e) {
  11. BonitaStudioLog.log("| Fails to clean shells after test : " + description.getMethodName());
  12. BonitaStudioLog.log("|====================================================");
  13. return;
  14. }
  15. }
  16.  
  17. private void closeAllShells(final SWTWorkbenchBot bot, final Description description) {
  18. final SWTBotShell[] shells = bot.shells();
  19. for (final SWTBotShell shell : shells) {
  20. if (shell.isOpen() && !isEclipseShell(shell)) {
  21. bot.captureScreenshot("screenshots/ShellOpenedAfter" + description.getMethodName() + ".jpg");
  22. shell.close();
  23. BonitaStudioLog.log("/!\\ Shell " + shell + " has been closed automatically,"
  24. + "please fix the corresponding test (" + description.getMethodName() + ")"
  25. + " to close it in @After (see screenshots)");
  26. }
  27. }
  28. }
  29.  
  30. @SuppressWarnings("boxing")
  31. public static boolean isEclipseShell(final SWTBotShell shell) {
  32. return UIThreadRunnable.syncExec(new BoolResult() {
  33.  
  34. public Boolean run() {
  35. return PlatformUI.getWorkbench().getActiveWorkbenchWindow()
  36. .getShell() == shell.widget;
  37. }
  38. });
  39. }
  40.  
  41. private static CleanShellListener INSTANCE = null;
  42.  
  43. private CleanShellListener() {
  44. };
  45.  
  46. public static CleanShellListener getInstance() {
  47. if (INSTANCE == null) {
  48. synchronized (CleanShellListener.class) {
  49. if (INSTANCE == null) {
  50. INSTANCE = new CleanShellListener();
  51. }
  52. }
  53. }
  54. return INSTANCE;
  55. }

Notice that we take a screenshot in the case of a remaining window. This is a big help for debugging.

This article is based on a simplified overview of code used at Bonitasoft. You can have a closer look on our Github repository.

Notifications