storeEval (JS code, variable) - Selenium IDE command

UI Vision Selenium IDE++ (Kantu) V5.0: The storeEval command is deprecated. Please use the new executeScript. Old macros with storeEval continue to work without any change.

What is the difference between storeEval and executeScript?

Both commands evaluate Javascript in the context of the website. There are three main differences. First, with executeScript variables are not in quotation marks. Second, you need to add a "return" command if you want to return a result. And third, storedVars is no longer required.

Example (generate a random number between 0 and ${max}):

storeEval | Math.floor(Math.random()* Number ("${max}") | randomNumber

becomes

executeScript | return Math.floor(Math.random()* Number (${max}); | randomNumber

In addition in the UI Vision's Selenium IDE++ the ExecuteScript command is available as "sandboxed" version that does not run in the context of the website. This fixes a long-standing design issue of the classic Selenium IDE and avoids CSP errors. For more details see executeScript_Sandbox.

--- Old storeEval documentation: ---

The storeEval command is useful to run javascript snippets. The command works just like "getEval" or "runScript" but has the advantage that the result of EVAL is stored in a variable. The result can be, for example, a time stamp, a random number or an entry from a list. The new IDE does not support "getEval" or "runScript" because storeEval can do the same, just better.

Inside storeEval you can access the value of a variable with the ${BBB} notation. If you want to use the variable itself, use the storedVars['BBB'] notation. So this works the same as in the Firefox IDE, even so internally it is very different.

VerifyEval works just like StoreEval, but instead of adding the result to a variable, it compares the result with the value in the 3rd column. It is not available in the UI.Vision RPA Selenium IDE, but it can be recreated with storeEval plus if | ${!LastCommandOK} ... endIf.

See also .

Useful Javascript snippets for use with storeEval/getEval/runScript

Use them like storeEval | Javascript here | VarWithResult. Then later in the macro, you can use the result with the ${VarWithResult} variable.

Javascript Result
Math.floor(Math.random()*11) Random number between 0 and 10
x="abc"; x.length; Returns the length of the string (here 3). You see that always the value of the last command is returned by storeEval.
var audio = new Audio('http://www.storiesinflight.com/html5/audio/shimmer.wav');audio.play(); Play sound!
x="${myvar}"; x.length; Same as above but with variable as input.
Note that our variable ${myvar} is converted to a text string before the Javascript EVAL is executed. Therefore ${myvar} has to be inside "..." like any other text.
var d=new Date(); d.getDate()+'-'+((d.getMonth()+1))+'-'+d.getFullYear(); Get todays date
var d= new Date(); var m=((d.getMonth()+1)<10)?'0'+(d.getMonth()+1):(d.getMonth()+1); d.getFullYear()+"-"+m+"-"+d.getDate(); Get todays date in YYYY-MM-DD format
var d= new Date(new Date().getTime() + 24 * 60 * 60 * 1000 * 5); var m=((d.getMonth()+1)<10)?'0'+(d.getMonth()+1):(d.getMonth()+1); d.getFullYear()+"-"+m+"-"+d.getDate(); Get yesterday's date (-1), tomorrows date (1) or the date in 5 days (5) => Just change the number 5 in the code snippet
(new Date().getHours()+" : " + new Date().getMinutes() + " : " + new Date().getSeconds()) Get current time
window.document.getSelection().toString() Copy selected text (e. g. text marked with Ctrl+A)
alert("Hello World!") Show an alert box. Note: Since Chrome 64 the prompt box is only shown (by Chrome) if the tab has the focus. This is a change in Chrome, not Kantu. Better use PAUSE | 0
prompt("Please enter a value") Ask user for input and store it in a variable. Note: Since Chrome 64 the prompt box is only shown (by Chrome) if the tab has the focus. This is a change in Chrome, not UI.Vision RPA
window.document.getElementsByName('Phone')[0].value; Retrieve the field value an input box. This is also a replacement for storeValue from the old IDE. storeAttribute does not work with input boxes.
document.getElementByClassName(“label-bold”)[0].innerHTML; Extract some data, in this case from "<div class=“label-bold”> Order Number# 123456 </div>"
window.history.go(-1); Simulate BACK button click
parseFloat("${!runtime}")-parseFloat("${starttime}") Calculate the difference between the current runtime and the start time. Very useful for measuring website performance
var x = parseInt((new Date('2018-03-20T12:00:00') - new Date()) / 1000); Math.max(0, x); Countdown to specific date and time. Use the result as input for PAUSE

storeEval Example

This example calculates 100-20, and then sets the page title to the result (80).

Command Target Pattern/Text
open https://ui.vision/
store 100 AAA
store 20 BBB
storeEval ${AAA}-${BBB} CCC
storeEval document.title = ${CCC};
assertTitle 80

Works in

UI.Vision RPA for Chrome Selenium IDE, UI.Vision RPA for Firefox Selenium IDE, Firefox IDE Classic

Related Demo Macros

DemoStoreEval

The ready-to-import-and-run source code of all demo macros can be found in the Open-Source RPA software Github repository.

See also

storedVars, storetext, storetitle, Web Automation Extension User Manual, Selenium IDE commands, Classic Firefox Selenium IDE.

Anything wrong or missing on this page? Suggestions?

...then please contact us.

<em>UI.Vision RPA</em> Selenium IDE for Chrome and Firefox - Web Test Automation
Subscribe to the UI Vision RPA software newsletter . We'll send you updates on new releases that we're working on.