SWTbot 测试SWT程序

这个程序借鉴的网上的一个程序一样。

 

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);
//        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();
    }
}

 

 

 

public class SWTBotDemo {
    static SWTBotButton loginButton;
    static SWTBotText userText;
    static SWTBotText passwordText;
    static SWTBot bot;
    static Display display;
    static Shell shell;
    
    @BeforeClass
    public static void beforeClass() throws Exception {
        SWTBotPreferences.PLAYBACK_DELAY = 1000;
        
        display = Display.getDefault();
        shell = new SampleSWTUI().showGUI(display);
        bot = new SWTBot(shell);
        loginButton = bot.button("Login");
        userText = bot.textWithLabel("User Name: ");
        passwordText = bot.textWithLabel("Password: ");
    }
    
    @Test
    public void testLogin() {
        bot.sleep(1000);

        userText.setFocus();
        userText.setText("aaaaaaaa");
        passwordText.setFocus();
        passwordText.setText("11111111");
        userText.setFocus();
        userText.setText("bbbbbbbb");
        passwordText.setFocus();
        passwordText.setText("2222222");
        userText.setFocus();
        userText.setText("ccccccc");

        passwordText.setFocus();
        passwordText.setText("3333333");
        bot.sleep(1000);

        loginButton.setFocus();
        loginButton.click();   
        bot.sleep(1000);

        userText.setFocus();
        userText.setText("Favonius");
        bot.sleep(1000);


        passwordText.setFocus();
        passwordText.setText("abcd123");
        bot.sleep(1000);

        assert(userText.getText().equals("abcd123"));

        loginButton.setFocus();
        loginButton.click();    
        bot.sleep(3000);

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

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值