【问题标题】:How to disable external javascript execution / site requests in phantomjs如何在 phantomjs 中禁用外部 javascript 执行/站点请求
【发布时间】:2013-07-12 14:37:08
【问题描述】:

我正在尝试在网站上运行一些测试以寻找问题。

为了记录,我在 C# 的 selenium 中使用 phantomjs 和 ghostdriver

一切正常,但我想加快速度。检查提琴手中的标题,大量时间花在对外部网站(facebook / twitter)的外部调用上,以获取所有网站这些天似乎认为是个好主意的社交插件:-\

我不需要测试这些功能,所以我尝试禁用外部站点调用,这应该会加快我的测试速度。

有没有办法在phantom中获得noscript / ghostery在firefox中给出的效果?

【问题讨论】:

  • 您可以访问网站代码吗?如果是这样,请从另一个角度攻击它 - 在您的测试环境中禁用这些插件。

标签: c# selenium-webdriver phantomjs noscript ghostdriver


【解决方案1】:

要过滤无效请求,您可以使用 onResourceRequested callback :这允许您中止不需要的 url。

这是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);
        }
    });
}

请注意,不建议中止 js 文件,因为这可能会导致您的页面出现 javascript 错误。

另一种加快测试速度的方法是使用参数--load-images=false 禁用图像

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-03-27
  • 2012-12-10
  • 2018-06-02
  • 1970-01-01
  • 1970-01-01
  • 2022-08-19
  • 2020-05-11
  • 1970-01-01
相关资源
最近更新 更多