在AngularJS应用程序的Protractor测试中访问localStorage [英] Accessing localStorage in Protractor test for AngularJS application
本文介绍了在AngularJS应用程序的Protractor测试中访问localStorage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一些测试来验证输入数据是否正确存储在本地存储中,如何从量角器测试中访问localStorage?
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?
...
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获取数据吗?
How do you use executeScript? And could I get data from an executeScript?
推荐答案
要从本地存储中获取项目,请使用 window.localStorage.getItem()
通过 executeScript()
:
var value = browser.executeScript("return window.localStorage.getItem('key');");
expect(value).toEqual(expectedValue);
至 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();
这篇关于在AngularJS应用程序的Protractor测试中访问localStorage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文