ATS : Interacting with external process

Introduction

Imagine you want to run some external process, but you expect to be asked some questions and you must provide the answers.

In order to interact with an external process you will have to send commands to its input stream and read data from its output streams.


Starting the external process to interact with

You have to use one of the constructors of the ProcessTalker class:

import com.axway.ats.action.processtalk.ProcessTalker;

ProcessTalker talker = new ProcessTalker( "My external command", 10 );

In the provided example, the first argument is the command which you will run.

The second argument is the default time to wait(in seconds) for some operations to happen or fail. These operations usually either send some command to the external process or expect some output from the external process.

You can work in the same way with a process on a remote host as long as you have a running ATS agent there.
All you need to change in your code is to add the ATS agent address as a first parameter in your constructor:

ProcessTalker talker = new ProcessTalker( <ATS agent address>, "My external command", 10 );



Wait for some output

In order to understand what the external process is doing, you need to monitor its output. This is done with some of the expect like methods. Here are some examples:

// wait until the following pattern is outputted by the external process
talker.expect( "Type your choice and press enter:" );

// same as the above example, but the provided pattern is treated as java regular expression
talker.expectByRegex( "Type your choice and press enter:" );

// java regular expression which is spread on 2 different lines
talker.expectByRegex( "line1\r\nline2" );


Output streams note

Processes have at least two different kinds of output

Technically they are referred as "standard output" and "standard error" (more info). The first is used for information and debug output whereas the second is used for error messages. As both streams usually produce output in one place (console) there is no easy way to determine which message is an error and which is standard output. To separate them you may separate the output this way:

$ my_executable >stdout.txt 2>stderr.txt

  • ProcessTalker's expectABC() methods are used to validate standard output messages whereas
  • expectErrABC() methods are used to validate standard error output


Expect some more complex output

If needed, you can wait for many patterns at once:

// make a list with patterns to match
String[] patterns = new String[]{"line1", "line3" };

// the following will succeed if all the patterns are matched in the given order
talker.expectAll( patterns );

If you want to use regular expressions, use the expectAllByRegex method



Send command to the external process

Here is how you can answer to some question:

// first wait to be asked for your choice
talker.expect( "Type your choice and press enter:" );

// now send your choice
talker.send( "1" );

// and send ENTER
talker.sendEnterKey();

Send ENTER key many times

There is an easy to use method which is appropriate for example for reading a user license agreement or something similar. In such special case the user is guided through many pages and basically needs to press ENTER key until pass all the pages

talker.sendEnterKeyInLoop( "Press ENTER to continue:",
                           "Press ENTER if you agree to this license agreement, press ESC to exit:",
                           10 );

In this example we state that we expect on each page to get the first pattern, when we find it, we will press ENTER - this is the first method argument called intermediate pattern. The second method argument is the one that tells us we reached the last page and is called final pattern. The last argument is the max number of time we will send ENTER.

This just a helper method, you can do all this by calling multiple times methods starting with expect... and send...



Get the current standard output

talker.getCurrentStandardOutContents();



Get the current error output

talker.getCurrentStandardErrContents();



Check if the current process has already exited.

talker.isClosed();



How do we know if some operation has failed

If the operation passed, nothing special happens. There will be only some log messages.

If the operation fail, an exception is thrown.



Timeout considerations

All methods starting with expect... have a versions with and without operation timeout. If timeout is not provided, we use the default timeout as provided in the ProcessTalker constructor.

It is always smart to provide a default timeout in the constructor.

This assures you will have some operation timeout even when calling methods without explicit timeout



Back to parent page

Go to Table of Contents