0

Am using the following code to create a dialog for my login page. I need to write SWTBot test cases for the dialog that I create.

I have written SWTBot for windows but not for jface dialog.

How to get the access of the dialog in the test class by using the active shell? so that it can detect the buttons or the fields in the dialog.

Besides using active shell, is there any other method to access the dialog that I have created?

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;
    }
}
HareshKannan
  • 83
  • 11

1 Answers1

0

You have to first invoke login dialog using some action assigned to it. Then you can perform different operations like setting text in userName and password text boxes, clicking OK button etc using SWTWorkbenchBot methods.

If you are trying to test login controls on splash screen, then it will not be posible using SWTBot. Because SplashHandler does not get loaded when called from SWTBot. The only way to launch your application in that case is to bypass your splash screen programatically. It means you have to write a code to skip login dialog/splash login screen.

Pooja
  • 41
  • 7
  • I have created an **object** for the above _PasswordDialog_ class. The object is created in the post-construct method of my application. So before the application is loaded, my login screen loads. Now in my SWTBot test case, the buttons of my login dialog are not recognised, but the buttons in my application window are recognised. – HareshKannan Mar 02 '15 at 07:03