Advancedcommands

From Autodrive
Jump to: navigation, search

This chapter shows how to create advanced commands. Below are a few examples on aspects opening up for programming advanced commands in Java and JavaScript. Only imagination limits its use.

Plain scripting language usage

SCRIPT:javascript
var s = 'Plain javascript string variable';
ENDSCRIPT

Accessing Automation Thread (ADbase)

This is a very powerful feature, which makes it possible to be cost-effective when automating things. Modularization has to be chosen carefully, though.

/*
 * The main automation thread at exposes all public methods on the ADbase thread object, see javadoc
 *
 * This example loads defined javascript functions from a file named stdfunc.js
 * located in the project root folder.
 */
SCRIPT:javascript
  importPackage( Packages.dk.autodrive.base );

  var f = ''+at.loadJS("stdfunc.js"); eval(f);
ENDSCRIPT

Reporting script steps

The htmlreport has a log coloumn (L). Steps within a script block can be reported here. A log4j like loglevel can be supplied.

This feature comes in handy when trying to understand unattended automations or when simply debugging some script.

/*
 * Reporting two steps to the htmlreport column (L)
 */
SCRIPT:javascript
  importPackage( Packages.dk.autodrive.base );

  at.Log('Begin','INFO');
  var f = ''+at.loadJS("stdfunc.js"); eval(f);
  at.Log('End','INFO');
ENDSCRIPT

Two additional columns can be used in the htmlreport. These coloumns are named (I)nput and (O)utput. They are primarily used by plugins when it makes sense. E.g. the Webservice plugin will report the request document in coloumn (I) and the response document in coloumn (O). The FTP and Connectivity plugins shows a transcript of the communication too.

Error handling

/*
 * This catches all exceptions from javascript and java, only exception is script errors
 */
SCRIPT:javascript
try {
  var s = 'Plain javascript string variable';
} catch (err) {
  // do something and report error to the automation driver
  at.throwError(err);
}
ENDSCRIPT
/*
 * The following error is not caught. The script error is caught by the automation driver and 
 * reported, which can be seen from the htmlreport.
 *
 * The script will never execute
 *
 * Furthermore the (L) column in the htmlreport will print this script block with line numbers
 */
SCRIPT:javascript
try {
  var s = ''';
} catch (err) {
  // do something
}
ENDSCRIPT

Calling Java class

SCRIPT:javascript
  importPackage( Packages.dk.autodrive.util );
  ResultWriter rw = new ResultWriter();
  rw.println("Test message to default result.txt file in the root of the project");
  rw.close();
ENDSCRIPT

Calling plugin

/*
 * theObj exposes all public methods on the plugin, see javadoc
 */
SCRIPT:javascript
  importPackage( Packages.dk.autodrive.base );
  importPackage( Packages.dk.autodrive.plugins );

  var f = ''+at.loadJS("stdfunc.js"); eval(f);
  theObj = getPlugin(Packages.dk.autodrive.plugins.AD_Webservice_Commands);
ENDSCRIPT

theObj is an object reference exposing all public method on the given plugin.

Variable substitution

The automation driver can be instructed to read a property file during startup. The content should be in ordinary Java property style. The actual file is provided to the driver by means of a system property "ad.properties".

############################################
### Properties for the driver 
############################################

# Example
my.variable.name=value

This can be referenced within script blocks.

/*
 * Variable substitution. Example is logging "value"
 */
SCRIPT:javascript
  importPackage( Packages.dk.autodrive.base );

  at.Log(''+at.getProperty('my.variable.name'),'INFO');
ENDSCRIPT

This pattern can also be overriden such that is can be used for controlling script block execution. I.e. only execute a script block given some state is present, which might be set in an earlier script block.

/*
 * 1st script block setting a variable property
 */
SCRIPT:javascript
  importPackage( Packages.dk.autodrive.base );

  at.setProperty('my.inline.variable.name','some value');
ENDSCRIPT
/*
 * 2nd script block logging inline variable
 */
SCRIPT:javascript
  importPackage( Packages.dk.autodrive.base );

  at.Log(''+at.getProperty('my.inline.variable.name'),'INFO');
ENDSCRIPT