我试图在nightwatch.js中编写一个简单的脚本,该脚本将打开Google,然后在搜索字段中输入文本。使用两个setValue()
方法将文本发送到元素时,我遇到了问题。
这是我的剧本:
module.exports = {
'Search bar displayed on Google Home Page'(driver) {
driver
.url('http://www.google.com')
.assert.urlContains('google')
.assert.title('Google')
.waitForElementPresent('input[title="Search"]')
.pause(5000)
.setValue('input[title="Search"]', 'test123') // error happens here
.end()
},
}
使用setValue()
时,出现以下错误:
运行.setElementValue()协议操作时出错:TypeError [ERR_UNESCAPED_CHARACTERS]:尝试为“ / wd / hub / session / ed0680ce58544facf2a4b193eccbc223 / element / [object Object] / value”创建HTTP请求时出错:请求路径包含未转义的字符在新的ClientRequest(_http_client.js:115:13)在Object.request(http.js:42:10)在HttpRequest.createHttpRequest在新的Promise()在Selenium2Protocol.sendProtocolAction
由于某种原因,.setValue()
试图在请求URL中将Object object
作为WebElement
ID发送。
脚本成功执行了assert
和.waitForElementPresent('input[title="Search"]')
,因此我知道该元素在页面上。我添加了pause(5000)
以确保在尝试发送密钥之前页面有足够的时间加载。我还尝试过在.click()
之前运行.keys()
来尝试使元素成为焦点。
我相信语法是正确的,但我对守夜人还是陌生的,所以这也可能是个问题。
此用户与我几乎有完全相同的问题,但没有答案:setValue method in Nightwatch is not working
我正在使用chromedriver
版本80.0.3987.16
我正在使用nightwatch
版本1.3.4
我使用chromedriver
安装了npm
至npm install chromedriver
,并在我的chromedriver.exe
文件中将路径设置为nightwatch.json
:
{
"src_folders" : ["tests"],
"output_folder" : "reports",
"custom_commands_path" : "",
"custom_assertions_path" : "",
"page_objects_path" : "./page_objects",
"globals_path" : "",
"selenium" : {
"start_process" : true,
"server_path" : "./node_modules/selenium-standalone/.selenium/selenium-server/3.141.5-server.jar",
"log_path" : "./reports",
"host": "127.0.0.1",
"port" : 4444,
"cli_args" : {
"webdriver.chrome.driver" : "./node_modules/chromedriver/lib/chromedriver/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",
"marionette": true,
"javascriptEnabled": true,
"acceptSslCerts": true
}
},
"chrome" : {
"desiredCapabilities": {
"browserName": "chrome"
}
}
}
}
有人可以帮助我了解这里的问题吗?
通过在我的"w3c": false
文件中的"chrome"
下添加nightwatch.json
解决了该问题。
特别感谢@Raju,他们的示例存储库中的nightwatch.conf.js
文件提供了此示例。
我更改了此:
"test_settings" : {
"default" : {
"launch_url" : "http://localhost",
"selenium_port" : 4444,
"selenium_host" : "localhost",
"silent": true,
"screenshots" : {
"enabled" : false,
"path" : ""
},
"desiredCapabilities": {
"browserName": "chrome",
"marionette": true,
"javascriptEnabled": true,
"acceptSslCerts": true
}
}
为此:
"test_settings" : {
"default" : {
"launch_url" : "http://localhost",
"selenium_port" : 4444,
"selenium_host" : "localhost",
"silent": true,
"screenshots" : {
"enabled" : false,
"path" : ""
},
"desiredCapabilities": {
"browserName": "chrome",
"marionette": true,
"javascriptEnabled": true,
"acceptSslCerts": true,
"chromeOptions": {
"w3c": false
}
}
}
添加chromeOptions
字段加上"w3c": false
现在一切正常。