javascript - 通过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>
解决方案
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);
});
推荐阅读
- android - Android 暗模式打破覆盖的转换
- c# - power point interop API - TextRange 中的多段无法正常工作
- c++ - 以 size_t 为起点的 C++ 擦除向量部分
- google-bigquery - 用于从 BigQuery 查询数据、将结果导出为 CSV 格式以及 sFTP 传输到目标文件夹的脚本
- r - R:如何将两列合并为一列(对于记录器列,而不是按列合并)
- reactjs - 为什么我得到:在返回的组件实例上找不到`render`方法:您可能忘记定义`render`
- ssh - ansible AWX 库存冲突
- php - imap_fetchbody 返回奇怪的字符串
- excel - 如果标题相同,比较两个工作表并将数据从一个工作表附加到另一个工作表
- c# - Microsoft.NET.Test.Sdk 和
真的 对于 COM 互操作 - 互斥?