How to create custom functions in Selenium IDE?

This should be possible according to the JavaScript Function in the Selenium IDE HTML tests :

<tr> <td>storeEval</td> <td>function(input) {return input.replace('foo', 'bar');}</td> <td>replaceText</td> </tr> <tr> <td>storeEval</td> <td>replaceText('foo')</td> <td>var</td> </tr> 

Instead, I get the following exception:

 function statement requires a name 

After specifying the name, the statement is executed:

 <tr> <td>storeEval</td> <td>function replaceText(input) {return input.replace('foo', 'bar');}</td> <td>replaceText</td> </tr> 

But the following line cannot find the definition:

 replaceText is not defined 

I also tried to refer to a variable instead of a function:

 <tr> <td>storeEval</td> <td>${replaceText}('foo')</td> <td>var</td> </tr> 

But apparently it is still undefined:

 null is not a function 

I also tried to make an anonymous function:

 <tr> <td>storeEval</td> <td>(function (input) {return input.replace('foo', 'bar')})</td> <td>replaceText</td> </tr> 

and running it with parentheses:

 <tr> <td>storeEval</td> <td>(${replaceText})('foo')</td> <td>var</td> </tr> 

Error:

 missing ) in parenthetical 

and without:

 <tr> <td>storeEval</td> <td>${replaceText}('foo')</td> <td>var</td> </tr> 

Error:

 missing ; before statement 
+4
source share
4 answers

You will need to perform anonymous function yourself :

 <tr> <td>storeEval</td> <td>(function(input) {return input.replace(input, 'bar');})('foo')</td> <td>replaceText</td> </tr> 

Note that you can also use variables as parameters:

 <tr> <td>store</td> <td>'foo'</td> <td>searchText</td> </tr> <tr> <td>storeEval</td> <td>(function(input) {return input.replace(input, 'bar');})(${searchText})</td> <td>replaceText</td> </tr> 
+3
source

I tested above, but I got the error message "[error] Threw an exception: none) after the argument list" so I change "$ {searchText}" to "storedVars ['searchText']", that's fine :)

ps: JavaScript can be used with two types of Selenese: script and non-script parameters (usually expressions). In most cases, you want to access and / or manipulate the test case variable inside the JavaScript fragment used as the Selenese parameter. All variables created in the test case are stored in an associative array of JavaScript. An associative array has string indices, not consecutive numeric indices. An associative array containing the variables of your test cases is called stored. Whenever you want to access or manipulate a variable in a JavaScript fragment, you must refer to it as storedVars ['yourVariableName].

http://www.seleniumhq.org/docs/02_selenium_ide.jsp#store-commands-and-selenium-variables

+1
source

Katranci's answer was really helpful, but as soon as I added for loops, the variables would lose scope. I ended up using the Katranci solution inside window.eval ().

 window.eval('(function() { var trs = document.querySelectorAll(".my-list table tbody tr"); for (var x in trs) { var trc = trs[x].childNodes; for (var y in trc) { var html = trc[y].innerHTML; if (typeof html != "undefined" && html.match(/Selenium Testing/)) { return trs[x].className.replace(" lastrow", ""); } } } } )();'); 

In this situation, I created test records with the prefix "Selenium Testing", and I use this code to identify them for subsequent test cases. This is a jquery-free page.

+1
source

You can define a function and reuse it elsewhere:

 <tr> <td>storeEval</td> <td>(function(){return function(min,max){return Math.floor(Math.random()*(max-min)) + min;} })()</td> <td>randomIntInRange</td> </tr> <tr> <td>storeEval</td> <td>(function(){return storedVars['randomIntInRange'](10000,99999) +'-'+ storedVars['randomIntInRange'](1000,9999) })()</td> <td>randomZip</td> </tr> <tr> <td>echo</td> <td>${randomZip}</td> <td></td> </tr> ... [info] echo: 92105-3139 

This works in Selenium IDE 2.9.0

+1
source

All Articles