Icontem

File: test/tests_helpers.js

Recommend this page to a friend!
  Classes of Arturs Sosins  >  Countly Web SDK  >  test/tests_helpers.js  >  Download  
File: test/tests_helpers.js
Role: Unit test script
Content type: text/plain
Description: Unit test script
Class: Countly Web SDK
Track site accesses and errors the Countly API
Author: By
Last change: [eslint] more fixes
[eslint] stricter lint including styles
[tests] update tests
Merge pull request #34 from frknbasaran/ga-adapter

[ga-adapter] added countlyLoaded() method
Date: 5 months ago
Size: 26,088 bytes
 

Contents

Class file image Download
var fs = require("fs");
function exists(value){
    return (typeof value != "undefined") ? true : false;
}
casper.test.begin("Testing example_helpers.html", 360, function(test) {
    var tests = [];
    var cnt = 0;
    tests.push(function (message){
        test.assertEquals(message[0], 'Countly initialized');
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Session started');
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Got metrics');
        var params = JSON.parse(message[1]);
        test.assertEquals(params._app_version, '0.0');
        test.assertEquals(params._resolution, '1024x768');
        test.assertEquals(params._locale, 'en-US');
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Adding event: ');
        var params = JSON.parse(message[1]);
        test.assertEquals(params.key, "[CLY]_view");
        test.assertEquals(params.count, 1);
        test.assert(exists(params.segmentation));
        test.assert(exists(params.segmentation.name));
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Adding event: ');
        var params = JSON.parse(message[1]);
        test.assertEquals(params.key, "[CLY]_action");
        test.assertEquals(params.count, 1);
        test.assert(exists(params.segmentation));
        test.assertEquals(params.segmentation.type, "click");
        test.assert(exists(params.segmentation.x));
        test.assert(exists(params.segmentation.y));
        test.assert(exists(params.segmentation.width));
        test.assert(exists(params.segmentation.height));
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Adding event: ');
        var params = JSON.parse(message[1]);
        test.assertEquals(params.key, "linkClick");
        test.assertEquals(params.count, 1);
        test.assert(exists(params.segmentation));
        test.assert(exists(params.segmentation.href));
        test.assert(exists(params.segmentation.text));
        test.assert(exists(params.segmentation.id));
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Processing request');
        var params = JSON.parse(message[1]);
        test.assertEquals(params.begin_session, 1);
        test.assertEquals(params.app_key, "YOUR_APP_KEY");
        test.assert(exists(params.device_id));
        test.assert(exists(params.timestamp));
        test.assert(exists(params.hour));
        test.assert(exists(params.dow));
        params.metrics = JSON.parse(params.metrics);
        test.assertEquals(params.metrics._app_version, '0.0');
        test.assertEquals(params.metrics._resolution, '1024x768');
        test.assertEquals(params.metrics._locale, 'en-US');
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Sending XML HTTP request');
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Request Finished');
        var params = JSON.parse(message[1]);
        test.assertEquals(params.begin_session, 1);
        test.assertEquals(params.app_key, "YOUR_APP_KEY");
        test.assert(exists(params.device_id));
        test.assert(exists(params.timestamp));
        test.assert(exists(params.hour));
        test.assert(exists(params.dow));
        
        params.metrics = JSON.parse(params.metrics);
        test.assertEquals(params.metrics._app_version, '0.0');
        test.assertEquals(params.metrics._resolution, '1024x768');
        test.assertEquals(params.metrics._locale, 'en-US');
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Processing request');
        var params = JSON.parse(message[1]);

        test.assertEquals(params.app_key, "YOUR_APP_KEY");
        test.assert(exists(params.device_id));
        test.assert(exists(params.timestamp));
        test.assert(exists(params.hour));
        test.assert(exists(params.dow));
        
        params.events = JSON.parse(params.events);
        test.assertEquals(params.events.length, 3);
        
        test.assertEquals(params.events[0].key, '[CLY]_view');
        test.assert(exists(params.events[0].segmentation));
        test.assert(exists(params.events[0].segmentation.name));
        test.assertEquals(params.events[0].count, 1);
        
        test.assertEquals(params.events[1].key, "[CLY]_action");
        test.assertEquals(params.events[1].count, 1);
        test.assert(exists(params.events[1].segmentation));
        test.assertEquals(params.events[1].segmentation.type, "click");
        test.assert(exists(params.events[1].segmentation.x));
        test.assert(exists(params.events[1].segmentation.y));
        test.assert(exists(params.events[1].segmentation.width));
        test.assert(exists(params.events[1].segmentation.height));
        
        test.assertEquals(params.events[2].key, "linkClick");
        test.assertEquals(params.events[2].count, 1);
        test.assert(exists(params.events[2].segmentation));
        test.assert(exists(params.events[2].segmentation.href));
        test.assert(exists(params.events[2].segmentation.text));
        test.assert(exists(params.events[2].segmentation.id));
        
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Sending XML HTTP request');
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Request Finished');
        var params = JSON.parse(message[1]);
        test.assertEquals(params.app_key, "YOUR_APP_KEY");
        test.assert(exists(params.device_id));
        test.assert(exists(params.timestamp));
        test.assert(exists(params.hour));
        test.assert(exists(params.dow));
        
        params.events = JSON.parse(params.events);
        test.assertEquals(params.events.length, 3);
        
        test.assertEquals(params.events[0].key, '[CLY]_view');
        test.assert(exists(params.events[0].segmentation));
        test.assert(exists(params.events[0].segmentation.name));
        test.assertEquals(params.events[0].count, 1);
        
        test.assertEquals(params.events[1].key, "[CLY]_action");
        test.assertEquals(params.events[1].count, 1);
        test.assert(exists(params.events[1].segmentation));
        test.assertEquals(params.events[1].segmentation.type, "click");
        test.assert(exists(params.events[1].segmentation.x));
        test.assert(exists(params.events[1].segmentation.y));
        test.assert(exists(params.events[1].segmentation.width));
        test.assert(exists(params.events[1].segmentation.height));
        
        test.assertEquals(params.events[2].key, "linkClick");
        test.assertEquals(params.events[2].count, 1);
        test.assert(exists(params.events[2].segmentation));
        test.assert(exists(params.events[2].segmentation.href));
        test.assert(exists(params.events[2].segmentation.text));
        test.assert(exists(params.events[2].segmentation.id));
    });
    
    tests.push(function (message){
        test.assertEquals(message[0], 'Adding event: ');
        var params = JSON.parse(message[1]);
        test.assertEquals(params.key, "[CLY]_action");
        test.assertEquals(params.count, 1);
        test.assert(exists(params.segmentation));
        test.assertEquals(params.segmentation.type, "click");
        test.assert(exists(params.segmentation.x));
        test.assert(exists(params.segmentation.y));
        test.assert(exists(params.segmentation.width));
        test.assert(exists(params.segmentation.height));
    });
    
    tests.push(function (message){
        test.assertEquals(message[0], 'Adding event: ');
        var params = JSON.parse(message[1]);
        test.assertEquals(params.key, "formSubmit");
        test.assertEquals(params.count, 1);
        test.assert(exists(params.segmentation));
        test.assertEquals(params.segmentation.id, "");
        test.assertEquals(params.segmentation.name, "comments");
        test.assertEquals(params.segmentation.action, "");
        test.assertEquals(params.segmentation.method, "post");
        test.assertEquals(params.segmentation["input:message"], "Message Name");
        test.assertEquals(params.segmentation["input:textarea"], "Message");
        test.assertEquals(params.segmentation["input:select-one"], "option1");
        test.assertEquals(params.segmentation["input:submit-form"], "Submit");
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Adding event: ');
        var params = JSON.parse(message[1]);
        test.assertEquals(params.key, "[CLY]_view");
        test.assertEquals(params.count, 1);
        test.assert(params.dur >= 29 && params.dur <= 31);
        test.assert(exists(params.segmentation));
        test.assert(exists(params.segmentation.name));
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Session extended');
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Countly initialized');
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Processing request');
        var params = JSON.parse(message[1]);
        test.assert(params.session_duration >= 29 && params.session_duration <= 31);
        test.assertEquals(params.app_key, "YOUR_APP_KEY");
        test.assert(exists(params.device_id));
        test.assert(exists(params.timestamp));
        test.assert(exists(params.hour));
        test.assert(exists(params.dow));
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Sending XML HTTP request');
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Adding event: ');
        var params = JSON.parse(message[1]);
        test.assertEquals(params.key, "[CLY]_view");
        test.assertEquals(params.count, 1);
        test.assert(exists(params.segmentation));
        test.assert(exists(params.segmentation.name));
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Request Finished');
        var params = JSON.parse(message[1]);
        test.assert(params.session_duration >= 29 && params.session_duration <= 31);
        test.assertEquals(params.app_key, "YOUR_APP_KEY");
        test.assert(exists(params.device_id));
        test.assert(exists(params.timestamp));
        test.assert(exists(params.hour));
        test.assert(exists(params.dow));
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Processing request');
        var params = JSON.parse(message[1]);
        test.assertEquals(params.app_key, "YOUR_APP_KEY");
        test.assert(exists(params.device_id));
        test.assert(exists(params.timestamp));
        test.assert(exists(params.hour));
        test.assert(exists(params.dow));
        
        params.events = JSON.parse(params.events);
        test.assertEquals(params.events.length, 3);
        
        test.assertEquals(params.events[0].key, "[CLY]_action");
        test.assertEquals(params.events[0].count, 1);
        test.assert(exists(params.events[0].segmentation));
        test.assertEquals(params.events[0].segmentation.type, "click");
        test.assert(exists(params.events[0].segmentation.x));
        test.assert(exists(params.events[0].segmentation.y));
        test.assert(exists(params.events[0].segmentation.width));
        test.assert(exists(params.events[0].segmentation.height));
        
        test.assertEquals(params.events[1].key, 'formSubmit');
        test.assert(exists(params.events[1].segmentation));
        test.assertEquals(params.events[1].segmentation.id, "");
        test.assertEquals(params.events[1].segmentation.name, "comments");
        test.assertEquals(params.events[1].segmentation.action, "");
        test.assertEquals(params.events[1].segmentation.method, "post");
        test.assertEquals(params.events[1].segmentation["input:message"], "Message Name");
        test.assertEquals(params.events[1].segmentation["input:textarea"], "Message");
        test.assertEquals(params.events[1].segmentation["input:select-one"], "option1");
        test.assertEquals(params.events[1].segmentation["input:submit-form"], "Submit");
        
        test.assertEquals(params.events[2].key, "[CLY]_view");
        test.assertEquals(params.events[2].count, 1);
        test.assert(params.events[2].dur >= 29 && params.events[2].dur <= 31);
        test.assert(exists(params.events[2].segmentation));
        test.assert(exists(params.events[2].segmentation.name));
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Sending XML HTTP request');
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Request Finished');
        var params = JSON.parse(message[1]);
        test.assertEquals(params.app_key, "YOUR_APP_KEY");
        test.assert(exists(params.device_id));
        test.assert(exists(params.timestamp));
        test.assert(exists(params.hour));
        test.assert(exists(params.dow));
        
        params.events = JSON.parse(params.events);
        test.assertEquals(params.events.length, 3);
        
        test.assertEquals(params.events[0].key, "[CLY]_action");
        test.assertEquals(params.events[0].count, 1);
        test.assert(exists(params.events[0].segmentation));
        test.assertEquals(params.events[0].segmentation.type, "click");
        test.assert(exists(params.events[0].segmentation.x));
        test.assert(exists(params.events[0].segmentation.y));
        test.assert(exists(params.events[0].segmentation.width));
        test.assert(exists(params.events[0].segmentation.height));
        
        test.assertEquals(params.events[1].key, 'formSubmit');
        test.assert(exists(params.events[1].segmentation));
        test.assertEquals(params.events[1].segmentation.id, "");
        test.assertEquals(params.events[1].segmentation.name, "comments");
        test.assertEquals(params.events[1].segmentation.action, "");
        test.assertEquals(params.events[1].segmentation.method, "post");
        test.assertEquals(params.events[1].segmentation["input:message"], "Message Name");
        test.assertEquals(params.events[1].segmentation["input:textarea"], "Message");
        test.assertEquals(params.events[1].segmentation["input:select-one"], "option1");
        test.assertEquals(params.events[1].segmentation["input:submit-form"], "Submit");
        
        test.assertEquals(params.events[2].key, "[CLY]_view");
        test.assertEquals(params.events[2].count, 1);
        test.assert(params.events[2].dur >= 29 && params.events[2].dur <= 31);
        test.assert(exists(params.events[2].segmentation));
        test.assert(exists(params.events[2].segmentation.name));
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Processing request');
        var params = JSON.parse(message[1]);
        test.assertEquals(params.app_key, "YOUR_APP_KEY");
        test.assert(exists(params.device_id));
        test.assert(exists(params.timestamp));
        test.assert(exists(params.hour));
        test.assert(exists(params.dow));
        
        params.events = JSON.parse(params.events);
        test.assertEquals(params.events[0].key, '[CLY]_view');
        test.assert(exists(params.events[0].segmentation));
        test.assert(exists(params.events[0].segmentation.name));
        test.assertEquals(params.events[0].count, 1);
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Sending XML HTTP request');
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Request Finished');
        var params = JSON.parse(message[1]);
        test.assertEquals(params.app_key, "YOUR_APP_KEY");
        test.assert(exists(params.device_id));
        test.assert(exists(params.timestamp));
        test.assert(exists(params.hour));
        test.assert(exists(params.dow));
        
        params.events = JSON.parse(params.events);
        test.assertEquals(params.events[0].key, '[CLY]_view');
        test.assert(exists(params.events[0].segmentation));
        test.assert(exists(params.events[0].segmentation.name));
        test.assertEquals(params.events[0].count, 1);
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Got metrics');
        var params = JSON.parse(message[1]);
        test.assertEquals(params._app_version, '0.0');
        test.assertEquals(params._resolution, '1024x768');
        test.assertEquals(params._locale, 'en-US');
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Adding event: ');
        var params = JSON.parse(message[1]);
        test.assertEquals(params.key, "[CLY]_action");
        test.assertEquals(params.count, 1);
        test.assert(exists(params.segmentation));
        test.assertEquals(params.segmentation.type, "click");
        test.assert(exists(params.segmentation.x));
        test.assert(exists(params.segmentation.y));
        test.assert(exists(params.segmentation.width));
        test.assert(exists(params.segmentation.height));
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Got metrics');
        var params = JSON.parse(message[1]);
        test.assertEquals(params._app_version, '0.0');
        test.assertEquals(params._resolution, '1024x768');
        test.assertEquals(params._locale, 'en-US');
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Processing request');
        var params = JSON.parse(message[1]);
        test.assertEquals(params.app_key, "YOUR_APP_KEY");
        test.assert(exists(params.device_id));
        test.assert(exists(params.timestamp));
        test.assert(exists(params.hour));
        test.assert(exists(params.dow));
        
        params.crash = JSON.parse(params.crash);
        test.assertEquals(params.crash._resolution, '1024x768');
        test.assertEquals(params.crash._app_version, '0.0');
        test.assertEquals(params.crash._background, true);
        test.assertEquals(params.crash._nonfatal, false);
        test.assertEquals(params.crash._logs, "Pressed unhandled button");
        test.assert(exists(params.crash._error));
        test.assert(exists(params.crash._run));
        test.assert(exists(params.crash._custom));
        test.assertEquals(params.crash._custom.jquery, "1.10");
        test.assertEquals(params.crash._custom.jqueryui, "1.10");
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Sending XML HTTP request');
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Request Finished');
        var params = JSON.parse(message[1]);
        test.assertEquals(params.app_key, "YOUR_APP_KEY");
        test.assert(exists(params.device_id));
        test.assert(exists(params.timestamp));
        test.assert(exists(params.hour));
        test.assert(exists(params.dow));
        
        params.crash = JSON.parse(params.crash);
        test.assertEquals(params.crash._resolution, '1024x768');
        test.assertEquals(params.crash._app_version, '0.0');
        test.assertEquals(params.crash._background, true);
        test.assertEquals(params.crash._nonfatal, false);
        test.assertEquals(params.crash._logs, "Pressed unhandled button");
        test.assert(exists(params.crash._error));
        test.assert(exists(params.crash._run));
        test.assert(exists(params.crash._custom));
        test.assertEquals(params.crash._custom.jquery, "1.10");
        test.assertEquals(params.crash._custom.jqueryui, "1.10");
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Processing request');
        var params = JSON.parse(message[1]);
        test.assertEquals(params.app_key, "YOUR_APP_KEY");
        test.assert(exists(params.device_id));
        test.assert(exists(params.timestamp));
        test.assert(exists(params.hour));
        test.assert(exists(params.dow));
        
        params.crash = JSON.parse(params.crash);
        test.assertEquals(params.crash._resolution, '1024x768');
        test.assertEquals(params.crash._app_version, '0.0');
        test.assertEquals(params.crash._background, true);
        test.assertEquals(params.crash._nonfatal, true);
        test.assertEquals(params.crash._logs, "Pressed handled button");
        test.assert(exists(params.crash._error));
        test.assert(exists(params.crash._run));
        test.assert(exists(params.crash._custom));
        test.assertEquals(params.crash._custom.jquery, "1.10");
        test.assertEquals(params.crash._custom.jqueryui, "1.10");
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Sending XML HTTP request');
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Request Finished');
        var params = JSON.parse(message[1]);
        test.assertEquals(params.app_key, "YOUR_APP_KEY");
        test.assert(exists(params.device_id));
        test.assert(exists(params.timestamp));
        test.assert(exists(params.hour));
        test.assert(exists(params.dow));
        
        params.crash = JSON.parse(params.crash);
        test.assertEquals(params.crash._resolution, '1024x768');
        test.assertEquals(params.crash._app_version, '0.0');
        test.assertEquals(params.crash._background, true);
        test.assertEquals(params.crash._nonfatal, true);
        test.assertEquals(params.crash._logs, "Pressed handled button");
        test.assert(exists(params.crash._error));
        test.assert(exists(params.crash._run));
        test.assert(exists(params.crash._custom));
        test.assertEquals(params.crash._custom.jquery, "1.10");
        test.assertEquals(params.crash._custom.jqueryui, "1.10");
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Processing request');
        var params = JSON.parse(message[1]);
        params.events = JSON.parse(params.events);
        test.assertEquals(params.events.length, 1);
        
        test.assertEquals(params.events[0].key, "[CLY]_action");
        test.assertEquals(params.events[0].count, 1);
        test.assert(exists(params.events[0].segmentation));
        test.assertEquals(params.events[0].segmentation.type, "click");
        test.assert(exists(params.events[0].segmentation.x));
        test.assert(exists(params.events[0].segmentation.y));
        test.assert(exists(params.events[0].segmentation.width));
        test.assert(exists(params.events[0].segmentation.height));
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Sending XML HTTP request');
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Request Finished');
        var params = JSON.parse(message[1]);
        params.events = JSON.parse(params.events);
        test.assertEquals(params.events.length, 1);
        
        test.assertEquals(params.events[0].key, "[CLY]_action");
        test.assertEquals(params.events[0].count, 1);
        test.assert(exists(params.events[0].segmentation));
        test.assertEquals(params.events[0].segmentation.type, "click");
        test.assert(exists(params.events[0].segmentation.x));
        test.assert(exists(params.events[0].segmentation.y));
        test.assert(exists(params.events[0].segmentation.width));
        test.assert(exists(params.events[0].segmentation.height));
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Session extended');
        test.assertEquals(message[1], '61');
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Processing request');
        var params = JSON.parse(message[1]);
        test.assert(params.session_duration >= 59 && params.session_duration <= 61);
        test.assertEquals(params.app_key, "YOUR_APP_KEY");
        test.assert(exists(params.device_id));
        test.assert(exists(params.timestamp));
        test.assert(exists(params.hour));
        test.assert(exists(params.dow));
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Sending XML HTTP request');
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Request Finished');
        var params = JSON.parse(message[1]);
        test.assert(params.session_duration >= 59 && params.session_duration <= 61);
        test.assertEquals(params.app_key, "YOUR_APP_KEY");
        test.assert(exists(params.device_id));
        test.assert(exists(params.timestamp));
        test.assert(exists(params.hour));
        test.assert(exists(params.dow));
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Adding event: ');
        var params = JSON.parse(message[1]);
        test.assertEquals(params.key, "[CLY]_view");
        test.assertEquals(params.dur, 65);
        test.assert(exists(params.segmentation));
        test.assert(exists(params.segmentation.name));
        test.assert(!exists(params.segmentation.visit));
        test.assertEquals(params.count, 1);
    });
    tests.push(function (message){
        test.assertEquals(message[0], 'Session extended');
        test.assertEquals(message[1], '4');
    });
    casper.removeAllListeners('remote.message');
    casper.on('remote.message', function(message) {
        this.echo(message);
        tests[cnt](message.split("\n"));
        cnt++;
    });
    casper.start(fs.workingDirectory+"/examples/example_helpers.html", function() {
        this.click('#track_link');
        var ob = this;
        setTimeout(function(){
            ob.click('#submit-form');
        }, 30000);
        setTimeout(function(){
            ob.click('#unhandled_error');
            ob.click('#handled_error');
        }, 70000);
    }).run(function() {
        setTimeout(function(){
            casper.clear();
            casper.clearCache();
            casper.clearMemoryCache();
            casper.open(fs.workingDirectory+"/test/files/clear.html", function() {});
            test.done();
        }, 95000);
    });
});