Squish tip of the week: How to handle tests requiring user input

Squish tip of the week: How to handle tests requiring user input

While the goal is automating a test from end to end, there are those (hopefully) occasional circumstances where human interaction isn’t avoidable.

How about tests where a password is required?
What if I have to manually configure something as part of a test?

Use Squish’s testInteraction Functions to allow user-input points during playback.

Examples using testInteraction functions

The following example uses a simple Java Swing application available for download from http://www.codejava.net/download-attachment?fid=154, or if you prefer to download directly from our blog: SwingJPasswordFieldDemo

Pause for input

As you can read more about here, testInteraction has many functions available. The example below demonstrates pausing, providing the end user with a message, and an OK button for continuing on line 7.

function main() {
    startApplication("SwingJPasswordFieldDemo.jar");
    clickButton(waitForObject(":Swing JPasswordField Demo Program.OK_JButton"));
    waitFor("object.exists(':Message.Wrong password!_JLabel')", 20000);
    test.compare(findObject(":Message.Wrong password!_JLabel").text, "Wrong password!");
    clickButton(waitForObject(":Message.OK_JButton"));
    testInteraction.information("Please enter the password and click OK on this dialog to continue.");
    clickButton(waitForObject(":Swing JPasswordField Demo Program.OK_JButton"));
    waitFor("object.exists(':Message.Congratulations! You entered correct password._JLabel')", 20000);
    test.compare(findObject(":Message.Congratulations! You entered correct password._JLabel").text, "Congratulations! You entered correct password.");
    clickButton(waitForObject(":Message.OK_JButton"));
}

Use password (input) in script

You can also provide input for use in a script, mask* the input from the screen if it’s a password, however, the data is not masked when used in the script (meaning, it becomes a variable value). See lines 6, 9 and 11. Also common is using testInteraction.input() when the information inputted is not a password (testInteraction.password is new in 6.0).

function main() {
    startApplication("SwingJPasswordFieldDemo.jar");
    clickButton(waitForObject(":Swing JPasswordField Demo Program.OK_JButton"));
    waitFor("object.exists(':Message.Wrong password!_JLabel')", 20000);
    test.compare(findObject(":Message.Wrong password!_JLabel").text, "Wrong password!");
    password = testInteraction.password("Please enter the password");
    clickButton(waitForObject(":Message.OK_JButton"));
    mouseClick(waitForObject(":Swing JPasswordField Demo Program.Enter password:_JPasswordField"), 35, 2, 0, Button.Button1);
    type(waitForObject(":Swing JPasswordField Demo Program.Enter password:_JPasswordField"), password);
    mouseClick(waitForObject(":Swing JPasswordField Demo Program.Confirm password:_JPasswordField"), 40, 3, 0, Button.Button1);
    type(waitForObject(":Swing JPasswordField Demo Program.Confirm password:_JPasswordField"), password);
    clickButton(waitForObject(":Swing JPasswordField Demo Program.OK_JButton"));
    waitFor("object.exists(':Message.OK_JButton')", 20000);
    test.compare(findObject(":Message.OK_JButton").text, "OK");
    waitFor("object.exists(':Message.Congratulations! You entered correct password._JLabel')", 20000);
    test.compare(findObject(":Message.Congratulations! You entered correct password._JLabel").text, "Congratulations! You entered correct password.");
    clickButton(waitForObject(":Message.OK_JButton"));
}

Check for interactive state

You can also create tests which will continue without the manual input, although you’ll want to consider if it’s possible to continue for each scenario. The following scenario allows the script to continue, but it will always fail on the next step, as the manual input is required to proceed. See lines 7 through 12.

function main() {
    startApplication("SwingJPasswordFieldDemo.jar");
    clickButton(waitForObject(":Swing JPasswordField Demo Program.OK_JButton"));
    waitFor("object.exists(':Message.Wrong password!_JLabel')", 20000);
    test.compare(findObject(":Message.Wrong password!_JLabel").text, "Wrong password!");
    clickButton(waitForObject(":Message.OK_JButton"));
    if (testInteraction.isAvailable()){
        testInteraction.information("Please enter the password and click OK on this dialog to continue.");
     }else{
        test.warning("This script requires the --interactive option when run from the command line.");
        test.log("Trying to proceed with test without manual input");
     }
    clickButton(waitForObject(":Swing JPasswordField Demo Program.OK_JButton"));
    waitFor("object.exists(':Message.Congratulations! You entered correct password._JLabel')", 20000);
    test.compare(findObject(":Message.Congratulations! You entered correct password._JLabel").text, "Congratulations! You entered correct password.");
    clickButton(waitForObject(":Message.OK_JButton"));
}

More examples are available here including:

  • testInteraction.question
  • testInteraction.warning


Learn more

froglogic_cropped