且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在尝试捕捉方面需要帮助

更新时间:2022-02-25 23:21:59

对 element(locator).sendKeys 的调用返回一个已解决或拒绝的承诺.承诺是测试的控制流的一部分.

The call to element(locator).sendKeys returns a promise which is either resolved or rejected. The promise is part of the test's control flow.

对 element(locator) 本身的调用不会抛出错误,它是被拒绝的承诺.如果你没有找到一个元素,你实际上希望你的整个测试失败,因为场景无法完成.

The call to element(locator) itself does not throw an error, it is the promise which is rejected. If you fail to find an element you actually want your entire test to fail, since the scneario cannot be completed.

要获取错误消息,您可以使用承诺回调,如下所示.

To get the error message you can use the promise callbacks, as demonstrated below.

重要提示:如果你自己处理promise失败,你的测试不会失败,所以你***重新抛出它

Important note: if you handle the promise failure by yourself your test won't fail, so you should better rethrow it

try {
    element(by.id('usernameas')).sendKeys(data).then(function() {
        console.log('keys sent successfully');
    }, function(err) {
        console.error('error sending keys ' + err);
        throw err;
    });
}
catch(err) {
    console.log('error occured');
}

控制台输出是(修剪):

The console output is (trimmed):

error sending keys NoSuchElementError: no such element
  (Session info: chrome=31.0.1650.63)
  (Driver info: chromedriver=2.8.241075,platform=Windows NT 6.1 S .....