【问题标题】:How to clean localstorage and sessionstorage on phantom?如何清理幻象上的本地存储和会话存储?
【发布时间】:2016-03-03 17:28:30
【问题描述】:

我在幻像和量角器的帮助下进行 e2e 测试,但是每次 我对我的代码进行了更改(不是 e2e 测试代码),似乎幻象不是 进行新的更改或清理 localStorage 和 SessionStorage。我认为它可能是缓存,但我不确定。

我尝试在我的 protractor.config 文件中添加下一句以清理 LocalStorage。

 onPrepare: function () { browser.executeScript("return window.localStorage.clear();"); }

但是,它没有工作,而不是我得到了这个错误

var template = new Error(this.message); ^ 未知错误: {"errorMessage":"SecurityError: DOM 异常 18","re​​quest":{"headers":{"Accept-Encoding":"gzip,deflate","Connection":"Keep-Alive","Content-Length":"58","Content-Type" :“应用程序/json; charset=utf-8","Host":"localhost:9781","User-Agent":"Apac 他-HttpClient/4.5.1 (Java/1.7.0_79)"},"httpVersion":"1.1","method":"POST","post":"{\"script\":\"return window.localStorage.clear();\",\"args\":[]}","url":"/execute","urlParsed":{"anchor":"","query":"", “文件”:“执行”,“目录”:“/”,“路径”:“/执行”,“相对” ":"/execute","port":"","host":"","password":"","user":"","userInfo":"","authority":"","协议":"","source":"/execute","queryKey":{},"chunks":["execute"]},"urlOriginal":"/session/dc9ce280-e15f-11e5-911a-4b92e3de49f0 /执行”}} 构建信息:版本:'2.51.0',修订:'1af067d',时间:'2016-02-05 19:15:17' 驱动信息:driver.version:未知

【问题讨论】:

    标签: phantomjs protractor


    【解决方案1】:

    和你一样,我也看到很多建议致电window.localStorage.clear() 以清除 PhantomJS 中的本地存储。但问题是——如果您在远程域上进行测试,此代码将引发安全异常,因为浏览器(不应该)允许跨域清除本地存储数据。

    但是,您可以控制在测试机器上写入本地存储的数据文件的位置。这将是 --local-storage-path(命令行参数)或(在 C# 中,Java 类似):

    PhantomJSDriverService service =
                           PhantomJSDriverService.CreateDefaultService();
    //IMPORTANT: avoid the common pitfall of trying to specify a file name!!
    // provide the path to the folder only
    service.LocalStoragePath = @"path\to\where\I\want\local-storage\saved;
    
    var driver = new PhantomJSDriver(service);
    

    本地存储文件以名称 [domain].localstorage 保存。在运行每个测试之前,请检查您指定的文件夹中是否存在 *.localstorage 文件,然后将其删除。这将为您清除本地存储。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-26
      • 2022-08-09
      • 2011-07-28
      • 2017-11-05
      • 1970-01-01
      • 2014-11-28
      • 2014-08-16
      相关资源
      最近更新 更多