1

In my application, i have a value populated during runtime.

<span id="uxUnitPrice">182.18</span>

I want to extract this unit price and multiply against a fixed value for e.g. 15. Once i get the result on screen i want to fetch that and compare to see if calculation done by me matches what is displayed on screen. For e.g. result should be 182.18*15=2732.25 I tried below code but it is giving me NaN. Can someone please suggest how to perform a mathematical operation correctly using Selenium or protractor?

MeltValueCoin = element(by.css('[id="uxUnitPrice"]'));
expect(ValidateMeltValueCalculator()).tobe(2732.25);
function ValidateMeltValueCalculator(){
      var units = 10;
      var MeltValue = parseInt(MeltValueCoin.getText());
      return units*MeltValue;}
NewWorld
  • 764
  • 1
  • 10
  • 31

1 Answers1

2

The method getText returns a promise. Thus you first need to resolve it with then to get the value:

MeltValueCoin = element(by.css('[id="uxUnitPrice"]'));

expect(ValidateMeltValueCalculator()).tobe(2732.25);

function ValidateMeltValueCalculator(){
    var units = 10;
    return MeltValueCoin.getText().then(text => text * units);
}
Florent B.
  • 41,537
  • 7
  • 86
  • 101
  • Thanks. Can you please suggest how should I limit it to 2 decimal digits?If i multiple it by 10, this is returning me 1822.1000000000001 – NewWorld Oct 17 '16 at 21:45
  • Have a look here: http://stackoverflow.com/questions/11832914/round-to-at-most-2-decimal-places-in-javascript – Florent B. Oct 17 '16 at 21:49
  • I tried toFixed already. It is showing me that "Failed: result.toFixed is not a function". I also tried below but this is giving me 'Expected NaN to be 2733.15'. `var result = MeltValueCoin.getText().then(text=>text*units); return Math.round(result * 100) / 100' – NewWorld Oct 17 '16 at 21:56
  • From your comment `result` is a promise, so you can't round it. Try this instead: `return MeltValueCoin.getText().then(text => Math.round(text * units * 100) / 100);` – Florent B. Oct 17 '16 at 22:00
  • Thanks for the guidance. Below usage works better for me. `return MeltValueCoin.getText().then(text => (text * units).toFixed(2));` – NewWorld Oct 17 '16 at 22:07
  • Congrats on reaching the deserved 10k! – alecxe Oct 18 '16 at 15:56