storeEval (JS code, variable) - Selenium IDE command

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 Kantu Selenium IDE, but it can be recreated with storeEval plus if | ${!LastCommandOK} ... endIf.

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.
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.
Mvar 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 Kantu
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://a9t9.com/
store 100 AAA
store 20 BBB
storeEval ${AAA}-${BBB} CCC
storeEval document.title = ${CCC};
assertTitle 80

Works in

Kantu for Chrome Selenium IDE, Kantu 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 Github repository testmacros folder.

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.

Kantu Selenium IDE for Chrome and Firefox - Web Test Automation
Subscribe to the a9t9 automation software newsletter . We'll send you updates on new releases that we're working on.