Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
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.noebauer@xxxxxxxxxxxxxxxxxx> 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@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/swtbot-dev

Back to the top