Executing external applications

Last edited on

Executing external applications

The RemoteSystem Object API provided by Squish can be used in test scripts to execute external applications, access files and directories, etc. locally as well as remotely.

Executing external applications asynchronously, non-blocking, in the background

Commands for executing external applications are typically executing the desired application synchronously, which means that Squish test script execution will not proceed beyond that command until the external application has exited again.

This default behaviour allows for easy synchronization with the external application, but may not always be desired.

To start an application with, say, OS.system() in the background, there are different approaches that can be used on Windows and Unix.

On Windows:

function main()
{
    // Asynchronously start notepad:
    OS.system('start "" notepad.exe');
}

On Unix:

function main()
{
    // Asynchronously start gedit:
    OS.system('gedit &');
}

(The use of start and the & character are the “standard” ways to start applications asynchronously on Windows and Unix respectively.)

Using functionality built into the scripting languages

All of the following mechanisms are being provided by the respective the scripting languages and the external process is always being always on the computer where the Squish IDE or squishrunner are being executed.

JavaScript

Since squishrunner is executing the test scripts, the following mechanism will execute external applications on the same computer where squishrunner is being executed.

The Squish JavaScript interpreter provides the OS.capture() and OS.system() functions.

function main():
    var stdout = OS.capture("mycommand");
    test.verify(stdout.indexOf("some expected substring") != -1, "Expected to find: 'some expected substring'");
test.js

Perl

Since squishrunner is executing the test scripts, the following mechanism will execute external applications on the same computer where squishrunner is being executed.

Perl provides the system() function.

Python

Since squishrunner is executing the test scripts, the following mechanism will execute external applications on the same computer where squishrunner is being executed.

Python provides the os.system() function, and also the <code>subprocess</code> module .

import os
import subprocess

def main():
    stdout, exit_code = os_capture("mycommand")
    test.compare(exit_code, 0, "Expected exit code: 0")
    test.verify(stdout.find("some expected substring") != -1, "Expected to find: 'some expected substring'")

def os_capture(args, cwd=None):
    if cwd is None:
        cwd = os.getcwd()
    proc = subprocess.Popen(
        args=args,
        cwd=cwd,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT)
    stdout = proc.communicate()[0]
 
    return stdout, proc.returncode
test.py

Ruby

Since squishrunner is executing the test scripts, the following mechanism will execute external applications on the same computer where squishrunner is being executed.

Ruby provides multiple mechanisms ( 5 ways to run commands from Ruby ), and the simplest is the one based on backticks:

s = `dir`

Tcl

Since squishrunner is executing the test scripts, the following mechanism will execute external applications on the same computer where squishrunner is being executed.

Tcl provides the <code>exec</code> and <code>open</code> functions.