使用SWTBot测试SWT GUI

sch*_*pps 8 testing swt gui-testing swtbot

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

Fav*_*ius 17

嗯,这是非常可能的.按照下面提到的步骤操作.

  1. 下载SWTBot进行SWT测试
  2. 把它放在<eclipsehome>/dropins文件夹中
  3. 重启你的日蚀

现在,您已准备好使用SWTBot.

为了演示目的,我为您编写了一个小的"登录"对话框,它看起来像这样: 在此输入图像描述

码

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

现在创建一个JUnit测试用例(如果你是新手,请谷歌google).还要在类路径中添加 SWTBot中存在的所有jar文件(已下载的文件) .

现在首先创建一个显示(因为应用程序需要一个).还要获取存在小部件/控件的容器句柄.就我而言,它是壳牌.

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,所有SWTBot方法和变量都在源中得到很好的定义,并且源被捆绑在SWTBot jar中.所以你总是可以继续破解它的源代码.

进一步阅读

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

希望这会有所帮助.