通过Cucumber-Protractor,无法获取网页标题

Through Cucumber-Protractor, unable to get Webpage Title

通过 Cucumber-Protractor,我无法控制 "Given" 标签中的网页标题,并且所有测试都通过了。 对我来说 Protractor.conf.js 看起来不错 Protractor.conf.js-

exports.config = {
//seleniumAddress: 'http://127.0.0.1:4444/wd/hub',
getPageTimeout: 60000,
allScriptsTimeout: 500000,
framework: 'custom',
frameworkPath: require.resolve('protractor-cucumber-framework'),
capabilities: {
    'browserName': 'chrome'
},

specs: [
    'features/Login.feature'
],

cucumberOpts: {
    require: 'features/steps/logic.js',
    format: [require.resolve('cucumber-pretty')]

}};

logic.js-

在下面的代码中,无法通过 disp()

控制 "webpagetitle"

const assert = require('assert')
const {Before,Given,When,Then} = require('cucumber');

var webdriver = require('selenium-webdriver'),
 By = webdriver.By,
 until = webdriver.until;

var firefox = require('selenium-webdriver/firefox');

var options = new firefox.Options();
options.addArguments("-headless");

var driver = new webdriver.Builder()
 .forBrowser('firefox')
 .setFirefoxOptions(options)
 .build();

Given('Go to Title', function () {
 function navigate(callback) {
  console.log("Getting Consoled - Getting to Google");
  driver.get("https://www.google.com/");
  callback();

 }

 function disp() {
  console.log("Getting Consoled - Getting title");
  driver.getTitle().then(function (webpagetitle) {
   console.log(webpagetitle);
   console.log("Not getting consoled - This section has the issue");

  });
 }
 navigate(disp);
});

When('do nothing', function () {});

Then('do nothing here also', function () {});

感谢您的帮助。

@yong 更改时出错: 我是否需要在任何地方调用完成的回调(正如您已经调用的那样)?

1) Scenario: Add two number # features\Login.feature:5
   × Given Go to Title # practice\example6\node_modules\cucumber\lib\support_code_library_builder\build_helpers.js:173
       Error: function timed out, ensure the callback is executed within 5000 milliseconds
           at Timeout._time.default.setTimeout [as _onTimeout] (C:\Users\Mohit.Garg\Desktop\Cucumber practice\example6\node_modules\cucumber\lib\user_code_runner.js:81:20)
           at ontimeout (timers.js:436:11)
           at tryOnTimeout (timers.js:300:5)
           at listOnTimeout (timers.js:263:5)
           at Timer.processTimers (timers.js:223:10)
   - When do nothing # practice\example6\node_modules\cucumber\lib\support_code_library_builder\build_helpers.js:173
   - Then do nothing here also # practice\example6\node_modules\cucumber\lib\support_code_library_builder\build_helpers.js:173
   √ After # practice\example6\node_modules\cucumber\lib\support_code_library_builder\build_helpers.js:173

1 scenario (1 failed)
3 steps (1 failed, 2 skipped)
0m05.006s
[16:00:07] I/local - Shutting down selenium standalone server.
[16:00:07] I/launcher - 0 instance(s) of WebDriver still running
[16:00:07] I/launcher - chrome #01 failed 1 test(s)
[16:00:07] I/launcher - overall: 1 failed spec(s)
[16:00:07] E/launcher - Process exited with error code 1

C:\Users\Mohit.Garg\Desktop\Cucumber practice\example6>

Protractor 建立在 selenium-webdriver 之上并实现了控制异步执行的方法。因为你直接使用selenium-webdriver,所以你必须通过done()

来控制每个Cucumber step函数的异步执行
var {setDefaultTimeout} = require('cucumber');

setDefaultTimeout(60 * 1000);

Given('Go to Title', function (done) {
    function navigate(callback) {
        console.log("Getting Consoled - Getting to Google");
        driver.get("https://www.google.com/").then(function(){
           callback(done);
        })    
    }

    function disp(done) {
        console.log("Getting Consoled - Getting title");
        driver.getTitle().then(function (webpagetitle) {
            console.log(webpagetitle);
            console.log("Not getting consoled - This section has the issue");
            // call done() at here
            done()
        });
    }
    navigate(disp);
});