在CapserJS / SlimerJS中设置屏幕分辨率 [英] Set screen resolution in CapserJS/SlimerJS

查看:103
本文介绍了在CapserJS / SlimerJS中设置屏幕分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在CentOS 7.2上使用CasperJS 0.10.1 SlimerJS 1.1.3 Firefox 45尝试设置window.screen属性,如下面的代码通过网站的屏幕截图所示,仍然显示为640x480

Im using CasperJS 0.10.1 SlimerJS 1.1.3 Firefox 45 on CentOS 7.2 Im trying to set the window.screen properties as seen by the code below by the screenshot of the website still says 640x480

var casper = require('casper').create({ verbose: true, logLevel: 'debug' });
casper.on('page.initialized', function (page) {
    page.evaluate(function () { 
        (function() {
            window.screen = {
                width: 1600,
                height: 900
            };
        })
    });
});
casper
  .start()
  .thenOpen('http://www.whatismyscreenresolution.com/')  
  .wait(5000, function() { this.capture('/cas/_test_screenres.jpg',{top:0,left:0,width:1600,height:900}); })
  .run();


推荐答案

您可以设置视口大小:

casper.viewport(1600, 900);

甚至更多:

function on_init (page){
page.viewportSize = {width:1600,height:900}
page.evaluate(function (){
window.screen = {width:1600,height:900,availWidth:1600,availHeight:900};
window.innerWidth=1600;  window.innerHeight=900;   window.outerWidth=1600;  window.outerHeight=900;
window.navigator = {
plugins: {length: 2, 'Shockwave Flash': {name: 'Shockwave Flash', filename: '/usr/lib/flashplugin-nonfree/libflashplayer.so', description: 'Shockwave Flash 11.2 r202', version: '11.2.202.440'}},
mimeTypes: {length: 2, "application/x-shockwave-flash": {description: "Shockwave Flash", suffixes: "swf", type: "application/x-shockwave-flash", enabledPlugin: {name: 'Shockwave Flash', filename: '/usr/lib/flashplugin-nonfree/libflashplayer.so', description: 'Shockwave Flash 11.2 r202', version: '11.2.202.440'}}},
appCodeName: "Mozilla",
appName: "Netscape",
appVersion: "5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36",
cookieEnabled: 1,
languages: "en-US,en",
language: "en",
onLine: 1,
doNotTrack: null,
platform: "Linux x86_64",
product: "Gecko",
vendor: "Google Inc.",
vendorSub: "",
productSub: 20030107,
userAgent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36",
geolocation: {getCurrentPosition: function getCurrentPosition(){},watchPosition: function watchPosition(){},clearWatch: function clearWatch(){}},
javaEnabled: function javaEnabled(){return 0} };});};

casper.on('page.initialized', on_init);

这只是 navigator 对象的简单实现: 插件看起来可能更好,但通常不需要。

it's just simple realization of the navigator object: plugins could look better, but usually it's not needed.

这篇关于在CapserJS / SlimerJS中设置屏幕分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆