Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if "should has text" in Jasmine?

I need to do something like:

expect(theElement.hasText()).toBe(true);

Do you know how can I do it?

I know that there is a "getText" function in protractor, but, how can I use it? Should I do?:

expect(theElement.getText().lenght > 0).toBe(true);

Thanks!

like image 602
Broda Noel Avatar asked Nov 29 '25 01:11

Broda Noel


1 Answers

I find jasmine-matchers library very helpful in terms of additional useful matchers. toBeNonEmptyString() is a perfect fit here (also notice how readable it is):

expect(theElement.getText()).toBeNonEmptyString();

FYI, here is the underlying implementation:

matchers.toBeNonEmptyString = function() {
  return matchers.toBeString.call(this) &&
    this.actual.length > 0;
};

It is quite reliable: it checks the type and the length.

like image 168
alecxe Avatar answered Nov 30 '25 15:11

alecxe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!