Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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
Javascript 在Cucumber中,回调函数通过调用回调来获取timedout。已增加DefaultTimeout,但仍然存在错误_Javascript_Node.js_Protractor_Cucumber - Fatal编程技术网

Javascript 在Cucumber中,回调函数通过调用回调来获取timedout。已增加DefaultTimeout,但仍然存在错误

Javascript 在Cucumber中,回调函数通过调用回调来获取timedout。已增加DefaultTimeout,但仍然存在错误,javascript,node.js,protractor,cucumber,Javascript,Node.js,Protractor,Cucumber,下面是给定的步骤定义,在此回调函数中,函数被成功命中,并且能够看到控制台行,即“这是一个回调函数” const assert=require('assert')) 常数{ 之前 鉴于 什么时候 然后 }=需要(“黄瓜”); 变量{ setDefaultTimeout }=需要(“黄瓜”); setDefaultTimeout(6*1000); 给定('这只有一个单词{string}',函数(string,callback){ console.log(字符串); 函数回调(){} 回调(); }

下面是给定的步骤定义,在此回调函数中,函数被成功命中,并且能够看到控制台行,即“这是一个回调函数”

const assert=require('assert'))
常数{
之前
鉴于
什么时候
然后
}=需要(“黄瓜”);
变量{
setDefaultTimeout
}=需要(“黄瓜”);
setDefaultTimeout(6*1000);
给定('这只有一个单词{string}',函数(string,callback){
console.log(字符串);
函数回调(){}
回调();
});1)当step函数中没有异步代码时,无需使用
回调
参数

Then(/^the response status is (.*)$/, function (status) {
  // Synchronous code
  assert.equal(this.responseStatus, status)
});
2) 但是,如果step函数中有任何异步代码,则需要使用
回调
参数或返回承诺

例如:

Given('This has only one word {string}', function (string, callback) {
   // the `callback` is specified by argument: callback

   ... do other things
   // when step done invoke the `callback`
   callback()
}

Given('This has only one word {string}', function (string, abc) {
   // the `callback` is specified by argument: abc

   ... do other things
   // when step done invoke the `callback`
   abc()
}

Given('This has only {amount} word {string}', function (amount, string, xyz) {
   // the `callback` is specified by argument: xyz

   ... do other things
   // when step done invoke the `callback`
   xyz()
}
重要:Cucumber将函数的最后一个参数指定为
回调
,无论您为哪个字符串指定参数名称

// Asynchronous - callback
// Take a callback as an additional argument to execute when the step is done
Then(/^the file named (.*) is empty$/, function (fileName, callback) {

  fs.readFile(fileName, 'utf8', function(error, contents) {
    if (error) {
      callback(error);
    } else {
      assert.equal(contents, '');
      callback();
    }
  });
});

// Asynchronous - promise
// Return a promise. The step is done when the promise resolves or rejects
When(/^I view my profile$/, function () {
  // Assuming this.driver is a selenium webdriver
  return this.driver.findElement({css: '.profile-link'}).then(function(element) {
    return element.click();
  });
});
回到您的代码:

Given('This has only one word {string}', function (string, callback) {
    console.log(string);
    // the `callback` specified by argument: callback   

    function callback() {} 
    // you define a function, named 'callback' too, which overwrite the
    // real `callback`

    callback();
    // you invoked the function you defined, not the real `callback`,
    // so cucumber wait the real `callback` be invoked, until reach
    // the timeout, then report timeout exception. 
});

感谢@yong再次提供您全面的答案,我真的在等待您的帮助,为我愚蠢的问题道歉。我可以通过skype/其他地方与您联系吗?