Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.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
Cucumber 量角器尝试/捕获问题_Cucumber_Try Catch_Protractor_Nosuchelementexception - Fatal编程技术网

Cucumber 量角器尝试/捕获问题

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

我正在使用量角器和Cucumber,并注意到在某些情况下,我希望使用locator:By.cssSelector(“someCssLocatorHere”)捕获一个
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);
    });
试试看{