Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'am experimenting with selenium IDE and i came across a problem with asserting an approximate value. I need to check a value inside an element with an id. It is numeric value with comma (",") as a separator.

Problem is that i need to check if the numeric value is valid with a tolerance of 0.01.

For example:

<div id="uniqueId">2,54</div>

assertText - value = 2.53

I need above example to pass the test, and also pass if the value in div si 2,52 or 2,53. I understand that i can use assertEval to insert javascript, but i'm not very good in javascript and also from what i've read the javascript capabilities of selenium are limited.

Any help would be greatly appreciated!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Using assertEval is a good idea. The javascript you will need will be something like

var numberStr = "${actualText}".replace(",", ".");
var number = parseFloat(numberStr);
var difference = Math.abs(eval(number-${expectedValue}));
(difference <= 0.01)?true:false;

I don't know much javascript but according to this thread we need to first replace decimal mark from ',' to '.' (1st line) so we can later convert the string found on page to number (2nd line).

${actualText} is a variable in which we store the actual value taken from page while the ${expectedValue} is a value you need to define on your own. Note that tolerance (0.01) is "hardcoded", you may want to replace it with variable too.

Now to make it shorter (and less readable):

(Math.abs(eval(parseFloat("${actualText}".replace(",", "."))-${expectedValue}))<=0.01)?true:false

Having the javascript we can prepare Selenium script:

storeText  | id=uniqueId                   | actualText
store      | 2.53                          | expectedValue
assertEval | JS LINE FORM ABOVE GOES HERE  | true
share|improve this answer
    
Awesome! This is just the solution i needed, great job and thanks man! –  j0hny Aug 28 '13 at 8:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.