0

My code:

var listItemText = element.all(by.css(".list-group-item.ng-binding.ngscope")).get(0).getText()
        .then(function(text){ return text.replace(/[\r\n]/g, "")
                    })
    expect(listItemText).toBeGreaterThan(0);

How to compare if string is number with texts should be greater than 0 in protractor.

nrs
  • 937
  • 3
  • 17
  • 42

2 Answers2

1

If you want to convert String to a Number you can either use parseFlaot for floating point numbers or parseInt for integers. As long as it will not start with letter characters it will cut off the non number part. Also watch out with parsing the number as it will also cut of leading 0s in front of a number - you might want to improve your regexp to grab only the number from where you expect it to be in the string to make it more bulletproof.

Also you don't need to use element.all(locator).get(0), element(locator) for multiple occurrences will always return first element found.

element(by.css(".list-group-item.ng-binding.ngscope")).getText()
    .then(function(text){
        var listItemText = text.replace(/[\r\n]/g, "");
        expect(praseFloat(listItemText)).toBeGreaterThan(0);
    });
Tom Tu
  • 9,573
  • 35
  • 37
  • 2
    In Protractor `expect()` is "patched" (actually by `jasminewd` package) to understand promises. There is no need to explicitly resolve the promise to make the expectation..but in this case `then()` would be needed to process the text anyway, so it does not hurt to have the expect inside `then()`.Thanks. – alecxe May 11 '16 at 13:54
  • @Tom Tu and Alecxe.. I tried both of your's code and both are working as i expected. Thanks – nrs May 12 '16 at 04:42
1

Expected '33990Times Jobs' to be greater than 0

First of all, you are comparing a string with a number. And, the string itself contains the extra Times Jobs part. Let's extract all the digits from the text and use parseInt to convert a string to an integer:

var listItemText = element.all(by.css(".list-group-item.ng-binding.ngscope")).first().getText().then(function(text) { 
    return parseInt(text.match(/\d+/)[0]);
});
expect(listItemText).toBeGreaterThan(0);
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195