我正在尝试测试依赖localStorage的应用。当我手动与浏览器交互时,一切正常。但是,在nightwatch.js
而不是所需的字符串中,我在请求localStorage时得到null响应。这适用于Chrome和Firefox。
我已经尝试通过在"webStorageEnabled" : true
中分配desiredCapabilities
这样在nightwatch JSON中启用localStore:
{
"src_folders" : ["tests/functional/tests"],
"output_folder" : "tests/functional/reports",
"custom_commands_path" : "",
"custom_assertions_path" : "",
"globals_path" : "",
"selenium" : {
"start_process" : true,
"server_path" : "/Library/WebDevelopment/selenium-server/selenium-server-standalone-2.45.0.jar",
"log_path" : "",
"host" : "127.0.0.1",
"port" : 4444,
"cli_args" : {
"webdriver.chrome.driver" : "/usr/local/lib/node_modules/chromedriver/lib/chromedriver/chromedriver",
"webdriver.ie.driver" : ""
}
},
"test_settings" : {
"default" : {
"launch_url" : "http://localhost",
"selenium_port" : 4444,
"selenium_host" : "localhost",
"silent": true,
"screenshots" : {
"enabled" : false,
"path" : ""
},
"desiredCapabilities": {
"browserName": "chrome",
"webStorageEnabled" : true,
"databaseEnabled" : true,
"applicationCacheEnabled" : true,
"nativeEvents" : true,
"javascriptEnabled": true,
"acceptSslCerts": true
}
}
}
使用localStorage
时,nightwatch.js
是否有效?
答案 0 :(得分:2)
在我的测试中使用localstorage,你需要使用“execute”命令在浏览器中注入javascript并与浏览器localstorage进行交互
it.only('expectation', function (client) {
client.url('www.someurl.com').execute(function(data) {
try {
// statements
localStorage.pepe = 1
console.log('local', localStorage)
} catch(e) {
// statements
console.log(e);
}
return true;
}, [], function(result) {
});
client.pause(0);
});
答案 1 :(得分:2)
对我有用的是将localStorage转储到新的Object中,否则返回一个空数组。这样它在Nightwatch的土地上可读:
client
.url('http://yahoo.com')
.execute(() => Object.assign({}, localStorage), [], (result) => {
console.log(result.value)
})