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

2022-01-16 00:00:00 testing protractor javascript

我有一个元素定义为

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

我想阅读这个元素中的文本,它是ABC",但是这样做:var client = page.clientRowName.getText();

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

推荐答案

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

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

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

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.

相关文章