0

I'm new to Protractor and I'm trying to retrieve only the numeric values contained in the following element

<div class="balances">
 <h3>Total Balance: EUR 718,846.67</h3>
</div>
I'm able to retrieve the whole text but would like to be able to print off just "718,846.67" (or should it be 718846.67") via my page object file

checkFigures (figures) {
browser.sleep(8000);
   var checkBalance = element.all(by.css('balances'));
 checkBalance.getText().then(function (text) {
      console.log(text);
     
});
}

I came across this when someone posted a similar question but I have no idea how to implement it or what it is even doing

function toNumber(promiseOrValue) {

    // if it is not a promise, then convert a value
    if (!protractor.promise.isPromise(promiseOrValue)) {
        return parseInt(promiseOrValue, 10);
    }

    // if promise - convert result to number
    return promiseOrValue.then(function (stringNumber) {
        return parseInt(stringNumber, 10);
    });
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Edmond
  • 131
  • 3
  • 15
  • If you have control over the markup then best is to wrap the value inside its own element. For example: ```

    Total Balance: EUR 718,846.67

    ``` This way you can retrieve the values w/o the textual noise around it.
    – Benny Halperin Jun 25 '17 at 19:40
  • Hi, unfortunately not, the site was developed without e2e in mind so some aspects are a bit tricky to test – Edmond Jun 25 '17 at 20:22

1 Answers1

0

This is just a javascript question, and easily acomplished with replace and a regular expression. This will remove all non numerics from the string. Alter the regular expression as needed.

checkFigures (figures) {
    browser.sleep(8000);
       var checkBalance = element.all(by.css('balances'));
        checkBalance.getText().then(function (text) {
          console.log(text.replace(/\D/g,''));

    });
}
Brine
  • 3,733
  • 1
  • 21
  • 38
  • Thanks for that, I added `toString()` and it is working, my only issue now is that if a figure has a decimal, it removes the decimal. (25.63 will appear as 2563) I've tried a few other regular expressions but not getting them to work. – Edmond Jun 25 '17 at 20:22
  • Actually, don't think I need decimal place after all with how I'll be testing it – Edmond Jun 25 '17 at 20:36