Skip to content

Commit

Permalink
Add headless Chrome support
Browse files Browse the repository at this point in the history
  • Loading branch information
saulirajala committed Jun 6, 2017
1 parent 62c9723 commit 988b705
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/wraith/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions lib/wraith/helpers/utilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
Expand Down
52 changes: 48 additions & 4 deletions lib/wraith/save_images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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

Expand Down

1 comment on commit 988b705

@saulirajala
Copy link
Owner Author

@saulirajala saulirajala commented on 988b705 Jun 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some notices about this solution:

  • Same config-file works as with PhantomJS. Just change the browser to chrome
  • Executing JavaScript-files before capture is not supported, so remember to disable all before_capture hooks
  • Tested with history-mode and with both options for resize_or_reload-setting
  • Works only with Mac

You can test this by copying the files to wherever your Wraith-files are. In my computer they are in /usr/local/lib/ruby/gems/2.3.0/gems/wraith-4.0.1

Please sign in to comment.