使用SWTBot测试SWT GUI [英] Testing SWT GUI with SWTBot

查看:125
本文介绍了使用SWTBot测试SWT GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to test a simple SWT GUI application with SWTBot. Unfortunatly, I have no idea how to start. There are several tutorials which describe the testing of an Eclipse plug-in but I could not find anything regarding my problem. I don't even know if it is possible.

推荐答案

很有可能.请按照以下步骤操作.

Well it is very much possible. Follow the step as mentioned below.

  1. 下载用于SWT测试的SWTBot
  2. 将其放入<eclipsehome>/dropins文件夹
  3. 重新开始日食
  1. Download SWTBot for SWT Testing
  2. Put it in the <eclipsehome>/dropins folder
  3. Restart your eclipse

现在,您可以开始使用 SWTBot 了.

Now at this point you are ready to play with SWTBot.

出于演示目的,我为您编写了一个小的登录"对话框,它看起来像这样:

For the demo purpose I have written a small Login dialog for you and it will look like this:

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测试用例(如果您不熟悉它,请用Google创建它).还要添加SWTBot (已下载的文件) 存在于您的类路径中的所有jar文件 .

Now create a JUnit test case (google for it if you are new to it) . Also add all the jar files present in SWTBot (the one you have downloaded) in your classpath.

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

Now first create a display (because application needs one). Also get the handle of container in which your widgets/controls are present. In my case it is the Shell.

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罐中.因此,您始终可以继续操作并破解其源代码.

Now all the SWTBot methods and variables are well defined in the source and the source is bundled within the SWTBot jars. So you can always go ahead and hack its source code.

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

希望这会有所帮助.

这篇关于使用SWTBot测试SWT GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆