SWTBotTest Case should be halted until Progressbar is over

90 Views Asked by At

I am relatively new to SWTBot for Tool Testing.I am runing JUnit Test case where TestCase should run on selection of project and files in the project should be loaded before the test case starts executing but Testcase starts executing without waiting for the Loading Process.

Loading of the files in the project was provided by Eclipse (Plugin :-org.eclipse.sphinx.emf.workspace.loading ) using ProgressBar. By surfing through the Internet i have foundout
bot.waitUntil() is used to halt TestCase if any operation have to be completed before proceeding further. I have tried multiple options but unable to get the result i was expecting.

Can any one help me on this one

1

There are 1 best solutions below

0
Emmanuel Chebbi On

I advise you to call:

Job.getJobManager().join(
    IExtendedPlatformConstants.FAMILY_MODEL_LOADING, 
    new NullProgressMonitor()
);

instead of using bot.waitUntil. It should block the current thread (i.e. your test) until the file loading is done.

The Progress View is automatically filled by the Eclipse UI when Eclipse Jobs are running in background. In your case, those jobs are scheduled by the LoadJobScheduler. The idea is to directly use Eclipse's Jobs API to wait for those jobs to end instead of querying the UI. This is exactly what Job.getJobManager().join does, see also this answer.

Note: join takes as argument the family to which belong the jobs to wait for. I found it in the implementation of the loading Job.

Preventing infinite loops

Since join is blocking it might lead to infinite loops. You can use JobManager::find to check running jobs without blocking the thread:

bot.waitUntil(new DefaultCondition() {
    @Override
    public boolean test() throws Exception {
        String family = IExtendedPlatformConstants.FAMILY_MODEL_LOADING;
        boolean allJobsAreDone = Job.getJobManager().find(family).length == 0;
        return allJobsAreDone;
    }
}); 

With the code above, SWTBot will throw a TimeoutException if Jobs are still found running after a few seconds.