alblue / com.packtpub.e4

Code samples for the "Eclipse Plugin Development by Example: Beginners Guide" book 978-1782160328
Eclipse Public License 1.0
55 stars 37 forks source link

ch9 SWTBot test won't work second time #3

Closed rayjcwu closed 11 years ago

rayjcwu commented 11 years ago

Hi author, I follow your book trying to create a SWTBot test case. For the first time, it takes a very long time to run, but in the end it would pass all test cases. However when I try to run the same code for second time, only testUI() will pass, the other three will have org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException: The widget was null.

I though I have typo somewhere so I download the sample code from github, but the problem still exist. In the book you worte

If one is not currently visible, it polls (every 500 milliseconds by default) until one is found or the default timeout period (5 seconds) ends when a WidgetNotFoundException is thrown

But I don't see why the first time all test cases will pass but not the second time.

alblue commented 11 years ago

What operating system are you using? Does the launched Eclipse application run and then close properly after the tests have run? Is this multiple test methods in the same UI Test class, or are they separate test methods in different classes?

rayjcwu commented 11 years ago

I guess I found where the problem is. In my Eclipse Juno, when I click Run As -> SWTBot, it will show a gray window with "Eclipse" as name and start printing a lot of exception messages in console every fixed duration of time. If I let the program run for a long time, it might success. But I change focus to another program, so after a long time of trying to get an active shell or something, that program still cannot find it, so it will fail.

I am not sure it's caused by Eclipse Juno or some configurations I changed in Juno, but this polling behavior won't happen in Eclipse Kepler. However, if I run this SWTBot test in Eclipse Kepler, there will be an Open Associated Perspective? ..... Do you want to open this perspective now? dialog asking you to change to java perspective. Without manually click yes, all test cases will fail.

In Tycho, I change my default JDK from 1.6 to 1.7 and it works perfectly. Not sure why it won't ask about java perspective though.

alblue commented 11 years ago

Ah OK - when an editor opens a new Java file for the first time it may prompt to ask the user whether they want to switch to the Java perspective or not. The best way to avoid that is to switch to the Java perspective at the start of the test; that way, you know you won't get that dialog up.

If a test condition fails in the UI test, it will block for a while and then fail the test. That sounds like what happens here.

The dialog "Do you want to switch to the Java Perspective?" also has a "Do not ask again" checkbox, which can also be invoked to persistently ignore the dialog in future. It may be that the switch to a different Java version resulted in the test which persisted the 'do not store this' if you interacted with it, and thereafter the dialog is not shown again (so the tests now work).