Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 量角器:element.getText()返回一个对象而不是字符串_Javascript_Testing_Protractor - Fatal编程技术网

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

Javascript 量角器:element.getText()返回一个对象而不是字符串,javascript,testing,protractor,Javascript,Testing,Protractor,我有一个元素定义为 this.clientRowName = element(by.id('CLIENT_NAME')); //page object file 我想阅读此元素中的文本,它是“ABC”,但要做: var client=page.clientRowName.getText() 返回一个对象而不是字符串。是否有其他方法可以获取元素的文本返回承诺,您需要解决它: page.clientRowName.getText().then(function (text) { conso

我有一个元素定义为

this.clientRowName = element(by.id('CLIENT_NAME')); //page object file
我想阅读此元素中的文本,它是“ABC”,但要做: var client=page.clientRowName.getText()

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

返回承诺,您需要解决它:

page.clientRowName.getText().then(function (text) {
    console.log(text);
});
或者,如果您只想声明文本,让
expect()
为您解决承诺:

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

文档页面应该会把事情弄清楚。

我通常使用
元素。getAttribute('value')
另一种解决方案可能是使用
异步/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年,你会想读这个答案 根据量角器文档,返回一个承诺

从2021年起,处理承诺的最佳方式是使用async/await关键字。这将使量角器“冻结”并等待,直到承诺得到解决,然后再运行下一个命令

it('test case 1', async () => {
  let text = await page.clientRowName.getText();
  console.log(text);
})

.then()
也可以使用,但是使用
async/await
将使代码更可读,更易于调试。

如果使用Chai expections,它有一个
。最终
方法来解析承诺并从其中获取值
expect(page.clientRowName.getText()).to.finally.equal(“ABC”)
大家好,我可以在toEqual()或toBe()中使用getText函数吗?类似于:expect(element.getText()).toEqual(element2.getText())
。getAttribute('value')
可能不起作用。您也可以尝试
.getAttribute('textContent')
.getAttribute('innerText')