Kasun Kodagoda
Kasun Kodagoda

Reputation: 4024

Getting the button text in Protractor

I have created a custom locator to find an element with the ng-click method used. I have used it to get a reference to a button in my DOM.

this.button = element(by.ngClick('login()'));

i want to get the text that is on the button from the reference. For example if the button has "Click to Login" as the text, how can i extract that from the button reference?

Upvotes: 3

Views: 4730

Answers (1)

Delian Mitankin
Delian Mitankin

Reputation: 3691

You can call getText() on the element selector, but keep in mind it returns a promise. The promise could be fed to expect though, it would resolve it and perform the comparision:

expect(this.button.getText()).toBe('Click to Login');

If you need to use the text for anything else in your code you'll have to resolve the promise yourself:

this.button.getText().then(function (text) {
    console.log(text);
});

Upvotes: 9

Related Questions