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.

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 behavior allows for easy synchronization with the external application, but may not always be desired.

The examples below demonstrate synchronous and asynchronous execution (where applicable).

Local and remote execution via RemoteSystem :

JavaScript (Unix)

source(findFile('scripts', 'javascript/remotesystem.js'));

function main()
{
    let rs = new RemoteSystem();


    // Synchronous execution:
    let result = rs.execute(["ping", "-c", "5", "localhost"]);


    // Asynchronous execution; on Unix
    // this requires redirecting the output:
    let result2 = rs.execute(["sh", "-c", 'ping -c 5 localhost >/dev/null 2>&1 &']);
}

JavaScript (Windows)

Local and remote execution via RemoteSystem :

source(findFile('scripts', 'javascript/remotesystem.js'));

function main()
{
    let rs = new RemoteSystem();


    // Synchronous execution:
    let result = rs.execute(["ping.exe", "-n", "5", "localhost"]);


    // Asynchronous execution:
    let result2 = rs.execute(["cmd.exe", "/c", "start", "ping.exe", "-n", "5", "localhost"]);


    // Asynchronous execution, but minimize
    // associated console window:
    let result3 = rs.execute(["cmd.exe", "/s", "/c", "start", "", "/min", "ping.exe", "-n", "5", "localhost"]);
}

Python (Unix)

Local and remote execution via RemoteSystem :

from remotesystem import RemoteSystem

def main():
    rs = RemoteSystem()


    # Synchronous execution:
    result = rs.execute(["ping", "-c", "5", "localhost"])


    # Asynchronous execution; on Unix
    # this requires redirecting the output:
    result2 = rs.execute(["sh", "-c", 'ping -c 5 localhost >/dev/null 2>&1 &'])

Python (Windows)

Local and remote execution via RemoteSystem :

from remotesystem import RemoteSystem

def main():
    rs = RemoteSystem()


    // Synchronous execution:
    result = rs.execute(["ping.exe", "-n", "5", "localhost"])


    // Asynchronous execution:
    result2 = rs.execute(["cmd.exe", "/c", "start", "ping.exe", "-n", "5", "localhost"])


    # Asynchronous execution, but minimize
    # associated console window:
    result3 = rs.execute(["cmd.exe", "/s", "/c", "start", "", "/min", "ping.exe", "-n", "5", "localhost"])

Using Functionality provided by 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

OS.system() (Unix)

Local execution (same computer where Squish IDE and squishrunner are running) via OS.system() :

function main()
{
    // Synchronous execution:
    let exitcode = OS.system('ping -c 5 localhost');


    // Asynchronous execution; on Unix
    // this requires redirecting the output:
    let exitcode2 = OS.system('ping -c 5 localhost >/dev/null 2>&1 &');
}

OS.system() (Windows)

Local execution (same computer where Squish IDE and squishrunner are running) via OS.system() :

function main()
{
    // Synchronous execution:
    let exitcode = OS.system('ping.exe -n 5 localhost');


    // Asynchronous execution:
    let exitcode2 = OS.system('start ping.exe -n 5 localhost');


    // Asynchronous execution, but minimize
    // associated console window:
    let exitcode3 = OS.system('start /min ping.exe -n 5 localhost');
}

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 subprocess 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

os.system() (Unix)

Local execution (same computer where Squish IDE and squishrunner are running) via os.system() :

import os

def main():
    # Synchronous execution:
    exitcode = os.system('ping -c 5 localhost')


    # Asynchronous execution:
    exitcode2 = os.system('ping -c 5 localhost &')

os.system() (Windows)

Local execution (same computer where Squish IDE and squishrunner are running) via os.system() :

import os

def main():
    # Synchronous execution:
    exitcode = os.system('ping.exe -n 5 localhost')


    # Asynchronous execution:
    exitcode2 = os.system('start ping.exe -n 5 localhost')


    # Asynchronous execution, but minimize
    # associated console window:
    exitcode3 = os.system('start /min ping.exe -n 5 localhost')

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 exec and open functions.