Hi Patrick,

I tried your code as-is and it works for me, the JUnit test is successful.

Make sure that the test is run in a non-UI thread, otherwise if the test runs 
in the UI thread, it won't give a chance for the asyncExec() to execute.
How can I do that by just running the tests as a Junit Test?

I found a solution that works 
(https://github.com/LorenzoBettini/junit-swtbot-example/blob/master/mathutils.ui.tests/src/mathutils/ui/tests/AbstractMathUtilsWindowTest.java)

But IMHO this seems a little bit too much overhead for a simple Dialog test.

Stefan


Von: swtbot-dev-boun...@eclipse.org <swtbot-dev-boun...@eclipse.org> Im Auftrag 
von Patrick Tasse
Gesendet: Freitag, 21. September 2018 15:03
An: SWTBot developers list <swtbot-dev@eclipse.org>
Betreff: Re: [swtbot-dev] Testing simple SWT Dialogs

Hi Stefan,

I tried your code as-is and it works for me, the JUnit test is successful.

Make sure that the test is run in a non-UI thread, otherwise if the test runs 
in the UI thread, it won't give a chance for the asyncExec() to execute.

I think you could create the dialog with a null parent shell in openConfirm() 
instead of an new invisible shell.

I would put back getting the dialog shell by name and activate() it, otherwise 
the button() call could find any "OK" button anywhere in any shell.
To be more strict, get a bot() from SWTShell, and find the button in that bot. 
That will restrict the search to the widgets inside that specific shell.

Patrick




On Fri, Sep 21, 2018 at 3:49 AM Stefan Nöbauer 
<stefan.noeba...@kgu-consulting.com<mailto:stefan.noeba...@kgu-consulting.com>> 
wrote:
Hi Patrick,

Thanks for your answer!

But somehow Display.asyncExec is not called at all in my test.
The syncExec is calling my MessageDialog fine. But than the test is waiting for 
the Dialog to be closed.

My code locks like that now:

    @Test
    public void testInfoDialogWithDontShowAgain() {

        Display.getDefault().asyncExec(() -> {
            System.out.println("Hallooooo");
   MessageDialog.openConfirm(new Shell(Display.getDefault()), "A
MessageDialog", "Buhu");
        });

        SWTBotPreferences.PLAYBACK_DELAY = 100;

        SWTBot bot = new SWTBot();
        bot.button("OK").click();
    }

Could you give me an example to open this MessageDialog and close it with 
SWTBot by clicking the OK Button?

Thanks very much
Stefan


Von: swtbot-dev-boun...@eclipse.org<mailto:swtbot-dev-boun...@eclipse.org> 
<swtbot-dev-boun...@eclipse.org<mailto:swtbot-dev-boun...@eclipse.org>> Im 
Auftrag von Patrick Tasse
Gesendet: Donnerstag, 20. September 2018 20:58
An: SWTBot developers list 
<swtbot-dev@eclipse.org<mailto:swtbot-dev@eclipse.org>>
Betreff: Re: [swtbot-dev] Testing simple SWT Dialogs

Hi Stefan,

The short answer is that the Shell you create to feed to the SWTBot instance 
has not been opened, so there is no active shell which makes activeShell() fail.

Long answer:

Usually we use SWTBot to test an existing application. Here the test is both 
creating the application and testing it. I guess it is OK for trying out SWTBot.

However you probably should create your application (the Dialog under test) 
using purely SWT APIs, not using SWTBot APIs (for example you can use 
Display.getDefault().asyncExec(...) directly). Then use SWTBot APIs to test the 
dialog.

I would try to avoid using activeShell() as much as possible. Lately we notice 
problems where opened shells do not become active, due to the platform or the 
window manager, I'm not sure. But it's usually better to get a shell by name 
and activate it manually, like you do at the end of the test.

It's usually not necessary to add sleeps in the tests, most SWTBot finder 
methods like shell(...) or button(...) have a retry with timeout mechanism, so 
if the searched widget comes only after a short delay, it still works. Not 
activeShell() though, it tries only once and fails immediately...

Hope this helps,
Patrick

On Thu, Sep 20, 2018 at 8:52 AM Stefan Nöbauer 
<stefan.noeba...@kgu-consulting.com<mailto:stefan.noeba...@kgu-consulting.com>> 
wrote:
Hi,

I am new to SWTBot and have some troubles writing my first test for a simple 
Dialog.

My test looks like:

    public void testInfoDialogWithDontShowAgain() {
        SWTBotPreferences.PLAYBACK_DELAY = 100;

        bot = new SWTBot(new Shell(Display.getDefault()));

        bot.activeShell().display.asyncExec(new Runnable() {
                public void run() {
                    MessageDialog.openConfirm(bot.activeShell().widget, "A 
MessageDialog", "Buhu");
                }
            });
        bot.shell("A MessageDialog").activate();
        bot.sleep(5000);
        bot.button("OK").click();
    }

I get following Trace:

org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException: The widget 
was null.
                at 
org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot.<init>(AbstractSWTBot.java:110)
                at 
org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBotControl.<init>(AbstractSWTBotControl.java:48)
                at 
org.eclipse.swtbot.swt.finder.widgets.SWTBotShell.<init>(SWTBotShell.java:56)
                at 
org.eclipse.swtbot.swt.finder.widgets.SWTBotShell.<init>(SWTBotShell.java:45)
                at 
org.eclipse.swtbot.swt.finder.SWTBotFactory.activeShell(SWTBotFactory.java:461)
                at 
org.eclipse.swtbot.swt.finder.SWTBot.activeShell(SWTBot.java:1)
                at 
de.kgucms.tps.client.base.dialog.ExtendedMessageDialogTest.testInfoDialogWithDontShowAgain(ExtendedMessageDialogTest.java:64)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
                at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at java.lang.reflect.Method.invoke(Method.java:498)
                at 
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
                at 
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
                at 
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
                at 
org.eclipse.swtbot.swt.finder.junit.internal.CapturingFrameworkMethod.invokeExplosively(CapturingFrameworkMethod.java:47)
                at 
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
                at 
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
                at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
                at 
org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner.runChild(SWTBotJunit4ClassRunner.java:75)
                at 
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
                at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
                at 
org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
                at 
org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
                at 
org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
                at 
org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
                at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
                at 
org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner.run(SWTBotJunit4ClassRunner.java:60)
                at 
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
                at 
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
                at 
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
                at 
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
                at 
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
                at 
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

What am I doing wrong?

I use Junit4 and the default Junit Runner.

Best Regards
Stefan


_______________________________________________
swtbot-dev mailing list
swtbot-dev@eclipse.org<mailto:swtbot-dev@eclipse.org>
To change your delivery options, retrieve your password, or unsubscribe from 
this list, visit
https://dev.eclipse.org/mailman/listinfo/swtbot-dev
_______________________________________________
swtbot-dev mailing list
swtbot-dev@eclipse.org<mailto:swtbot-dev@eclipse.org>
To change your delivery options, retrieve your password, or unsubscribe from 
this list, visit
https://dev.eclipse.org/mailman/listinfo/swtbot-dev
_______________________________________________
swtbot-dev mailing list
swtbot-dev@eclipse.org
To change your delivery options, retrieve your password, or unsubscribe from 
this list, visit
https://dev.eclipse.org/mailman/listinfo/swtbot-dev

Reply via email to