SWTBot用于Jface对话框

时间:2015-02-19 04:47:48

标签: dialog jface swtbot

使用以下代码为我的登录页面创建对话框。我需要为我创建的对话框编写SWTBot测试用例。

我为Windows编写了SWTBot,但没有编写jface对话框。

如何使用活动shell访问测试类中的对话框?这样它就可以检测对话框中的按钮或字段。

除了使用活动shell之外,还有其他方法可以访问我创建的对话框吗?

package com.login.model;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.GridData;

import com.database.*;
import com.login.controller.UserValidation;

public class PasswordDialog extends Dialog {
    private Text txtUser;
    private Text txtPassword;
    private String user = "";
    private String password = "";
    protected Composite container;
    public Button ok;
    public Button cancel;
    public PasswordDialog(Shell parentShell) {
        super(parentShell);
    }

    @Override
    protected void setShellStyle(int arg0) {
        // Use the following not to show the default close X button in the title
        // bar
        super.setShellStyle(SWT.TITLE);
    }

    @Override
    protected Control createDialogArea(Composite parent) {
        container = (Composite) super.createDialogArea(parent);
        org.eclipse.swt.graphics.Color color = new org.eclipse.swt.graphics.Color(
                container.getDisplay(), 255, 255, 255);
        container.setBackground(color);
        System.out.println(container);
        GridLayout layout = new GridLayout(2, false);
        layout.marginRight = 5;
        layout.marginLeft = 10;
        container.setLayout(layout);

        // Grid for labels user and password
        GridData gdForLabel = new GridData(SWT.LEFT, SWT.CENTER, false, false,
                10, 5);
        gdForLabel.horizontalIndent = 84;

        // Grid for text box user and password
        GridData gdForText = new GridData(SWT.CENTER, SWT.NONE, true, true, 10,
                5);
        gdForText.horizontalIndent = -30;
        gdForText.minimumWidth = 200;

        // To display the application image
        Image image = new Image(container.getDisplay(), new ImageData(
                "C:\\Users\\myName\\workspace\\Login\\Image\\c.gif"));
        Label lblImg = new Label(container, SWT.NONE);
        lblImg.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, true,
                10, 10));
        lblImg.setImage(image);
        lblImg.setBackground(color);

        // The username label
        Label lblUser = new Label(container, SWT.NONE);
        lblUser.setLayoutData(gdForLabel);
        lblUser.setFont(new Font(container.getDisplay(), new FontData("Corbel",
                12, SWT.NORMAL)));
        lblUser.setText("Username");
        lblUser.setBackground(color);

        // The username text box
        txtUser = new Text(container, SWT.BORDER);
        txtUser.setFont(new Font(container.getDisplay(), new FontData("Corbel",
                12, SWT.NORMAL)));
        txtUser.setLayoutData(gdForText);
        txtUser.setText(user);

        // The password label
        Label lblPassword = new Label(container, SWT.NONE);
        lblPassword.setLayoutData(gdForLabel);
        lblPassword.setFont(new Font(container.getDisplay(), new FontData(
                "Corbel", 12, SWT.NORMAL)));
        lblPassword.setText("Password");
        lblPassword.setBackground(color);

        // The password text box
        txtPassword = new Text(container, SWT.BORDER | SWT.PASSWORD);
        txtPassword.setFont(new Font(container.getDisplay(), new FontData(
                "Corbel", 12, SWT.NORMAL)));
        txtPassword.setLayoutData(gdForText);
        txtPassword.setText(password);

        return container;
    }

    @Override
    protected Point getInitialSize() {
        // Initialise window size
        return new Point(400, 400);
    }

    @Override
    protected void createButtonsForButtonBar(Composite parent) {
        // Set Color of Background to white
        org.eclipse.swt.graphics.Color color = new org.eclipse.swt.graphics.Color(
                container.getDisplay(), 255, 255, 255);

        // Change parent layout data to fill the whole bar
        parent.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        parent.setBackground(color);

        // Create a spacer label
        Label spacer = new Label(parent, SWT.NONE);
        spacer.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        spacer.setBackground(color);

        // Update layout of the parent composite to count the spacer
        GridLayout layout = (GridLayout) parent.getLayout();
        layout.numColumns += 16;
        layout.makeColumnsEqualWidth = false;

        // The button creation
        ok=createButton(parent, IDialogConstants.OK_ID, "OK", true);
        cancel=createButton(parent, IDialogConstants.CANCEL_ID, "Cancel", false);
    }

    @Override
    protected void okPressed() {
        // Method to invoke connection to database and authenticate
        user = txtUser.getText();
        password = txtPassword.getText();
        DataBaseConnectivity.createConnection();
        boolean valid = UserValidation.validateString(user);
        if(valid){
            boolean auth = DataBaseConnectivity.authenticate(user, password);
        // Password authentication
            if (auth)
                super.okPressed();
            else {
                Label rt = new Label(container, SWT.CENTER);
                rt.setSize(200, 150);
                rt.setLocation(100, 295);
                org.eclipse.swt.graphics.Color color = new org.eclipse.swt.graphics.Color(
                        container.getDisplay(), 255, 0, 0);
                rt.setForeground(color);
                color = new org.eclipse.swt.graphics.Color(container.getDisplay(),
                        255, 255, 255);
                rt.setBackground(color);
                rt.setText("Username or Password is wrong");
                System.out.println(rt.getText());
            }
        }
        else{
            Label rt = new Label(container, SWT.CENTER);
            rt.setSize(200, 150);
            rt.setLocation(100, 295);
            org.eclipse.swt.graphics.Color color = new org.eclipse.swt.graphics.Color(
                    container.getDisplay(), 255, 0, 0);
            rt.setForeground(color);
            color = new org.eclipse.swt.graphics.Color(container.getDisplay(),
                    255, 255, 255);
            rt.setBackground(color);
            rt.setText("Username is invalid");
            System.out.println(rt.getText());
        }
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

1 个答案:

答案 0 :(得分:0)

您必须首先使用分配给它的某个操作来调用登录对话框。然后,您可以执行不同的操作,例如在userName和password文本框中设置文本,使用 SWTWorkbenchBot 方法单击“确定”按钮等。

如果您尝试在启动画面上测试登录控件,那么使用SWTBot将无法进行测试。因为从SWTBot调用时不会加载SplashHandler。 在这种情况下启动应用程序的唯一方法是以编程方式绕过启动屏幕。这意味着您必须编写代码以跳过登录对话框/启动登录屏幕。