Spy dataLayer from Google Analytics with Puppeteer and Mocha / Sinon (or similar) -


so, trying make automatic tests on google analytics calls headless chromes + puppeteer + mocha + sinon can't manage read datalayer value sinon spy.

this have far. window undefined. test class proxy pass puppeteer calls inner browser.

const { test } = require('../browser'); const sinon = require('sinon'); const datalayername = 'datalayer';  const assert = sinon.assert;  describe('tests analytics', () => {     let spy;      it('find home analytics', test(async (browser, opts) => {         const page = await browser.newpage();         await page.goto(`${opts.appurl}`);          spy = sinon.spy(window.datalayer, 'push');         assert.called(spy);         assert.calledwith(spy, [           'fail me',         ]);         spy.restore();     })); }); 

this browser class:

const puppeteer = require('puppeteer');  /**  * thin wrapper use singleton of  * browser puppeteer creates  */ class browser {   setup(done) {     const puppeteeropts = this.options && this.options.puppeteer ?       this.options.puppeteer :       {};      puppeteer.launch(puppeteeropts).then(async (b) => {       this.setbrowser(b);       done();     });   }    setbrowser(b) {     this.browser = b;     const oldnewpage = this.browser.newpage.bind(this.browser);      this.browser.newpage = async function () {       const page = await oldnewpage();       this.lastpage = page;        return page;     };   }    setoptions(opts) {     this.options = opts;   }    test(promise) {     return (done) => {       promise(this.browser, this.options)         .then(() => done()).catch(done);     };   } }  /*  * create new browser , use proxy pass  * puppeteer calls inner browser  */ module.exports = new proxy(new browser(), {   get(target, name) {     return name in target ? target[name].bind(target) : target.browser[name];   }, }); 


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -