通过黄瓜量角器,无法获取网页标题

时间:2019-01-16 06:57:45

标签: javascript selenium protractor cucumber

通过黄瓜量角器,我无法在“给定”标签中管理网页标题,并且所有测试都无法通过。 对我来说,下面的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()来管理“网页标题”

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>

1 个答案:

答案 0 :(得分:0)

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

控制每个Cucumber步骤函数的异步执行
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);
});