Skip to content

Doesn't work on non-angular website #2

@chonduhvan

Description

@chonduhvan

Hi!

I'm trying to test non-angular website but window.GAWebTester.getLastEvent() is empty.

For example http://www.w3schools.com test

Output:

Starting selenium standalone server...
[launcher] Running 1 instances of WebDriver
Selenium standalone server started at http://192.168.10.24:56868/wd/hub
Demo application for the Home page of the Google Analytics WebTester
  The Library deployment
    The page without "Google Analytics Event Data Interceptor"
      should have "window.ga()" defined - pass
      should not have a global "GAWebTester" object - pass
    The registration of the "Google Analytics Event Data Interceptor"
      should have "window.ga()" defined - pass
      should have "window.GAWebTester.getEventBuffer()" defined - pass
      should have "window.GAWebTester.getLastEvent()" defined - pass
    The deregistration of the "Google Analytics Event Data Interceptor"
      should have "window.ga()" defined - pass
      should have "window.GAWebTester" be null - pass
  The "GAWebTester.EventBuffer" object
    should be exposed globally - pass
  The wiring up of the Google Analytics Event Data interceptor
    The "LastEvent" object
      should contain the pageview - fail

Failures:

  1) Demo application for the Home page of the Google Analytics WebTester The wiring up of the Google Analytics Event Data interceptor The "LastEvent" object should contain the pageview
  at 19.006s [Sun, 12 Jul 2015 18:39:14 GMT]
   Message:
     Expected {  } to equal [ 'send', 'pageview' ].
   Stacktrace:
     Error: Expected {  } to equal [ 'send', 'pageview' ].
    at successCallback (/home/chonduhvan/Storage/Projects/GoogleAnalytics-WebTester/nyt/e2e/scenarios/home/scenarios-home.js:246:47)
    at Array.forEach (native)

Finished in 19.007 seconds
9 tests, 17 assertions, 1 failure

Shutting down selenium standalone server.
[launcher] 0 instance(s) of WebDriver still running
[launcher] chrome #1 failed 1 test(s)
[launcher] overall: 1 failed spec(s)
[launcher] Process exited with error code 1

protractor.conf.js

exports.config = {
    allScriptsTimeout: 11 * 1000,
    suites: {
        home:      'scenarios/home/*.js'
    },
    exclude: [],
    directConnect: false,
    capabilities: {
        'browserName': 'chrome'
    },
    baseUrl: 'http://www.w3schools.com',
    framework: 'jasmine',
    jasmineNodeOpts: {
        isVerbose: true,
        showColors: true,
        showTiming: true,
        includeStackTrace: true,
        defaultTimeoutInterval: 30 * 1000
    },
    params: {
        GoogleAnalyticsWebTester: {
            usesGACalls: true,
            usesGTMCalls: false,
            submitToGA: false,
            disableClicks: false
        }
    },
    onPrepare: './onPrepare.js'
};

onPrepare.js

beforeEach(function () {
    'use strict';

    var GoogleAnalyticsWebTester = require('./../../lib/GoogleAnalyticsWebTester');
    browser.driver.get(browser.baseUrl);
    browser.ignoreSynchronization = true;
    GoogleAnalyticsWebTester.initialize({
        browserParams: browser.params, 
        browserDriver: browser.driver  
    });
});

scenarios-home.js

'use strict';

describe('Demo application for the Home page of the Google Analytics WebTester', function () {
    describe('The Library deployment', function () {
        describe('The page without "Google Analytics Event Data Interceptor"', function () {
            beforeEach(function () {
                browser.get('/').then(
                    function() { browser.driver.sleep(1000);
                    });
            });

            it('should have "window.ga()" defined', function (done) {
                browser.driver.executeScript(function () {
                    return window.ga;
                })
                .then(function successCallback (ga) {
                    expect( ga ).toBeDefined();
                    expect( ga ).not.toBeNull();
                })
                .then(null, function errorCallback (error) {
                    fail('Error: ' + JSON.stringify(error));
                })
                .then(done);
            });

            it('should not have a global "GAWebTester" object', function (done) {
                browser.driver.executeScript(function () {
                    return window.GAWebTester;
                })
                .then(function successCallback (GAWebTester) {
                    expect( GAWebTester ).toBeNull();
                })
                .then(null, function errorCallback (error) {
                    fail('Error: ' + JSON.stringify(error));
                })
                .then(done);
            });
        });


        describe('The registration of the "Google Analytics Event Data Interceptor"', function () {
            beforeEach(function () {
                browser.get('/').then(
                function() { browser.driver.sleep(3000);
                });

                browser.driver.registerGoogleAnalyticsEventDataInterceptor();
            });

            it('should have "window.ga()" defined', function (done) {
                browser.driver.executeScript(function () {
                    return window.ga;
                })
                .then(function successCallback (ga) {
                    expect( ga ).toBeDefined();
                    expect( ga ).not.toBeNull();
                })
                .then(null, function errorCallback (error) {
                    fail('Error: ' + JSON.stringify(error));
                })
                .then(done);
            });

            it('should have "window.GAWebTester.getEventBuffer()" defined', function (done) {
                browser.driver.executeScript(function () {
                    return window.GAWebTester.getEventBuffer();
                })
                .then(function successCallback (EventBuffer) {
                    expect( EventBuffer ).toBeDefined();
                    expect( EventBuffer ).not.toBeNull();
                    expect( EventBuffer ).toEqual( [] );
                })
                .then(null, function errorCallback (error) {
                    fail('Error: ' + JSON.stringify(error));
                })
                .then(done);
            });

            it('should have "window.GAWebTester.getLastEvent()" defined', function (done) {
                browser.driver.executeScript(function () {
                    return window.GAWebTester.getLastEvent();
                })
                .then(function successCallback (LastEvent) {
                    expect( LastEvent ).toBeDefined();
                    expect( LastEvent ).not.toBeNull();
                    expect( LastEvent ).toEqual( {} );
                })
                .then(null, function errorCallback (error) {
                    fail('Error: ' + JSON.stringify(error));
                })
                .then(done);
            });
        });


        describe('The deregistration of the "Google Analytics Event Data Interceptor"', function () {
            beforeEach(function () {
                browser.get('/').then(
                function() { browser.driver.sleep(1000);
                });
                browser.driver.registerGoogleAnalyticsEventDataInterceptor();
                browser.driver.unregisterGoogleAnalyticsEventDataInterceptor();
            });

            it('should have "window.ga()" defined', function (done) {
                browser.driver.executeScript(function () {
                    return window.ga;
                })
                .then(function successCallback (ga) {
                    expect( ga ).toBeDefined();
                    expect( ga ).not.toBeNull();
                })
                .then(null, function errorCallback (error) {
                    fail('Error: ' + JSON.stringify(error));
                })
                .then(done);
            });

            it('should have "window.GAWebTester" be null', function (done) {
                browser.driver.executeScript(function () {
                    return window.GAWebTester;
                })
                .then(function successCallback (GAWebTester) {
                    expect( GAWebTester ).toBeNull();
                })
                .then(null, function errorCallback (error) {
                    fail('Error: ' + JSON.stringify(error));
                })
                .then(done);
            });
        });
    });


    describe('The "GAWebTester.EventBuffer" object', function () {
        beforeEach(function () {
            browser.driver.registerGoogleAnalyticsEventDataInterceptor();
        });

        it('should be exposed globally', function (done) {
            browser.driver.executeScript(function () {
                return window.GAWebTester.EventBuffer;
            })
            .then(
                function successCallback (EventBuffer) {
                    expect( EventBuffer ).toBeDefined();
                    expect( EventBuffer ).not.toBeNull();
                },
                function errorCallback (error) {
                    fail('Should not have received Error: ' + JSON.stringify(error));
                }
            )
            .then(done);
        });
    });


    describe('The wiring up of the Google Analytics Event Data interceptor', function () {
        beforeEach(function () {
            browser.get('/').then(
                function() { browser.driver.sleep(1000);
                });
            browser.driver.registerGoogleAnalyticsEventDataInterceptor();
            browser.driver.disableClicks();
        });

        describe('The "LastEvent" object', function () {
            it('should contain the pageview', function (done) {
                browser.driver.executeScript(function () {
                    return window.GAWebTester.getLastEvent();
                })
                .then(
                    function successCallback (EventBuffer) {
                        expect( EventBuffer ).toEqual( ['send', 'pageview'] );
                    },
                    function errorCallback (error) {
                        fail('Should not have received Error: ' + JSON.stringify(error));
                    }
                )
                .then(done);
            });
        });
    });
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions