4

I am trying to run some tests on a website looking for issues.

For the record, I am using phantomjs with the ghostdriver in selenium from C#

Everything is working fine, but I would like to speed things up. Checking on the headers in fiddler, a lot of time is spent on external calls to external sites (facebook / twitter) for the social plugins all sites seem to think is a good idea these days :-\

I am not required to test these functions, so am trying to disable external site calls, which should speed my tests up some what.

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

1
  • Do you have access to the site code? If so, attack it from another angle - disable those plugins on your testing environment. Commented Jul 12, 2013 at 22:29

1 Answer 1

2

To filter invalid requests, you could use the onResourceRequested callback : this allow you to abort 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);
        }
    });
}

Note that it's not recommended to abort js files, as this can cause a javascript error on you page.

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I will check this out and see if I can figure out how to do it in c#
Here is a c# example for running a script: stackoverflow.com/a/26484685/289506

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.