Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protractor: element.getText() returns an object and not String

I have an element defined as

this.clientRowName = element(by.id('CLIENT_NAME')); //page object file

I want to read the text in this element which is "ABC" but doing: var client = page.clientRowName.getText();

returns an object instead of a string. Is there any other way that I can get the text for the element

like image 735
Roopali Bansal Avatar asked Apr 06 '15 20:04

Roopali Bansal


People also ask

How do you use getText in protractor?

getText()If the element has any text in it, then getText( ) function is used to get that text from the element. element(by.id('username')). getText(); sendKeys() If you want to send text to the input field, we use sendKeys( ) function.

How to use getText in javascript?

getText() This method returns a string representing the value of the current element. You must use the other XML get methods to navigate through an XML document. This method is a synonym for getNodeValue();.


2 Answers

getText() returns a promise, you need to resolve it:

page.clientRowName.getText().then(function (text) {
    console.log(text);
});

Or, if you just want to assert the text, let expect() resolve the promise for you:

expect(page.clientRowName.getText()).toEqual("ABC");

Promises and the Control Flow documentation page should clear things up.

like image 151
alecxe Avatar answered Oct 18 '22 17:10

alecxe


Another solution may be to use async/await.

class Page {
  constructor() {
    this.clientRowName = $('#CLIENT_NAME');
  }
}

/****************/

it('should console.log client name', async () => {
  const client = await Page.clientRowName.getText();
  console.log(client);
});
like image 4
robdonn Avatar answered Oct 18 '22 19:10

robdonn