Selenium IDE JavaScript functions
In addition to user-defined JavaScript commands introduced through user-extensions.js
, the Selenium IDE allows the user to create JavaScript queries or functions directly in the Target field. For example, let's run a Google search by getting a random number between 1 and 100, as follows:
The following HTML source tags let you convert the steps into runnable test scripts:
<tr> <td>store</td> <td>javascript{'Random number ' + Math.floor(Math.random() * 100);} </td> <td>search</td> </tr> <tr> <td>echo</td> <td>${search}</td> <td></td> </tr> <tr> <td>open</td> <td>/</td> <td></td> </tr> <tr> <td>type</td> <td>id=gbqfq</td> <td>${search}</td> </tr>
Simple JavaScript execution
The predefined Selenium IDE JavaScript command runScript
is a very powerful command that lets the user execute simple JavaScript functions directly from the IDE, for example, javascript{alert("Hello!")}
.
Let's see how we can disable an active textbox and enable an inactive textbox using the following code snippet:
document.getElementsByName('****')[0].setAttribute('disabled', '') document.getElementsByName('****')[0].removeAttribute('disabled');
Mouse scroll
The scroll
event is currently unavailable in the Selenium IDE. However, the user-extensions.js
file includes a JavaScript method that lets you scroll the mouse through the web page.
Refer to the Google site https://sites.google.com/site/seleniumworks/selenium-ide-tricks to download user-extensions.js
. This user extension file includes IDE commands like while
, endWhile
, gotoIf
, gotoLabel
, and push
. Increase the value to 10
based upon the vertical length of the web page, as shown in the following screenshot:
Parameterization using arrays
The Selenium IDE command storeEval
is used to store values in a variable while running scripts, whereas storedVars
is a JavaScript associate array with string indexes containing variables. In the following example, storeEval
reserves the list of rivers in an array, and
getEval
is a command for initiating and incrementing the values. Some of the commands used in this section are purely user-defined, such as while
, endWhile
, and so on. Here, the endWhile
command is used to break the loop once the value inside the array reaches the maximum limit. The following screenshot gives us a clear idea of what is being discussed here:
Let's see another example of advanced parameterization concepts using the Selenium IDE. Refer to the Google site https://sites.google.com/site/seleniumworks/selenium-ide-tricks to download the user-extensions.js
file. The following screenshot captures the essence of this discussion:
In this example, the values are pushed into the array manually, and it does a Google search one by one.