Javascript 量角器:element.getText() 返回一个对象而不是 String

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29478905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:26:12  来源:igfitidea点击:

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

javascripttestingprotractor

提问by Roopali Bansal

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();

我想阅读这个元素中的文本,它是“ABC”,但正在做: 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

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

回答by alecxe

getText()returns a promise, you need to resolveit:

getText()返回一个 promise,你需要解决它:

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()为您解决承诺:

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

Promises and the Control Flowdocumentation page should clear things up.

Promises 和 Control Flow文档页面应该可以解决问题。

回答by robdonn

Another solution may be to use async/await.

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

回答by ji-ruh

I usually used element.getAttribute('value')

我通常用 element.getAttribute('value')