javascript - 在 PhantomJS 上禁用 LocalStorage 以进行干净测试?

标签 javascript testing local-storage phantomjs casperjs

几天前才开始使用 CasperJS。

我今天创建了一个脚本来测试 LocalStorage 的行为方式以及是否可以禁用它,因为为了防止测试相互影响,这是必要的。

背景

我正在开发一个要求在第一页上输入值的 Backbone 向导。当您单击“继续”按钮时,它会将值保存到 LocalStorage,然后在第二页上显示。

我正在使用 casperjs test <script.js>有无--local-storage-quota=0 .

第一次尝试

编写了一个执行以下操作的 CasperJS 脚本:

  1. 加载页面
  2. 检查模型的内容(应该是空的)
  3. 点击继续
  4. 在第 2 页加载后检查模型的内容(包含应有的值)
  5. 使用 casper.thenOpen() 直接将第 2 页作为新页面打开
  6. 在第 2 页加载后检查模型的内容

如果启用了 LocalStorage,则第 6 步的结果应该与第 4 步的结果相同(值存在于模型中)。

如果 LocalStorage 被禁用,第 6 步的结果应该与第 2 步相同(模型为空)。

每次运行脚本时,我都确定已启用 LocalStorage。 “--local-storage-quota=0”参数没有任何区别。

第二次尝试

那时,我决定确定 LocalStorage 是否附加到特定的 Casper 实例。如果是这样,那么我可以通过为每个测试创建一个新的 Casper 实例来解决这个问题,从而从头开始。

var Casper = require( 'casper' );

casper = Casper.create();
casper.test.begin( 'test for Local Storage, part 1', 0, function suite (test) { ... });

casper = Casper.create();
casper.test.begin( 'test for Local Storage, part 2', 0, function suite (test) { ... });

但是,第二个测试套件从未运行。我不知道 Casper 是否不打算在同一个脚本中创建多个实例,或者我只是错误地组织了它。

附录

我应该补充一点,所有测试套件都以以下步骤结束,以防相关:

casper.run( function () {
    test.done();
    casper.exit();
});

文档仅指定 test.done() 是必需的。但是,我的测试脚本将永远挂起,直到我添加对 casper.exit() 的调用。

最佳答案

您不能在 phantomjs 中禁用 localStorage 或 sessionStorage。但是,建议您在每次运行测试时清理执行环境。我建议添加具有完整设置的通用测试功能,如下所示:

function beginTest(casper, description, num, tests){
    function clearStorage(){
        casper.evaluate(function() {
            localStorage.clear();
            sessionStorage.clear();
        });
    }
    // Select between two possible signatures and queue a casper test
    if (typeof num == "number" && typeof tests == "function") {
        casper.test.begin(description, num, function suite(test){
            phantom.clearCookies();
            casper.start(config.baseurl, clearStorage);
            tests(test);
            casper.run(function(){
                test.done();
            });
        });
    } else if (typeof num == "function" && !tests) {
        casper.test.begin(description, function suite(test){
            phantom.clearCookies();
            casper.start(config.baseurl, clearStorage);
            num(test);
            casper.run(function(){
                test.done();
            });
        });
    }
}

然后你可以调用它

beginTest(casper, 'test for Local Storage, part 1', 0, function suite(test){ /* ... */ });
beginTest(casper, 'test for Local Storage, part 2', 0, function suite(test){ /* ... */ 

关于javascript - 在 PhantomJS 上禁用 LocalStorage 以进行干净测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18408868/

相关文章:

node.js - 了解 'integration' 测试中的 Mocha 语法

python - 循环测试函数时如何让Pytest识别迭代次数

database - WinRT/Metro 应用程序的本地数据库存储

jQuery Mobile - 保留用户访问的最后一页?

javascript - 我将如何保存一个 JS 变量,以便它可以在另一个 HTML 页面上使用

javascript - 如何将文本字段中的填充值保存到 Ionic 6/Capacitor 中的 SQLite 数据库?

javascript - JQuery:使用可拖动 div 进行 z 索引排序

javascript - 如何在我的 Google Apps 脚本/Google 表格 HTML 中包含脚本?

javascript - jquery 颜色选择器有问题吗?

model-view-controller - MVC 数据注解测试