.waitForElementVisible 是 Nightwatch 模板项目中未解决的功能
.waitForElementVisible is unresolved function in Nightwatch template project
我已按照 http://nightwatchjs.org/gettingstarted 中的步骤进行操作。
在我的测试文件中,我有来自 http://nightwatchjs.org/guide/
的下一个代码
module.exports = {
'Demo test Google' : function (browser) {
browser
.url('http://www.google.com')
.waitForElementVisible('body', 1000)
.setValue('input[type=text]', 'nightwatch')
.waitForElementVisible('button[name=btnG]', 1000)
.click('button[name=btnG]')
.pause(1000)
.assert.containsText('#main', 'Night Watch')
.end();
}
};
此外,这就是我的 package.json 的样子。
{
"name": "try_nw",
"version": "1.0.0",
"description": "try_nightwatch",
"main": "nightwatch.js",
"scripts": {
"test": "node nightwatch -e chrome"
},
"author": "",
"license": "ISC",
"dependencies": {
"bower": "^1.8.2",
"chromedriver": "^2.34.0",
"geckodriver": "^1.10.0",
"nightwatch": "^0.9.19",
"selenium-server-standalone-jar": "^3.8.1"
}
}
还有我的nightwatch.json
{
"src_folders" : ["tests"],
"output_folder" : "reports",
"custom_commands_path" : "",
"custom_assertions_path" : "",
"page_objects_path" : "",
"globals_path" : "",
"selenium" : {
"start_process" : true,
"start_session" : true,
"server_path" : "bin/selenium-server-standalone-3.8.1.jar",
"log_path" : "",
"port" : 4444,
"cli_args" : {
"webdriver.chrome.driver" : "bin/chromedriver.exe",
"webdriver.gecko.driver" : "",
"webdriver.edge.driver" : ""
}
},
"test_settings" : {
"default" : {
"launch_url" : "http://localhost",
"selenium_port" : 4444,
"selenium_host" : "localhost",
"silent": true,
"screenshots" : {
"enabled" : false,
"path" : ""
},
"desiredCapabilities": {
"browserName": "chrome",
"javascriptEnabled": true,
"acceptSslCerts": true,
"chromeOptions" : {
"args" : ["--no-sandbox", "--start-maximized", "--disable-infobars"]
}
}
},
"chrome" : {
"desiredCapabilities": {
"browserName": "chrome",
"javascriptEnabled": true,
"acceptSslCerts": true
}
},
"edge" : {
"desiredCapabilities": {
"browserName": "MicrosoftEdge"
}
}
}
}
但是,当我 运行 测试时,chrome 的空白选项卡打开并且控制台显示下一个输出。
Starting selenium server... started - PID: 15024
[Googletests] Test Suite
============================
Running: Demo test Google
× Timed out while waiting for element <body> to be present for 1000
milliseconds. - expected "visible" but got: "not found"
at Object.Demo test Google (E:\JS\Try_NW\tests\googletests.js:6:14)
at process._tickCallback (internal/process/next_tick.js:150:11)
FAILED: 1 assertions failed (5.972s)
_________________________________________________
TEST FAILURE: 1 assertions failed, 0 passed. (6.088s)
× googletests
- Demo test Google (5.972s)
Timed out while waiting for element <body> to be present for 1000 milliseconds. - expected "visible" but got: "not found"
at Object.Demo test Google (E:\JS\Try_NW\tests\googletests.js:6:14)
at process._tickCallback (internal/process/next_tick.js:150:11)
Process finished with exit code 1
因此, <.waitForElementVisible> 未定义。有人可以提出决定吗?
还有其他原因导致您的测试失败。我能够 运行 您的测试具有相同的 1000 ms
超时,并且它通过了轻微的修改。您可能想尝试增加 waitForElementVisible()
的超时时间。以下测试对我来说毫无问题地通过了。
browser
.url('http://www.google.com')
.waitForElementVisible('body', 1000)
.setValue('input[type=text]', ['nightwatch', browser.Keys.ENTER])
.pause(1000)
.assert.containsText('#main', 'Night Watch')
.end();
}
您遇到的问题不是 waitForElement,也不是您脚本的任何其他问题。之前的 setValue 与 chromeDriver 和您的浏览器的工作方式不同。这就是为什么您会看到一个选项卡打开以及错误消息是关于找不到 body
元素的原因。
如果您更新 chromedriver,您应该能够让您的脚本正常工作。
我已按照 http://nightwatchjs.org/gettingstarted 中的步骤进行操作。 在我的测试文件中,我有来自 http://nightwatchjs.org/guide/
的下一个代码module.exports = {
'Demo test Google' : function (browser) {
browser
.url('http://www.google.com')
.waitForElementVisible('body', 1000)
.setValue('input[type=text]', 'nightwatch')
.waitForElementVisible('button[name=btnG]', 1000)
.click('button[name=btnG]')
.pause(1000)
.assert.containsText('#main', 'Night Watch')
.end();
}
};
此外,这就是我的 package.json 的样子。
{
"name": "try_nw",
"version": "1.0.0",
"description": "try_nightwatch",
"main": "nightwatch.js",
"scripts": {
"test": "node nightwatch -e chrome"
},
"author": "",
"license": "ISC",
"dependencies": {
"bower": "^1.8.2",
"chromedriver": "^2.34.0",
"geckodriver": "^1.10.0",
"nightwatch": "^0.9.19",
"selenium-server-standalone-jar": "^3.8.1"
}
}
还有我的nightwatch.json
{
"src_folders" : ["tests"],
"output_folder" : "reports",
"custom_commands_path" : "",
"custom_assertions_path" : "",
"page_objects_path" : "",
"globals_path" : "",
"selenium" : {
"start_process" : true,
"start_session" : true,
"server_path" : "bin/selenium-server-standalone-3.8.1.jar",
"log_path" : "",
"port" : 4444,
"cli_args" : {
"webdriver.chrome.driver" : "bin/chromedriver.exe",
"webdriver.gecko.driver" : "",
"webdriver.edge.driver" : ""
}
},
"test_settings" : {
"default" : {
"launch_url" : "http://localhost",
"selenium_port" : 4444,
"selenium_host" : "localhost",
"silent": true,
"screenshots" : {
"enabled" : false,
"path" : ""
},
"desiredCapabilities": {
"browserName": "chrome",
"javascriptEnabled": true,
"acceptSslCerts": true,
"chromeOptions" : {
"args" : ["--no-sandbox", "--start-maximized", "--disable-infobars"]
}
}
},
"chrome" : {
"desiredCapabilities": {
"browserName": "chrome",
"javascriptEnabled": true,
"acceptSslCerts": true
}
},
"edge" : {
"desiredCapabilities": {
"browserName": "MicrosoftEdge"
}
}
}
}
但是,当我 运行 测试时,chrome 的空白选项卡打开并且控制台显示下一个输出。
Starting selenium server... started - PID: 15024
[Googletests] Test Suite
============================
Running: Demo test Google
× Timed out while waiting for element <body> to be present for 1000
milliseconds. - expected "visible" but got: "not found"
at Object.Demo test Google (E:\JS\Try_NW\tests\googletests.js:6:14)
at process._tickCallback (internal/process/next_tick.js:150:11)
FAILED: 1 assertions failed (5.972s)
_________________________________________________
TEST FAILURE: 1 assertions failed, 0 passed. (6.088s)
× googletests
- Demo test Google (5.972s)
Timed out while waiting for element <body> to be present for 1000 milliseconds. - expected "visible" but got: "not found"
at Object.Demo test Google (E:\JS\Try_NW\tests\googletests.js:6:14)
at process._tickCallback (internal/process/next_tick.js:150:11)
Process finished with exit code 1
因此, <.waitForElementVisible> 未定义。有人可以提出决定吗?
还有其他原因导致您的测试失败。我能够 运行 您的测试具有相同的 1000 ms
超时,并且它通过了轻微的修改。您可能想尝试增加 waitForElementVisible()
的超时时间。以下测试对我来说毫无问题地通过了。
browser
.url('http://www.google.com')
.waitForElementVisible('body', 1000)
.setValue('input[type=text]', ['nightwatch', browser.Keys.ENTER])
.pause(1000)
.assert.containsText('#main', 'Night Watch')
.end();
}
您遇到的问题不是 waitForElement,也不是您脚本的任何其他问题。之前的 setValue 与 chromeDriver 和您的浏览器的工作方式不同。这就是为什么您会看到一个选项卡打开以及错误消息是关于找不到 body
元素的原因。
如果您更新 chromedriver,您应该能够让您的脚本正常工作。