我正在尝试测试依赖localStorage的应用程序。当我手动与浏览器交互时,一切正常。但是,在nightwatch.js而不是所需的字符串中,当请求localStorage时,我得到一个空响应。这适用于Chrome和Firefox。

我已经尝试通过像这样在"webStorageEnabled" : true中分配desiredCapabilities在夜间监视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
      }
    }
}

使用localStoragenightwatch.js应该工作吗?

最佳答案

在我的测试中正在使用localstorage,您需要使用“execute”命令将JavaScript注入(inject)浏览器中并与浏览器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);
});

关于selenium - 在nightwatch.js中启用localStorage/webStorage,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31134090/

10-17 00:49