[英]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();我想阅读这个元素中的文本,它是“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
有没有其他方法可以获取元素的文本
getText()
returns a promise , you need to resolve it: 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 Flow documentation page should clear things up. Promises 和 Control Flow文档页面应该可以解决问题。
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);
});
according to protractors documentation, .getText() returns a promise.根据量角器文档, .getText()返回一个承诺。
The best way to handle a promise, as of 2021, is to use async/await keywords.截至 2021 年,处理承诺的最佳方法是使用 async/await 关键字。 This will make Protractor 'freeze' and wait, until the promise is resolved before running the next command
这将使量角器“冻结”并等待,直到在运行下一个命令之前解决承诺
it('test case 1', async () => {
let text = await page.clientRowName.getText();
console.log(text);
})
.then()
can also be used, but using async/await
will make your code a lot more readable and easier to debug. .then()
也可以使用,但使用async/await
将使您的代码更具可读性和更易于调试。
我通常使用element.getAttribute('value')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.