9

SWTBotを使用して単純な SWT GUI アプリケーションをテストしたいと考えています。残念ながら、始め方がわかりません。Eclipse プラグインのテストについて説明したチュートリアルがいくつかありますが、私の問題に関するものは見つかりませんでした。それが可能かどうかさえわかりません。

4

1 に答える 1

19

まあ、それは非常に可能です。以下の手順に従ってください。

  1. SWT テスト用の SWTBot をダウンロード
  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();
    }
}

ここで、JUnit テスト ケースを作成します (初めての場合はググってください) 。また、SWTBot (ダウンロードしたもの)に存在するすべての jar ファイルを classpath に追加します

最初にディスプレイを作成します (アプリケーションが必要とするため)。また、ウィジェット/コントロールが存在するコンテナーのハンドルも取得します。私の場合、それは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

これが役立つことを願っています。

于 2011-04-20T05:53:00.683 に答える