1

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!

j0hny
  • 431
  • 1
  • 5
  • 16

1 Answers1

1

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
Community
  • 1
  • 1
JacekM
  • 4,041
  • 1
  • 27
  • 34