How to disable external javascript execution / site requests in phantomjs

I am trying to run some tests on a website that is looking for problems.

For recording, I use phantomjs with ghostdriver in selenium from C #

Everything is working fine, but I would like to speed up the process. Checking the violin headlines, a lot of time is spent on external calls to external sites (facebook / twitter) for social plugins, which seem to seem good today: - \

I do not need to check these functions, so I'm trying to disable external calls on the site, which should speed up my tests.

Is there a way in phantom to get the effect that noscript / ghostery gives in firefox?

+4
source share
1 answer

To filter out invalid requests, you can use the onResourceRequested callback : this allows you to cancel unwanted URLs.

Here is a basic example for stackoverflow.

var system = require('system'); var page = require('webpage').create(); var domain = 'stackoverflow.com' var url = 'http://www.stackoverflow.com'; page.onResourceRequested = function (requestData, networkRequest) { if (requestData.url.indexOf('.js')===-1 && requestData.url.indexOf(domain) === -1) { networkRequest.abort(); console.log('aborted :'+ requestData.url) } }; page.onResourceReceived = function (response) { console.log('Response (#' + response.url + ', stage "' + response.stage + '"): '); }; if (system.args.length !== 1) { console.log("Usage: phantomjs filter.js url"); } else { page.open(url, function (status) { if (status = 'succeed') { console.log("status", status); phantom.exit(0); } }); } 

Please note that it is not recommended to interrupt js files, as this may cause a javascript error on your page.

Another way to speed up your test is to disable images using the argument --load-images=false

+2
source

Source: https://habr.com/ru/post/1491137/


All Articles