Prevent zombie.js loading from external resources only

I use zombie.js to load the page from the local express server during the test. Unfortunately, there is a script element that is called in Google Analytics. I would like to block this external script (gracefully) without stopping other (local) scripts from loading, if possible.

I know what { runScripts : false }is available with challenges browser.visit(). However, he refuses to load all and all the scripts on the page, and not just those who live on other hosts. Is it possible?

+5
source share
3 answers

You must use the resources object .

, , , . , Google Analytics :

browser.resources.mock('http://google.com/url/to/analytics.js',{});

, URL-, , URL-, .

+7

zombie 3.1, browser.resources.mock . nock:

var nock = require('nock')

nock('http://www.google-analytics.com')
  .get('/analytics.js')
  .times(Math.Infinity)
  .reply(200, '{}')

var Browser = require('zombie')
var browser = new Browser()
+8

, - ? "" , .

const Fetch = require('zombie/lib/fetch');

const ignoredResources = [
  'google-analytics.com'
];

browser.pipeline.addHandler((browser, request) => {
  let doAbort = false;

  ignoredResources.forEach(domain => {
    if (request.url.includes(domain)) {
      doAbort = true;
    }
  });

  if (doAbort) {
    return new Fetch.Response('', { status: 200 });
  }
});
0

All Articles