2015-06-30 65 views
8

我試圖測試依賴localStorage的應用程序。當我手動與瀏覽器交互時,一切正常。但是,在nightwatch.js而不是所需的字符串時,請求localStorage時收到空響應。這適用於Chrome和Firefox。在nightwatch.js中啓用localStorage/webStorage

我已經嘗試過讓localStore在夜巡JSON通過分配"webStorageEnabled" : truedesiredCapabilities這樣的:

{ 
    "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何時工作?

回答

2

在我的測試正在與本地存儲,則需要使用「運行」命令,插入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); 
}); 
+0

這讓我走上了正軌,儘管在代碼中它在瀏覽器中打印出來而不是控制檯。我想通過result.value如何返回它。謝謝! –

2

什麼工作對我來說是傾銷的localStorage到一個新的互動對象,否則它返回一個空數組。這樣它可以在Nightwatch land中讀取:

client 
    .url('http://yahoo.com') 
    .execute(() => Object.assign({}, localStorage), [], (result) => { 
    console.log(result.value) 
    })