diff --git a/.ruby-version b/.ruby-version index 133fcc5f..cb506813 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -2.0.0-p247 \ No newline at end of file +2.0.0-p247 diff --git a/lib/wraith/javascript/_phantom__common.js b/lib/wraith/javascript/_phantom__common.js index 282f78e3..dfcb7f9c 100644 --- a/lib/wraith/javascript/_phantom__common.js +++ b/lib/wraith/javascript/_phantom__common.js @@ -15,6 +15,7 @@ module.exports = function (config) { selector = systemArgs[4], globalBeforeCaptureJS = systemArgs[5], pathBeforeCaptureJS = systemArgs[6], + timeoutMs = parseInt(systemArgs[7]), dimensionsProcessed = 0, currentDimensions; @@ -74,7 +75,7 @@ module.exports = function (config) { function resizeAndCaptureImage() { console.log('Snapping ' + url + ' at: ' + currentDimensions.viewportWidth + 'x' + currentDimensions.viewportHeight); resize(); - setTimeout(captureImage, 1000); // give page time to re-render properly + setTimeout(captureImage, timeoutMs); // give page time to re-render properly } function captureImage() { @@ -90,7 +91,7 @@ module.exports = function (config) { if (helper.takingMultipleScreenshots(dimensions) && dimensionsProcessed < dimensions.length) { currentDimensions = dimensions[dimensionsProcessed]; image_name = helper.replaceImageNameWithDimensions(image_name, currentDimensions); - setTimeout(resizeAndCaptureImage, 1000); + setTimeout(resizeAndCaptureImage, timoutMs); } else { // prevent CI from failing from 'Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL' errors @@ -109,7 +110,7 @@ module.exports = function (config) { // rendering, just in case the page kicks off another request if (current_requests < 1) { clearTimeout(final_timeout); - last_request_timeout = setTimeout(runSetupJavaScriptThenCaptureImage, 1000); + last_request_timeout = setTimeout(runSetupJavaScriptThenCaptureImage, timeoutMs); } // Sometimes, straggling requests never make it back, in which @@ -117,4 +118,4 @@ module.exports = function (config) { final_timeout = setTimeout(runSetupJavaScriptThenCaptureImage, 5000); } -} \ No newline at end of file +} diff --git a/lib/wraith/save_images.rb b/lib/wraith/save_images.rb index 3a3cef27..e5b4db81 100644 --- a/lib/wraith/save_images.rb +++ b/lib/wraith/save_images.rb @@ -63,7 +63,7 @@ def prepare_widths_for_cli(width) def capture_page_image(browser, url, width, file_name, selector, global_before_capture, path_before_capture) - command = "#{browser} #{wraith.phantomjs_options} '#{wraith.snap_file}' '#{url}' \"#{width}\" '#{file_name}' '#{selector}' '#{global_before_capture}' '#{path_before_capture}'" + command = "#{browser} #{wraith.phantomjs_options} '#{wraith.snap_file}' '#{url}' \"#{width}\" '#{file_name}' '#{selector}' '#{global_before_capture}' '#{path_before_capture}' #{wraith.timeout_ms}" # @TODO - uncomment the following line when we add a verbose mode # puts command @@ -80,7 +80,7 @@ def run_command(command) end def parallel_task(jobs) - Parallel.each(jobs, :in_threads => 8) do |_label, _path, width, url, filename, selector, global_before_capture, path_before_capture| + Parallel.each(jobs, :in_threads => wraith.num_threads) do |_label, _path, width, url, filename, selector, global_before_capture, path_before_capture| begin attempt_image_capture(width, url, filename, selector, global_before_capture, path_before_capture, 5) rescue => e diff --git a/lib/wraith/wraith.rb b/lib/wraith/wraith.rb index d792ef15..3499c5b5 100644 --- a/lib/wraith/wraith.rb +++ b/lib/wraith/wraith.rb @@ -59,6 +59,14 @@ def before_capture @config["before_capture"] ? convert_to_absolute(@config["before_capture"]) : "false" end + def num_threads + @config['num_threads'] ? @config['num_threads'] : 8 + end + + def timeout_ms + @config['timeout_ms'] ? @config['timeout_ms'] : 1000 + end + def widths @config["screen_widths"] end diff --git a/spec/config_spec.rb b/spec/config_spec.rb index a9473d8e..54210086 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -17,6 +17,27 @@ it "contains shot options" do expect(wraith.config).to include "directory" => "shots" end + + it 'returns default values for num_threads' do + expect(wraith.num_threads).to eq 8 + end + + it 'returns default values for timeout_ms' do + expect(wraith.timeout_ms).to eq 1000 + end + + context 'non-standard config values' do + let(:config) { YAML.load "browser: phantomjs\nnum_threads: 2\ntimeout_ms: 4000"} + let(:non_standard_wraith) { Wraith::Wraith.new( config, true) } + + it 'returns non standard values for num_threads if specified in config' do + expect(non_standard_wraith.num_threads).to eq 2 + end + + it 'returns non_standard values for timeout_ms if specified in config' do + expect(non_standard_wraith.timeout_ms).to eq 4000 + end + end end describe "When creating a wraith worker" do