Cucumber 量角器尝试/捕获问题
我正在使用量角器和Cucumber,并注意到在某些情况下,我希望使用locator:By.cssSelector(“someCssLocatorHere”)捕获一个Cucumber 量角器尝试/捕获问题,cucumber,try-catch,protractor,nosuchelementexception,Cucumber,Try Catch,Protractor,Nosuchelementexception,我正在使用量角器和Cucumber,并注意到在某些情况下,我希望使用locator:By.cssSelector(“someCssLocatorHere”)捕获一个NoSuchElementError:No元素。但是,除非调用.then()函数的回调和errBack并抛出错误,以便以后可以像下面这样捕获它,否则使用传统的try/catch块将不起作用: try{ somePromise.then(function(){ //callback function if promise get
NoSuchElementError:No元素。但是,除非调用.then()函数的回调和errBack并抛出错误,以便以后可以像下面这样捕获它,否则使用传统的try/catch块将不起作用:
try{
somePromise.then(function(){
//callback function if promise gets resolved successfully
}, function(e){
console.log(e); //errBack in case promise gets rejected or fails
throw e; <--------------- THROW error here so can catch it in catch block below
});
}catch(e){
console.log('error:'+e);
}
try { <---------------------------- Traditional TRY/CATCH method
loadWebApp();
login();
openUserPreferences();
changePassword();
} catch (err) {
console.error(
"An error was thrown! " + err);
}
loadWebApp().
then(login).
then(openUserPreferences).
then(changePassword).
then(null, function(err) { <----------------- PROTRACTOR equivalent of try/catch
console.error(
"An error was thrown! " + err);
});
试试看{
somePromise.then(function(){
//成功解析承诺时的回调函数
},功能(e){
console.log(e);//在承诺被拒绝或失败时出错
抛出e;我最近遇到了这个问题,并注意到您不需要try/catch块。在量角器中,您可以实现try/catch,如下所示:
try{
somePromise.then(function(){
//callback function if promise gets resolved successfully
}, function(e){
console.log(e); //errBack in case promise gets rejected or fails
throw e; <--------------- THROW error here so can catch it in catch block below
});
}catch(e){
console.log('error:'+e);
}
try { <---------------------------- Traditional TRY/CATCH method
loadWebApp();
login();
openUserPreferences();
changePassword();
} catch (err) {
console.error(
"An error was thrown! " + err);
}
loadWebApp().
then(login).
then(openUserPreferences).
then(changePassword).
then(null, function(err) { <----------------- PROTRACTOR equivalent of try/catch
console.error(
"An error was thrown! " + err);
});
试试看{