Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Selenium IDE JavaScript and Command Parameters

In Selenium, JavaScript can be used with two types of Selenium command parameters: script and non-script. In most cases, QA Engineers need to access or manipulate a test case variable inside the JavaScript snippet used as a Selenium parameter.

 

All variables created in a test case are store in a JavaScript associative array. An associative array has string indexes rather than sequential numeric indexes. The associative array containing test case's variable is named storedVars. When users wish to access or manipulate a variable within a JavaScript snippet, they must refer to it as storeVars['yourVariableName'] .

 

Several Selenium commands specify a script parameter including assertEval, verifyEval, storeEval, and waitForEval. These parameters require no special syntax. A Selenium-IDE user would simply place a snippet of JavaScript code into the appropriate field. Normally, the Target filed.

The example below shows you how a JavaScript snippet can be used to perform a simple numerical calculation.

Command Target Value
store 10 Number1
store 20 Number2
storeEval parseInt(storedVars['Number1'])+parseInt(storedVars['Number2']) sumResult
echo ${sumResult}  

Here is the above script execution result.

  • [info] Executing: |store | 10 | Number1 |
  • [info] Executing: |store | 20 | Number2 |
  • [info] Executing: |storeEval | parseInt(storedVars['Number1'])+parseInt(storedVars['Number2']) | sumResult |
  • [info] script is: parseInt(storedVars['Number1'])+parseInt(storedVars['Number2'])
  • [info] Executing: |echo | ${sumResult} | |
  • [info] echo: 30

 

JavaScript Usage with Non-Script Parameters

JavaScript can also be used to help generate values for parameters, even when the parameter is not specified to be of type script. However, in this case, special syntax is required–the JavaScript snippet must be enclosed inside curly braces and preceded by the label JavaScript.

 

Command Target Value
store 10 Number1
store 20 Number2
echo javascript{parseInt(storedVars['Number1'])+parseInt(storedVars['Number2'])}