diff --git a/lib/wraith/cli.rb b/lib/wraith/cli.rb index 5c744529..844faf5d 100644 --- a/lib/wraith/cli.rb +++ b/lib/wraith/cli.rb @@ -155,6 +155,7 @@ def latest(config) within_acceptable_limits do logger.info Wraith::Validate.new(config).validate("latest") reset_shots(config) + setup_folders(config) save_images(config, true) copy_base_images(config) crop_images(config) diff --git a/lib/wraith/helpers/utilities.rb b/lib/wraith/helpers/utilities.rb index 9b7218de..add0239e 100644 --- a/lib/wraith/helpers/utilities.rb +++ b/lib/wraith/helpers/utilities.rb @@ -33,6 +33,7 @@ def list_debug_information command_run = ARGV.join ' ' ruby_version = run_command_safely("ruby -v") || "Ruby not installed" phantomjs_version = run_command_safely("phantomjs --version") || "PhantomJS not installed" + chrome_version = run_command_safely('/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version') || "Google Chrome not installed" casperjs_version = run_command_safely("casperjs --version") || "CasperJS not installed" imagemagick_version = run_command_safely("convert -version") || "ImageMagick not installed" @@ -41,6 +42,7 @@ def list_debug_information logger.debug " Wraith version: #{wraith_version}" logger.debug " Ruby version: #{ruby_version}" logger.debug " ImageMagick: #{imagemagick_version}" + logger.debug " Chrome version: #{chrome_version}" logger.debug " PhantomJS version: #{phantomjs_version}" logger.debug " CasperJS version: #{casperjs_version}" # @TODO - add a SlimerJS equivalent diff --git a/lib/wraith/save_images.rb b/lib/wraith/save_images.rb index 500eb53e..8143df4f 100644 --- a/lib/wraith/save_images.rb +++ b/lib/wraith/save_images.rb @@ -63,6 +63,13 @@ def prepare_widths_for_cli(width) width end + def prepare_widths_for_chrome(width) + # prepare for the chrome. "30x40" => "30,40" + width = width.sub! 'x', ',' + width = prepare_widths_for_cli(width) + width + end + def run_command(command) output = [] IO.popen(command).each do |line| @@ -85,13 +92,50 @@ def parallel_task(jobs) end def construct_command(width, url, file_name, selector, global_before_capture, path_before_capture) - width = prepare_widths_for_cli(width) + selector = selector.gsub '#', '\#' # make sure id selectors aren't escaped in the CLI global_before_capture = convert_to_absolute global_before_capture path_before_capture = convert_to_absolute path_before_capture - - command_to_run = "#{meta.engine} #{wraith.phantomjs_options} '#{wraith.snap_file}' '#{url}' '#{width}' '#{file_name}' '#{selector}' '#{global_before_capture}' '#{path_before_capture}'" - logger.debug command_to_run + home_path = run_command_safely('pwd') + + if "#{meta.engine}" == "chrome" + # if width is in format 100X100,200X200 => resize_or_reload: 'resize' + # In this case, we need to generate chrome command twize + if width.include? "," + + # resize => double command + screenshots_width = width.split(',') + command_to_run = '' + screenshots_width.each_with_index do |screenshot_width, index| + if index != 0 + command_to_run += ' && ' + end + screenshot_width = prepare_widths_for_chrome(screenshot_width) + new_file_name = file_name.sub('MULTI', "#{screenshot_width}") + new_file_name = new_file_name.sub! ',', 'x' + target_folder = File.dirname(new_file_name) + basename = File.basename(new_file_name) + logger.warn target_folder + + command_to_run += 'cd ' + target_folder + ' && ' + # @TODO - this command will work only in Mac OS X + command_to_run += '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --headless --disable-gpu --screenshot --window-size=' + "#{screenshot_width} #{url}" + + command_to_run += " && mv screenshot.png " + basename + command_to_run += " && cd " + home_path + end + else + # reload => single command + screenshot_width = prepare_widths_for_chrome(width) + # @TODO - this command will work only in Mac OS X + command_to_run = '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --headless --disable-gpu --screenshot --window-size=' + "#{screenshot_width} #{url}" + command_to_run += " && mv screenshot.png " + file_name + end + else + width = prepare_widths_for_cli(width) + command_to_run = "#{meta.engine}" + " #{wraith.phantomjs_options} '#{wraith.snap_file}' '#{url}' '#{width}' '#{file_name}' '#{selector}' '#{global_before_capture}' '#{path_before_capture}'" + end + #logger.debug command_to_run command_to_run end