首页 > 解决方案 > 通过Cucumber-Protractor,无法获取网页标题

问题描述

通过 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')]

}};

逻辑.js-

在下面的代码中,无法通过 disp() 控制台“网页标题”

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 更改出错:我是否需要在任何地方调用 done 回调(正如您已经调用的那样)?

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>

标签: javascriptseleniumprotractorcucumber

解决方案


Protractor构建selenium-webdriver并实现了控制异步执行的方法。因为你selenium-webdriver直接使用,你必须通过控制每个 Cucumber step 函数的异步执行done()

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);
});

推荐阅读