Hooking up Java sub-processes started by Qt AUTs

Last edited on

Overview

The following shows how to alter the Qt process’ environment to hook up Java sub-processes started afterwards.

Python

import os.path

def main():
    # Lets use Squish for Qt's addressbook example as AUT:
    ctx_qt = startApplication("addressbook")

    # Must be provided by the user:
    squish_for_java_dir = "C:/Users/myuser/squish-6.0.2-java"

    undo_info = prepare_java_subprocess_hook_up(squish_for_java_dir)



    # Do something in Qt AUT that starts a Java sub-process:
    start_java_subprocess(squish_for_java_dir)



    # Undo the changes to the process environment:
    undo_prepare_java_subprocess_hook_up(undo_info)

    # Synchronize for the hook up of the/a new
    # (sub)process:
    waitForApplicationLaunch()

    # Verify that a second AUT is hooked up now:
    test.compare(
        2,
        len(applicationContextList()),
        "Expected app ctx count: 2; actual: %s" % len(applicationContextList()))

def start_java_subprocess(squish_for_java_dir):
    # This is faking the start of a Java application
    # through the current Qt AUT:
    p = QProcess()
    p.setProgram("C:/Program Files (x86)/Java/jre/bin/java")
    p.setArguments(QString("-jar   " + squish_for_java_dir + "/examples/java/addressbook/AddressBookSwing.jar").split(QString("   ")))
    p.start()

def prepare_java_subprocess_hook_up(squish_for_java_dir):
    path_env_var_backup = qgetenv("PATH")
    path, _java_options = get_squish_java_path_and__java_options(
        squish_for_java_dir,
        str(qgetenv("PATH")),
        os.path.expandvars("$TEMP"))
    qputenv("PATH", path)
    qputenv("_JAVA_OPTIONS", _java_options)

    # Optional, to make it attachable:
    #qputenv("SQUISH_ATTACHABLE_PORT"), "4444")

    return path_env_var_backup

def undo_prepare_java_subprocess_hook_up(path_env_var_backup):
    # Restore PATH:
    qputenv("PATH", path_env_var_backup)

    # Invalidate _JAVA_OPTIONS:
    qputenv("_JAVA_OPTIONS", "")

def get_squish_java_path_and__java_options(squish_java_path, path, user_temp_path):
    squish_bin = os.path.join(squish_java_path, "bin")
    squish_lib = os.path.join(squish_java_path, "lib")
    path = squish_bin + os.pathsep + squish_lib + os.pathsep + str(path)
    _java_options =  '"-Xbootclasspath/p:%(squish_java_path)s/lib/squishrt.jar"'
    _java_options += ' "-Dsquish.wrapper=%(squish_java_path)s/bin/squishjavawrapper.dll"'
    _java_options += ' "-javaagent:%(squish_java_path)s/lib/squishagent.jar=%(squish_java_path)s/lib"'
    _java_options += ' "-Xverify:none"'
    _java_options += ' "-Dsquish.tmpdir=%(user_temp_path)s/frogtemp"'
    _java_options += ' "-DSQUISH_LOG_HOOKUP=1"'
    _java_options += ' "-Dsquish.bcel=%(squish_java_path)s/lib/bcel.jar"'
    _java_options += ' "-Djava.security.policy=file:%(user_temp_path)s/frogtemp/squish.policy"'
    _java_options = _java_options % {"squish_java_path": squish_java_path, "user_temp_path": user_temp_path}
    return path, _java_options
test.py