0

So I'm testing an eclipse plugin with SWTbot and I'm not getting the result I'm expect - when I cut the test down it turns out that the problem isn't with the bot it's with some code that I've copied accross from another part of the program (where it was fully functional)

The following code...

@RunWith(SWTBotJunit4ClassRunner.class)
public class Tests {

    private static SWTWorkbenchBot bot;

    @BeforeClass
    public static void beforeClass() throws Exception {
        bot = new SWTWorkbenchBot();
        bot.viewByTitle("Welcome").close();
    }

    @Test
    public  void maybeThisWillWork(){
        IWorkbenchWindow activeWorkbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        System.out.println("A");
        IWorkbenchPage activePage = activeWorkbenchWindow.getActivePage();
        System.out.println("B");
    }

    @AfterClass
    public static void sleep() {
        System.out.println("In the sleep function");
        bot.sleep(10000);
    }
}

Gives me the output -

A
In the sleep function

Rather than the expected

A
B
In the sleep function

Any ideas?

Joe
  • 4,367
  • 7
  • 33
  • 52

2 Answers2

0

you may need to run your test as JUnit plugin test. Have you tried that?

Bela Vizer
  • 2,527
  • 22
  • 26
  • I'm running it as an SWTBot test… I don't belive the SWTBot funcationality works otherwise… could be wrong though... – Joe Apr 08 '12 at 21:03
  • could you please check whether activeWorkbenchWindow is null or not? – Bela Vizer Apr 08 '12 at 21:17
0

So it turns out that the answer is thus (also a nice advantage of stackoverflow is that I actually solved this somewhere else, remembered I'd had a similar problem and then had to come back to stackoverflow to remind myself of the details)

SWTBot isn't running in the UI thread proper hence the null pointer errors, what I had to do was use effectively:

Display display = bot.getDisplay();
display.syncExec(objectThatdoesthethingiwanttogetdoneintheUIthread);
System.out.println(objectThatdoesthethingiwanttogetdoneintheUIthread.results);

...and that got things working...

Joe
  • 4,367
  • 7
  • 33
  • 52