Sunday, October 14, 2007

JavaScript Functions in Selenium IDE HTML Tests

I wanted to run a JavaScript function in my Selenium IDE HTML test. Specifically, I wanted to remove any currency symbols and commas from currency values. Here is the best way that I have found to do it.

First, declare the JavaScript function.

<tr>
<td>storeEval</td>
<td>
function(input) {var output =""; output = input.replace",","");
return output.replace("$","");}
</td>
<td>replaceText</td>
</tr>

To call the replaceText function do something like this:


<tr>
<td>storeEval</td>
<td>var parsedTotal = 0; parsedTotal =
parseFloat(storedVars['replaceText']('${Amount}')).toFixed(2);
</td>
<td>parsedTotal</td>
</tr>

Now I can use the parsedTotal value later in the test:

<tr>
<td>verifyExpression</td>
<td>${parsedTotal}</td>
<td>SomeValueHere</td>
</tr>

6 comments:

Chandrima said...

Hi,

This is what I was looking for. However when I use the javascript function 'replaceText' in my script, I get below error
[error] Threw an exception: syntax error
Do you have any corrections in syntax?

Please let me know. thanks for the help.

Mark McFadden said...

Chandrima,
The syntax worked for me. However, that was 4 years ago. What does your syntax look like?
Mark

Chandrima said...

Hi Mark,

Thanks a lot for the reply.

I have to use some amount values but selenium stores them with the currency and the comma e.g 10,000 EUR

So I tried to use the 'replacetext' function to convert the stored values into just amount.

So I used your function with and just used 'EUR' instead of '$'
but I am getting 'syntax error' in selenium :(..please help me :)

Anonymous said...

Can you please give selenium ide example for method "addScript". I m trying to use it like this

addScript | selenium.browserbot.getCurrentWindow().document.title="uak";

following error is displayed:
[error] Unexpected Exception: TypeError: head is undefined. fileName -> chrome://selenium-ide/content/selenium-core/scripts/selenium-api.js, lineNumber -> 3186, columnNumber -> 4

If I use it like:
addScript | javascript{selenium.browserbot.getCurrentWindow().document.title="uak";}

then it executed but with above error.

Please help.

Mark said...

Hi Umar,
It has been a long time but here is what I was able to dig up. Hope this helps:
https://gist.github.com/m2web/6058068

Anonymous said...

Hi Mark,

Thanks for prompt reply. The link does not talk about using "addScript" method.

Could you please take a look and suggest.