javascript 在 AngularJS 应用程序的量角器测试中访问 localStorage
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21960598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing localStorage in Protractor test for AngularJS application
提问by Zack Argyle
I am writing some tests to verify that input data is being stored in local storage correctly, how can I access localStorage from within the protractor test?
我正在编写一些测试来验证输入数据是否正确存储在本地存储中,如何从量角器测试中访问 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
How do you use executeScript? And could I get data from an executeScript?
你如何使用executeScript?我可以从 executeScript 获取数据吗?
回答by alecxe
To get an item from local storageuse window.localStorage.getItem()
through executeScript()
:
为了获得从本地存储中的项目使用window.localStorage.getItem()
过executeScript()
:
var value = browser.executeScript("return window.localStorage.getItem('key');");
expect(value).toEqual(expectedValue);
To clear local storagecall clear()
:
要清除本地存储的呼叫clear()
:
browser.executeScript("window.localStorage.clear();");
We can also have this helper object/wrapper around the local storage for convenience:
为方便起见,我们还可以在本地存储周围使用此辅助对象/包装器:
"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();