tronjavolta tronjavolta - 1 year ago 47
React JSX Question

How to use casperjs evaluate to diff 2 strings and assert

I have a simple test that checks to see a user's quota correctly changes after they upload a file.

casper.then(function() {
quota_begin = this.evaluate(function() {
return document.querySelector('.storage_used p').textContent;

casper.then(function() {

casper.then(function() {
quota_changed = this.evaluate(function() {
return document.querySelector('.storage_used p').textContent;
this.echo('Storage quota change: ' + quota_begin + ' => ' + quota_changed);

That last echo's output gives me:

Storage quota change: Upload quota 0B of 1GB used => Upload quota 192 KB of 1GB used

I'd like to include an assert in the test that fails when quota_begin and quota_changed do not actually change.

Something like:

test.assert(parseFloat(quota_changed) > parseFloat(quota_begin), "Quota was increased by file");

(doesn't work)

Is there an easy way to assert a diff on the two? regex?

Answer Source

Write a simple function to parse used bytes from that string will do that task:

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");