Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create custom functions in Selenium IDE?

This should be possible according to JavaScript Functions in 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 giving it a name the statement runs:

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

But the next line fails to find the definition:

replaceText is not defined

I've also tried referencing the variable instead of the function:

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

But apparently it's still undefined:

null is not a function

I also tried making 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
like image 721
l0b0 Avatar asked Jan 17 '13 10:01

l0b0


People also ask

Is it possible to add custom functions in JavaScript?

In JavaScript you can create functions to write reusable blocks of code with functionality similar to most other programming languages, although, because of the nature of JavaScript, they might behave a bit differently, depending on the circumstances and the way you declare them.

What is custom function in JavaScript?

For more flexibility, you can create custom JavaScript functions to manipulate data in coaches and views. In a coach, define the custom JavaScript function in a custom HTML, within <script> tags. In a view, define the custom JavaScript functions in the Inline JavaScript editor, under Behavior.


2 Answers

What you need is a self executing anonymous function:

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

Note that you can also use your 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>
like image 97
katranci Avatar answered Sep 19 '22 09:09

katranci


I test above,but I got a error "[error] Threw an exception: missing ) after argument list" so,I change "${searchText}" to "storedVars['searchText']", it's ok :)

ps:JavaScript can be used with two types of Selenese parameters: script and non-script (usually expressions). In most cases, you’ll want to access and/or manipulate a test case variable inside the JavaScript snippet used as a Selenese parameter. All variables created in your test case are stored in a JavaScript associative array. An associative array has string indexes rather than sequential numeric indexes. The associative array containing your test case’s variables is named storedVars. Whenever you wish to access or manipulate a variable within a JavaScript snippet, you must refer to it as storedVars[‘yourVariableName’].

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

like image 21
user3026903 Avatar answered Sep 20 '22 09:09

user3026903