我正在编写一些测试来验证输入数据是否正确存储在本地存储中,如何从量角器测试中访问localStorage?
...
describe('vgPersist', function() {
it('Should save input data in local storage until form submitted', function() {
// Prepare Object and Open browser
var addOns = new AddOns();
addOns.get();
-> Clear localStorage
-> Get from localStorage
你如何使用executeScript?我可以从executeScript获取数据吗?
答案 0 :(得分:27)
从本地存储中获取项目使用window.localStorage.getItem()
到executeScript()
:
var value = browser.executeScript("return window.localStorage.getItem('key');");
expect(value).toEqual(expectedValue);
要清除本地存储空间,请致电clear()
:
browser.executeScript("window.localStorage.clear();");
为方便起见,我们还可以在本地存储周围安装此辅助对象/包装器:
"use strict";
var LocalStorage = function () {
this.getValue = function (key) {
return browser.executeScript("return window.localStorage.getItem('" + key + "');");
};
this.get = function () {
browser.executeScript("return window.localStorage;");
};
this.clear = function () {
browser.executeScript("return window.localStorage.clear();");
};
};
module.exports = new LocalStorage();