量角器:element.getText() returns 一个对象而不是字符串

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

我有一个元素定义为

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

我想阅读此元素中的文本,它是 "ABC" 但正在执行: var 客户端 = page.clientRowName.getText();

returns 对象而不是字符串。有没有其他方法可以获取元素的文本

getText()returns一个承诺,你需要解决它:

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

或者,如果您只想断言文本,让 expect() 为您解决承诺:

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

Promises and the Control Flow 文档页面应该清楚了。

我平时用element.getAttribute('value')

另一种解决方案可能是使用 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);
});

如果你在 2021 年,你会想阅读这个答案

根据量角器文档,.getText() returns 一个承诺。

截至 2021 年,处理承诺的最佳方式是使用 async/await 关键字。这将使 Protractor 'freeze' 并等待,直到在 运行 下一个命令

之前解决承诺
it('test case 1', async () => {
  let text = await page.clientRowName.getText();
  console.log(text);
})

.then()也可以,但是使用async/await会让你的代码更易读,更容易调试。