【问题标题】:Testing SWT GUI with SWTBot使用 SWTBot 测试 SWT GUI
【发布时间】:2011-08-08 15:21:35
【问题描述】:

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

【问题讨论】:

    标签: testing swt gui-testing swtbot


    【解决方案1】:

    嗯,很有可能。请按照以下步骤操作。

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

    现在您已经准备好使用 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();
        }
    }
    

    现在创建一个 JUnit 测试用例(如果您是新手,请用 Google 搜索)。还添加所有存在于 SWTBot 中的 jar 文件(您已下载的那个)在您的类路径中

    现在首先创建一个显示(因为应用程序需要一个)。还要获取您的小部件/控件所在的 container 的句柄。在我的例子中,它是 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

    希望这会有所帮助。

    【讨论】:

    • 您好,favonius,感谢您提供本教程!我做了和你说的一样的,它有效。现在我尝试将它用于我自己的应用程序。
    • 您好,favonius,当我这样做时,我只有一个新的 Eclipse 窗口可以打开和关闭。我是否应该在 Run Config 中设置其他内容以针对登录窗口而不是 Eclipse?谢谢
    • 嗨@zylootino:这篇文章已经发布了将近一年,我不再接触SWT 或eclipse 开发。我建议你在这里和那里调整你的代码,看看。如果它仍然不起作用,那么将其作为问题发布在 SO 上。 SO上的某个人肯定会回答这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    • 2014-10-19
    相关资源
    最近更新 更多