1

使用 SWTBot 测试 SWT 应用程序:

我遇到了一个需求,我需要使用 SWTBot 测试一个 SWT 应用程序。我不知道如何开始使用 SWTBot,在参考了一些博客之后,我可以使用 eclipse 设置 SWTBot。而且我发现大多数教程都描述了 Eclipse 插件的测试,但是我找不到任何关于我的需求的东西(比如测试第三方 SWT 应用程序)。我什至不知道这是否可能。

我的要求 -我有一个 JNLP 文件,运行此 JNLP 文件后,SWT 应用程序将打开。一旦应用程序打开,我必须使用用户 ID 和密码登录到应用程序。我正在尝试使用下面的代码来消除异常“WidgetNotFoundException”。

代码

import java.io.IOException;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swtbot.swt.finder.SWTBot;
import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
import org.eclipse.swtbot.swt.finder.waits.Conditions;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotText;
import org.junit.Test;

public class TestLogin {
    @Test
    public void verifyLogin() throws IOException, InterruptedException {

        // Set the timeout to 6 seconds
        SWTBotPreferences.TIMEOUT = 6000;

        //Launch the SWT applicaton.
        Runtime.getRuntime().exec("javaws C:\\Users\\anand\\Downloads\\Testing.jnlp");
        Thread.sleep(60000);

        Display display = new Display();
        Shell shell = new Shell(display); 
        SWTBot bot = new SWTBot(shell);

        //"User Authentication" is the name of the login window
        bot.waitUntil(Conditions.shellIsActive("User Authentication")); 
        bot.shell("User Authentication").activate(); 

        bot.activeShell();
        bot.textWithLabel("User ID:").setText("anand");
        bot.textWithLabel("Password:").setText("anand123");
        bot.button("Login").click();

        /*       
        SWTBotText userID = bot.textWithLabel("User ID:");
        SWTBotText passwordText = bot.textWithLabel("Password:");
        SWTBotButton loginButton = bot.button("Login");

        userID.setFocus();
        userID.setText("Superman");
        //assert(userID.getText().equals("Superman"));

        passwordText.setFocus();
        passwordText.setText("test123");
        //assert(userID.getText().equals("test123"));

        loginButton.setFocus();
        loginButton.click();   
        */
    }
}

输出:

org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException: Could not find shell matching: with text 'User Authentication'
    at org.eclipse.swtbot.swt.finder.SWTBotFactory.waitUntilWidgetAppears(SWTBotFactory.java:472)
    at org.eclipse.swtbot.swt.finder.SWTBotFactory.shells(SWTBotFactory.java:116)
    at org.eclipse.swtbot.swt.finder.SWTBotFactory.shell(SWTBotFactory.java:106)
    at org.eclipse.swtbot.swt.finder.SWTBotFactory.shell(SWTBotFactory.java:97)
    at com.fis.teller.testscripts.TestLogin.verifyLogin(TestLogin.java:27)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.eclipse.swtbot.swt.finder.widgets.TimeoutException: Timeout after: 6000 ms.: Could not find shell matching: with text 'User Authentication'
    at org.eclipse.swtbot.swt.finder.SWTBotFactory.waitUntil(SWTBotFactory.java:522)
    at org.eclipse.swtbot.swt.finder.SWTBotFactory.waitUntil(SWTBotFactory.java:496)
    at org.eclipse.swtbot.swt.finder.SWTBotFactory.waitUntil(SWTBotFactory.java:484)
    at org.eclipse.swtbot.swt.finder.SWTBotFactory.waitUntilWidgetAppears(SWTBotFactory.java:466)
    ... 27 more

我认为我无法将控件转移到“用户身份验证”外壳/小部件/窗口。

4

1 回答 1

0

是不是因为外壳还没有出现,所以等待它激活会失败?

对我来说,遍历所有视图,然后按标题匹配它们效果更好,即使它不是最干净的编码。您可以有一个单独的匹配器 ICondition 类,它将首先检查外壳是否存在,然后检查它是否处于活动状态,然后才报告匹配。

于 2019-09-18T11:07:15.867 回答