Search code examples
casperjsslimerjs

Set screen resolution in CapserJS/SlimerJS


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();

Solution

  • You can set the viewport size:

    casper.viewport(1600, 900);
    

    Or even more:

    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);
    

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