toEqual by binding不起作用

toEqual by binding is not working

本文关键字:不起作用 binding by toEqual      更新时间:2023-09-26

我必须通过以下方式断言文本:

expect(accessPolicyPage.accessPolicyName).toEqual(element.all(by.binding("pol.name")).get(0).getText());

它给了我一些很长的错误,如下所示。

"访问策略名称01"应等于({ptor_:({controlFlow:Function,schedule:Function,setFileDetector:Function,getSession:Function,getCapabilities:Function,quit:Function,actions:Function,touchActions:Function,executeScript:Function,executeSyncScript:函数,调用:函数,等待:函数,睡眠:Function,getWindowHandle:Function,getAllWindowHandles:Function,getPageSource:Function,close:Function,getCurrentUrl:Function,getTitle:Function,findElementInternal_:Function,findElementsInternal_:函数,takeScreenshot:函数,管理:功能,switc

您在控制台上看到的是一个"可怕"的promise对象表示。如果您需要一个真实的值,使用then():显式地解析promise

element.all(by.binding("pol.name")).get(0).getText().then(function (text) {
    expect(accessPolicyPage.accessPolicyName).toEqual(text);
});

或者,由于accessPolicyPage.accessPolicyName是预先定义的实际文本,您可以交换匹配器中的内容,然后expect()隐式解析promise

expect(element.all(by.binding("pol.name")).get(0).getText()).toEqual(accessPolicyPage.accessPolicyName);

此选项更简单,通常推荐使用。

事实上,Protractor支持对承诺的期望。但它只处理在期望中的第一个论点是承诺时的情况。所以以下应该起作用:
expect(somePromise).toEqual(someString);
expect(somePromise).toEqual(anotherPromise);

但这个不会:

expect(notPromise).toEqual(somePromise);

getText()返回一个promise,而不是需要处理的文本。

Protractor:element.getText()返回一个对象,而不是字符串