Processing Javascript with Selenium

 

 

To allow you to create dynamic testing scripts, i.e. ones where the information entered onto forms, such as dates change over time, Cloud Testing has implemented the processing of JavaScript.

Stored Variables

The commands store, storeValue and storeText can be used to store a variable value for later.

Store javascript{var d=new Date(); d.toString()} dateToday
Store javascript{var d=new Date(); d.setDate(d.getDate() + 1); d.toDateString();} dateTomorrow
Store javascript{var d=new Date(); d.setDate(d.getDate() + 40); d.toDateString();} date40days
Store Plain text string variableName

These variables are stored internally in a map called “storedVars”, with values keyed by the variable name, which can be accessed as follows:

storedVars['date40days']

JavaScript

All parameters or arguments for Selenium commands can be constructed using both simple variable substitution as well as full JavaScript. They both have access to the variables stored above, but in different ways:

Full JavaScript – javascript{…}

Javascript evaluation provides the full power of JavaScript when constructing a parameter or argument for a Selenium command. To use this mechanism, the entire parameter value must be prefixed by ‘javascript{‘ with a trailing ‘}’. The text inside the braces is evaluated as a JavaScript expression, and can access previously stored variables using the storedVars map detailed above.

Note that variable substitution cannot be combined with JavaScript evaluation.

Echo javascript{storedVars['dateTomorrow']}
Type textField javascript{‘Tomorrow=’ + storedVars['dateTomorrow']}
Echo javascript{‘Tomorrow=’ + storedVars['dateTomorrow']}
Type fullName javascript{storedVars['fullName'].toUpperCase()}

Simple variable substitution – ${…}

Variable substitution provides a simple way to use a previously stored variable in a command parameter. The variable to substitute is indicated by ${variableName}. Multiple variables can be substituted, and intermixed with static text.

Echo ${dateTomorrow}
Type textField Today: ${dateToday} Tomorrow: ${dateTomorrow}
Echo ${variableName}
Type fullName $(title} ${firstName} ${lastName}

Example in Cloud Testing

The following example from Cloud Testing shows what can be done.

JavaScript - settings

The example script is available to view in the ‘Live Demo’ account is available via the Online demo page.

Did you enjoy this post? Why not subscribe to our feed and get articles like this delivered automatically to your feed reader.

Comments

No comments yet.

Sorry, the comment form is closed at this time.