Search code examples
javascriptjasmineprotractorfunctional-testingend-to-end

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!


Solution

  • 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.