0

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

我已经为 windows 编写了 SWTBot,但没有为 jface 对话框编写。

如何使用活动外壳获取测试类中对话框的访问权限?以便它可以检测对话框中的按钮或字段。

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

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;
    }
}
4

1 回答 1

0

您必须首先使用分配给它的一些操作来调用登录对话框。然后您可以使用SWTWorkbenchBot方法执行不同的操作,例如在用户名和密码文本框中设置文本、单击确定按钮等。

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

于 2015-02-24T13:40:29.103 回答