Eclipse plugin 使用SWTBot测试SWT应用程序:

Eclipse plugin 使用SWTBot测试SWT应用程序:,eclipse-plugin,swt,gui-testing,swtbot,swt-awt,Eclipse Plugin,Swt,Gui Testing,Swtbot,Swt Awt,使用SWTBot测试SWT应用程序: 我遇到了一个需求,我需要使用SWTBot测试SWT应用程序。我不知道如何开始使用SWTBot,在参考了一些博客之后,我可以使用eclipse设置SWTBot。我还发现,大多数教程都描述了Eclipse插件的测试,但我找不到任何与我的需求相关的内容(比如测试第三方SWT应用程序)。我甚至不知道这是否可能 我的要求-我有一个JNLP文件,运行此JNLP文件后,SWT应用程序将打开。一旦应用程序打开,我必须使用UserID和Pwd登录到应用程序。我正在使用下面的代

使用SWTBot测试SWT应用程序:

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

我的要求-我有一个JNLP文件,运行此JNLP文件后,SWT应用程序将打开。一旦应用程序打开,我必须使用UserID和Pwd登录到应用程序。我正在使用下面的代码进行尝试,但出现了一个异常“WidgetNotFoundException”

代码

输出:


我想我无法将控件转移到“用户身份验证”shell/Widget/window。

是否可能是因为shell甚至还不存在,因此等待它激活将失败


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

是否可能是因为外壳尚未出现,因此等待它激活将失败

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

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