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. The code requires a java_env.ini file generated from this batch file PrintJavaEnv.bat to be present in the directory where test.py is located.

The batch file is called like this 'PrintJavaEnv.bat SQUISH_INSTALL_DIR' (replace capitalized placeholder with the folder for your setup) and will generate the java_env.ini file in the same directory it is in.

Python

import os
import sys
import configparser

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, java_binary, _java_options = read_information()

    process_env = prepare_java_subprocess_hook_up(squish_for_java_dir, _java_options)

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

    # 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()),
        f"Expected app ctx count: 2; actual: {len(applicationContextList())}"  )


def read_information():
    # Read in the information from the configuration file and the Java used by the squishserver
    info_parser = configparser.ConfigParser()
    if not os.path.exists("java_env.ini"):
        raise IOError("Configuration file not found")
    info_parser.read("java_env.ini")

    if sys.platform == "win32":
        directory = os.path.join(os.getenv("APPDATA"), "froglogic", "Squish", "ver1")
    else:
        directory = os.path.join(os.getenv("HOME"), ".squish", "ver1")
    server_file = os.path.join(directory, "server.ini")
    parser = configparser.ConfigParser(strict=False)
    parser.read(server_file)

    data = (info_parser["General"]["SQUISH_PREFIX"],
             parser["General"]["JavaVM"],
              info_parser["General"]["_JAVA_OPTIONS"])
    return data


def start_java_subprocess(squish_for_java_dir, java_binary, process_env):
    # This is faking the start of a Java application
    # through the current Qt AUT:
    p = QProcess()
    p.setProgram(java_binary)
    p.setProcessEnvironment(process_env);
    java_app = os.path.join(squish_for_java_dir, "examples", "java", "addressbook", "AddressBookSwing.jar")
    p.setArguments(QString(f'-jar   {java_app}').split(QString("   ")))
    p.start()


def prepare_java_subprocess_hook_up(squish_for_java_dir, _java_options):
    path = get_squish_java_path(
        squish_for_java_dir,
        str(os.getenv("PATH")))
    process_environment = QProcessEnvironment.systemEnvironment()
    process_environment.insert("PATH", QString(path))
    process_environment.insert("_JAVA_OPTIONS", QString(_java_options))
    process_environment.insert("QT_PLUGIN_PATH", QString(os.path.join(squish_for_java_dir, "bin", "plugins")))
    process_environment.insert("SQUISH_PREFIX", QString(squish_for_java_dir))

    # Optional, to make it attachable:
    #process_environment.insert("SQUISH_ATTACHABLE_PORT"), "4444")

    return process_environment


def get_squish_java_path(squish_java_path, path):
    squish_bin = os.path.join(squish_java_path, "bin")
    squish_lib = os.path.join(squish_java_path, "lib")
    path = os.pathsep.join([squish_bin, squish_lib, str(path)])
    return path
test.py