odi*_*ity 5 javascript testing local-storage phantomjs casperjs
几天前刚刚开始与CasperJS合作.
我今天创建了一个脚本来测试LocalStorage的行为方式以及是否可以禁用它,因为这是为了防止测试相互影响.
背景
我正在开发一个在第一页上要求输入值的Backbone向导.单击"继续"按钮时,它会将值保存到LocalStorage,然后在第二页上显示.
我casperjs test <script.js>有和无用--local-storage-quota=0.
第一次尝试
写了一个执行以下操作的CasperJS脚本:
如果启用了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) { ... });
Run Code Online (Sandbox Code Playgroud)
但是,第二个测试套件永远不会运行.我不知道Casper是不打算在同一个脚本中创建多个实例,或者我是否只是错误地组织它.
附录
我应该补充说,所有测试套件都以以下步骤结束,以防相关:
casper.run( function () {
test.done();
casper.exit();
});
Run Code Online (Sandbox Code Playgroud)
文档仅指定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();
});
});
}
}
Run Code Online (Sandbox Code Playgroud)
然后您可以使用以下命令调用它
beginTest(casper, 'test for Local Storage, part 1', 0, function suite(test){ /* ... */ });
beginTest(casper, 'test for Local Storage, part 2', 0, function suite(test){ /* ... */
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3019 次 |
| 最近记录: |