node.jsprotractoraureliaangularjs-e2ee2e-testing

Protractor:Is it possible to print only the numbers contained in an elements that contains both numbers and characters


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);
    });
}


Solution

  • 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,''));
    
        });
    }