首页 > 解决方案 > UnhandledPromiseRejectionWarning:未处理的承诺拒绝。警告在量角器控制台中使用 aync 和等待时不受欢迎

问题描述

我已经使用量角器编写了以下自动化代码。

this.promoteToOperationaltSandboxes3 = async function(){


        const allMainPanel =  element.all(by.className('tropos-appbox-main-panel col-md-12'));
        console.log(typeof allMainPanel);

        console.log('In the wait function , number of the main panels is '+allMainPanel.length);
const allLitags = await allMainPanel[0].all(by.className('dropdown-menu ng-scope')).all(by.tagName('li'));
        console.log('In the wait function , number of the litags is '+allLitags.length);
}

我在我的一个规范文件中调用这个函数,就像这样。

await appsPage.promoteToOperationaltSandboxes3();

在执行此功能时,即使浏览器没有打开并将错误显示为 ;

消息:TypeError:无法读取未定义的属性“全部”堆栈:TypeError:无法读取未定义的属性“全部”

此外,在日志中,我可以看到消息:

在等待功能中,主面板的数量未定义。

任何人都可以帮助我解决这些问题并帮助我更好地处理承诺。

标签: javascriptjasmineprotractor

解决方案


您应该先更改allMainPanel以获取索引:

const allMainPanel =  element.all(by.className('tropos-appbox-main-panel col-md-12')).get(0);  

然后allLitags也可能分成2个单独的变量:

const allLitags = allMainPanel.all(by.className('dropdown-menu ng-scope'));
const liElement = await allLitags.all(by.tagName('li'));

推荐阅读