Semi automatic testing

Last edited on

Overview

The following solution is based on the open source project easygui.

Installation

Please download and unzip easygui and put the file [easygui.py](/squish/howto/semi-automatic-testing//easygui.py) in your test suite directory.

Then create a file [choicebox.py](/squish/howto/semi-automatic-testing//choicebox.py) with the following content in the test suite directory:

import os
import subprocess
import sys
import easygui

def remove_squish_paths_and_run_again(squish_prefix):
    """Squish's Python may not have Tkinter, so remove Squish's
    path entries from the PATH environment variable, and then run
    this complete command again as another process. That way any
    other Python installation (if any) should be used to run this
    script. (And "proper" Python installations typically have
    Tkinter.)"""

    # Get PATH
    path_env = os.environ["PATH"]
    # Split PATH to get paths
    paths = path_env.split(os.pathsep)
    # Loop over paths and concatenate the ones that do not start with SQUISH_PREFIX
    new_path = ""
    for p in paths:
        if p.startswith(squish_prefix):
            print "Dropping Squish path from PATH:", p
        else:
            new_path += os.pathsep + p
    # Remove os.path.separator prefix
    new_path = new_path[len(os.pathsep):]
    # Put PATH
    os.putenv("PATH", new_path)

    # Execute original command
    args = sys.argv
    args.insert(0, "python")
    proc = subprocess.Popen(args=args, shell=True)
    proc.communicate()
    sys.exit(proc.returncode)

def choicebox():
    args = sys.argv[1:]
    msg = args[0]
    args = args[1:]
    title = args[0]
    args = args[1:]
    choice = easygui.choicebox(msg=msg, title=title, choices=args)
    if choice:
        print choice

def has_tkinter():
    """Squish's included Python interpreter may not have Tkinter,
    allow to check for this"""

    try:
        import Tkinter
        return True
    except:
        return False

if __name__ == "__main__":
    n = "SQUISH_PREFIX"
    if has_tkinter() or not n in os.environ:
        choicebox()
    else:
        squish_prefix = os.environ[n]
        os.unsetenv(n)
        remove_squish_paths_and_run_again(squish_prefix)
choicebox.py

Using the choicebox.py script from JavaScript

To use the above Python script from inside your test script use something like the choicebox function in this example:

function main() {
    var c = choicebox("Message", "Title", ["Choice 1", "Choice 2"]);
    test.log(c);
}

function choicebox(msg, title, choicesArray) {
    var cmd = 'python "' + squishinfo.testCase + '/../choicebox.py"';
    cmd += ' "' + msg + '"';
    cmd += ' "' + title + '"';
    for (var i = 0; i < choicesArray.length; i++) {
        cmd += ' "' + choicesArray[i] + '"';
    }

    var s = OS.capture(cmd);

    // Return first line, on all platforms
    s = s.replace(/\r\n/g, "\n");
    s = s.replace(/\r/g, "\n");
    return s.split("\n")[0];
}
test.js

Using the choicebox.py script from Python

To use the above Python script from inside your test script use something like the choicebox function in this example:

import subprocess

def main():
    c = choicebox("Message", "Title", ["Choice 1", "Choice 2"])
    test.log(c)

def choicebox(msg, title, choices_array):
    cmd = ["python"]
    cmd.append(squishinfo.testCase + "/../choicebox.py")
    cmd.append(msg)
    cmd.append(title)
    for c in choices_array:
        cmd.append(c)

    s = os_capture(cmd)

    # Return first line, on all platforms
    s = s.replace("\r\n", "\n")
    s = s.replace("\r", "\n")
    return s.split("\n")[0]

def os_capture(cmd):
    return subprocess.Popen(
        args=cmd,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT).communicate()[0]
test.py