Question

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?

Was it helpful?

Solution 2

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top