在量角器测试中从 localStorage 中删除项目 [英] Remove an item from localStorage in a protractor test

查看:21
本文介绍了在量角器测试中从 localStorage 中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从量角器测试中删除 localStorage 中的一个条目

I am trying to remove an entry in the localStorage from a protractor test

describe('The feature', function() {

  beforeEach(function() {
    browser.executeScript('localStorage.removeItem("key");');
  });

  it('should do this', function() {

  });
});

但是当我在 chrome 中运行测试时出现这个错误

but i get this error when the test is run in chrome

UnknownError: <unknown>: Access to 'localStorage' is denied for this document. Storage is disabled inside 'data:' URLs.
  (Session info: chrome=32.0.1700.77)
  (Driver info: chromedriver=2.8.241036,platform=Mac OS X 10.9.0 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 436 milliseconds
Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:11:15'
System info: host: 'MyPC.local', ip: '192.168.1.1', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.9', java.version: '1.7.0_45'
Session ID: 23c01c8f756c653a6345e4b2f20c06e5
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{platform=MAC, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={userDataDir=/var/folders/9h/6j5pzftn4sxdw3rt25ffrqx80000gn/T/.org.chromium.Chromium.xrCG1d}, rotatable=false, locationContextEnabled=true, version=32.0.1700.77, takesHeapSnapshot=true, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=true, browserConnectionEnabled=false, nativeEvents=true, webStorageEnabled=true, applicationCacheEnabled=false, takesScreenshot=true}]

推荐答案

我通过在尝试清除/修改 sessionStorage 或 localStorage 之前检查 window.location 解决了这个问题.

I solved this issue by checking the window.location before attempting to clear/modify sessionStorage or localStorage.

如果页面尚未加载,则 window.location.hostname 将等于空字符串 ''.因此,如果您获得空字符串,则不要尝试与 sessionStoragelocalStorage 进行交互.

If a page has not been loaded then window.location.hostname will equal the empty string ''. So if you get the emptystring, then don't attempt to interact with sessionStorage or localStorage.

这是我在量角器套件中使用的一些 (ES6) 代码,以防止出现此错误.注意它是一个cube-js After函数,但它仍然是使用chrome从量角器执行的,它演示了你需要做什么来避免这个错误:

Here's some (ES6) code I used in my protractor suite to prevent this error. Note it's a cucumber-js After function, but it is still executed from protractor using chrome, and it demonstrates what you need to do to avoid this error:

this.After(function(scenario) {

  function getWindowLocation() {
    return window.location;
  }

  function clearStorage() {
    window.sessionStorage.clear();
    window.localStorage.clear();
  }

  return browser.executeScript(getWindowLocation).then(function(location) {
    // NB If no page is loaded in the scneario then calling clearStorage will cause exception
    // so guard against this by checking hostname (If no page loaded then hostname == '')
    if (location.hostname.length > 0) {
      return browser.executeScript(clearStorage);
    }
    else {
      return Promise.resolve();
    }
  });
});

这篇关于在量角器测试中从 localStorage 中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆