为多个网站拍摄多个视口屏幕截图

时间:2014-02-24 23:51:39

标签: screenshot phantomjs casperjs

好的,所以我试图为多个网站运行多个屏幕截图!我有一个多个网站的屏幕截图,我也可以为一个网站获得多个Viewport屏幕截图,但我有34个网站,我想这样做!所以有人知道用casperjs做这个的方法吗?这是我认为可以工作的代码,但只有第一个网址,然后才结束!

var casper = require("casper").create();

var screenshotUrls = ["http://www.google.com", "http://www.yahoo.com", "http://www.espn.com"];

var screenshotPaths = ["google.com", "yahoo.com", "espn.com"];
    screenshotNow = new Date(),
    screenshotDateTime = screenshotNow.getFullYear() + pad(screenshotNow.getMonth() + 1) + pad(screenshotNow.getDate()),
    viewports = [
      {
        'name': 'smartphone-portrait',
        'viewport': {width: 320, height: 480}
      },
      {
        'name': 'smartphone-landscape',
        'viewport': {width: 480, height: 320}
      },
      {
        'name': 'tablet-portrait',
        'viewport': {width: 768, height: 1024}
      },
      {
        'name': 'tablet-landscape',
        'viewport': {width: 1024, height: 768}
      },
      {
        'name': 'desktop-standard',
        'viewport': {width: 1280, height: 1024}
      }
    ];
i = 0; 
//casper.start(); 
casper.start(screenshotUrls[i], function(self, screenshotUrl) {
  this.echo('Current location is ' + this.getCurrentUrl(), 'info');
});

casper.each(viewports, function(casper, viewport) {
  this.then(function() {
    this.viewport(viewport.viewport.width, viewport.viewport.height);
  });
  this.thenOpen(screenshotUrls[i], function() {
    this.wait(5000);
  });
  this.then(function(){
    this.echo('Screenshot for ' + viewport.name + ' (' + viewport.viewport.width + 'x' + viewport.viewport.height + ')', 'info');
    this.capture('screenshots/' + screenshotPaths[i] + '/' + screenshotDateTime + '/' + viewport.name + '-' + viewport.viewport.width + 'x' + viewport.viewport.height + '.png', {
        top: 0,
        left: 0,
        width: viewport.viewport.width,
        height: viewport.viewport.height
    });
  });
});

casper.run();

function pad(number) {
  var r = String(number);
  if ( r.length === 1 ) {
    r = '0' + r;
  }
  return r;
}

1 个答案:

答案 0 :(得分:1)

以下是工作版本:

    var casper = require("casper").create();

    var screenshotUrls = ["http://www.google.com", "http://www.yahoo.com", "http://www.espn.com"];

    var screenshotPaths = ["google.com", "yahoo.com", "espn.com"];
        screenshotNow = new Date(),
        screenshotDateTime = screenshotNow.getFullYear() + pad(screenshotNow.getMonth() + 1) + pad(screenshotNow.getDate()),
        viewports = [
          {
            'name': 'smartphone-portrait',
            'viewport': {width: 320, height: 480}
          },
          {
            'name': 'smartphone-landscape',
            'viewport': {width: 480, height: 320}
          },
          {
            'name': 'tablet-portrait',
            'viewport': {width: 768, height: 1024}
          },
          {
            'name': 'tablet-landscape',
            'viewport': {width: 1024, height: 768}
          },
          {
            'name': 'desktop-standard',
            'viewport': {width: 1280, height: 1024}
          }
        ];
    i = -1;
    casper.start().eachThen(screenshotUrls, function(response) {

    this.each(viewports, function(casper, viewport) {
      this.then(function() {
        this.viewport(viewport.viewport.width, viewport.viewport.height);
      });
      this.thenOpen(response.data, function() {
        this.wait(5000);
      });
      this.then(function(){
        this.echo('Screenshot for '+ screenshotPaths[i]  + viewport.name + ' (' + viewport.viewport.width + 'x' + viewport.viewport.height + ')', 'info');
        this.capture('screenshots/' + screenshotPaths[i] + '/' + screenshotDateTime + '/' + viewport.name + '-' + viewport.viewport.width + 'x' + viewport.viewport.height + '.png', {
            top: 0,
            left: 0,
            width: viewport.viewport.width,
            height: viewport.viewport.height
        });
      });
    });
    ++i;
    });
    casper.run();

    function pad(number) {
      var r = String(number);
      if ( r.length === 1 ) {
        r = '0' + r;
      }
      return r;
    }