我有一个简单的测试,检查用户上传文件后配额是否正确更改。
casper.then(function() {
quota_begin = this.evaluate(function() {
return document.querySelector('.storage_used p').textContent;
});
});
casper.then(function() {
common.ACTIONS.uploadFile(casper);
});
casper.then(function() {
quota_changed = this.evaluate(function() {
return document.querySelector('.storage_used p').textContent;
});
this.echo('Storage quota change: ' + quota_begin + ' => ' + quota_changed);
});
最后一个echo的输出给我:
Storage quota change: Upload quota 0B of 1GB used => Upload quota 192 KB of 1GB used
我想在测试中包含一个断言,当quota_begin和quota_changed实际上没有改变时,该断言会失败。
类似:
test.assert(parseFloat(quota_changed) > parseFloat(quota_begin), "Quota was increased by file");
(没用)
是否有一种简单的方法来断言两者的差异?正则表达式?
编写一个简单的函数,从该字符串中解析已使用的字节,将执行该任务:
function get_used_bytes(input) {
var unit_dict = {'B':1,'KB':1024,'MB':1024*1024,'GB':1024*1024*1024}
var ret = /Upload quota ([d.]+)(S+) of ([d.]+)(S+) used/g.exec(input)
return ret[1] * unit_dict[ret[2]]
}
// get_used_bytes("Upload quota 192KB of 1GB used")
// 196608
test.assert(get_used_bytes(quota_changed) > get_used_bytes(quota_begin), "Quota was increased by file");