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.

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. Macro execution stops and waits for the user to press OK. Also useful for debugging. Of course, you can also use PAUSE | 0
prompt("Please enter a value") Ask user for input and store it in a variable.
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.
window.history.go(-1); Simulate BACK button click
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, 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.