使用 SWTBot 测试 SWT GUI

标签 testing swt gui-testing swtbot

我想用 SWTBot 测试一个简单的 SWT GUI 应用程序.不幸的是,我不知道如何开始。有几个教程描述了 Eclipse 插件的测试,但我找不到任何关于我的问题的信息。我什至不知道这是否可能。

最佳答案

嗯,这是很有可能的。按照下面提到的步骤操作。

  1. 下载SWTBot for SWT Testing
  2. 把它放在<eclipsehome>/dropins文件夹
  3. 重启你的 eclipse

此时您已准备好使用 SWTBot

为了演示目的,我为您编写了一个小的登录对话框,它看起来像这样: enter image description here

代码

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class SampleSWTUI 
{

    public Shell showGUI(final Display display)
    {
        Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(3,true));
        shell.setText("Sample SWT UI");

        new Label(shell, SWT.NONE).setText("User Name: ");
        final Text nameText = new Text(shell, SWT.BORDER);
        nameText.setText ("");
        GridData data = new GridData(SWT.FILL, SWT.FILL, true, false);
        data.horizontalSpan = 2;
        nameText.setLayoutData(data);
        
        new Label(shell, SWT.NONE).setText("Password: ");
        final Text passwordText = new Text(shell, SWT.BORDER|SWT.PASSWORD);
        passwordText.setText ("");
        data = new GridData(SWT.FILL, SWT.FILL, true, false);
        data.horizontalSpan = 2;
        passwordText.setLayoutData(data);

        Button loginButton = new Button (shell, SWT.PUSH);
        loginButton.setText ("Login");
        data = new GridData(SWT.FILL, SWT.FILL, true, false);
        data.horizontalSpan = 3;
        loginButton.setLayoutData(data);
        loginButton.addSelectionListener(new SelectionAdapter(){
            public void widgetSelected(SelectionEvent e) 
            {
                String user = nameText.getText();
                String password = passwordText.getText();
                
                System.out.println("\n\n\n");
                if(user.equals("Favonius") && password.equals("abcd123")){
                    System.out.println("Success !!!");
                }else {
                    System.err.println("What the .. !! Anyway it is just a demo !!");
                }                   
            }
        });

        shell.pack();
        shell.open();
        return shell;

    }

    public static void main(String [] args) 
    {
        Display display = new Display();
        Shell shell = new SampleSWTUI().showGUI(display);
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }

        display.dispose();
    }
}

现在创建一个 JUnit 测试用例(如果您不熟悉,请谷歌搜索)。同时添加 SWTBot 中存在的所有 jar 文件(您下载的那个) 在您的类路径中

现在首先创建一个显示(因为应用程序需要一个)。还可以获取您的小部件/控件所在的容器 的句柄。在我的例子中,它是 Shell

SWTBot代码

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.widgets.SWTBotButton;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotText;
import org.junit.Test;


public class SWTBotDemo 
{
    @Test
    public void test() 
    {
        SWTBotPreferences.PLAYBACK_DELAY = 100; // slow down tests...Otherwise we won't see anything
        
        Display display = new Display();
        Shell shell = new SampleSWTUI().showGUI(display);
        SWTBot bot = new SWTBot(shell);
        
        SWTBotButton loginButton = bot.button("Login");
        SWTBotText userText = bot.textWithLabel("User Name: ");
        SWTBotText passwordText = bot.textWithLabel("Password: ");
        
        userText.setFocus();
        userText.setText("Superman");
        
        assert(userText.getText().equals("Superman"));
        
        passwordText.setFocus();
        passwordText.setText("test123");
        
        assert(userText.getText().equals("test123"));
        
        loginButton.setFocus();
        loginButton.click();    
        
        
        userText.setFocus();
        userText.setText("Favonius");
        
        assert(userText.getText().equals("Favonius"));
        
        passwordText.setFocus();
        passwordText.setText("abcd123");
        
        assert(userText.getText().equals("abcd123"));
        
        loginButton.setFocus();
        loginButton.click();    

        while (!shell.isDisposed()) {
             if (!display.readAndDispatch()) display.sleep();
        }

        display.dispose();
    }
}

现在,所有 SWTBot 方法和变量都在源代码中得到了很好的定义,并且源代码被捆绑在 SWTBot jar 中。因此,您始终可以继续破解其源代码。

进一步阅读

  1. http://wiki.eclipse.org/SWTBot/FAQ
  2. http://wiki.eclipse.org/SWTBot/UsersGuide

希望这会有所帮助。

关于使用 SWTBot 测试 SWT GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5716174/

相关文章:

Oracle 数据库在负载测试期间收集指标

css - Cypress 测试伪 CSS 类 :before

ruby-on-rails - rails capybara - 如何检测 js 点击 li(不是链接或按钮)

java - SWTbot - 如何构建项目

java - 如何使用 SWT 应用程序发送 XulRunner?

ruby-on-rails - 如何获取 rspec 的 ENV 变量集?

java - 我们如何在swt中添加菜单到标签

java - 将鼠标从当前位置移动到 JComponent 位置

android - 机器人安装

functional-testing - 为什么编码的 UI 测试很重要?