From 3414aa01d2cb0478b80695fff70f87ab448715b7 Mon Sep 17 00:00:00 2001 From: shmargum Date: Thu, 1 Mar 2018 16:51:06 -0500 Subject: [PATCH 1/3] add support for setting user agent and other selenium options --- README.md | 8 +++ lib/wraith/helpers/save_metadata.rb | 4 ++ lib/wraith/save_images.rb | 3 ++ lib/wraith/wraith.rb | 4 ++ spec/config_spec.rb | 14 ++++++ spec/configs/test_config--chrome-mobile.yaml | 52 ++++++++++++++++++++ spec/configs/test_config--chrome.yaml | 3 +- spec/save_images_spec.rb | 11 +++++ templates/configs/capture.yaml | 8 ++- 9 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 spec/configs/test_config--chrome-mobile.yaml diff --git a/README.md b/README.md index 6d2f484b..4b216bdd 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,14 @@ For instructions on how to install, set up and use Wraith and all of its feature A brief overview of how Wraith works is provided below. +## Configuration Options + +Some options that are not listed in the above documentation are explained here + +* `browser` also accepts 'chrome' as a valid engine +* `selenium_options` is an array of options to pass to selenium + * example: `--user-agent=MyUserAgent` + ## Wraith modes There are several ways in which Wraith can be used: diff --git a/lib/wraith/helpers/save_metadata.rb b/lib/wraith/helpers/save_metadata.rb index 55bd0ca5..907ab988 100644 --- a/lib/wraith/helpers/save_metadata.rb +++ b/lib/wraith/helpers/save_metadata.rb @@ -28,4 +28,8 @@ def compare_label def engine wraith.engine end + + def selenium_options + wraith.selenium_options + end end diff --git a/lib/wraith/save_images.rb b/lib/wraith/save_images.rb index bf76426b..8748d27f 100644 --- a/lib/wraith/save_images.rb +++ b/lib/wraith/save_images.rb @@ -101,6 +101,9 @@ def get_driver options.add_argument('--force-device-scale-factor') options.add_argument("--window-size=1200,1500") # resize later so we can reuse drivers options.add_argument("--hide-scrollbars") # hide scrollbars from screenshots + meta.selenium_options.each do |opt| + options.add_argument(opt) + end Selenium::WebDriver.for :chrome, options: options end end diff --git a/lib/wraith/wraith.rb b/lib/wraith/wraith.rb index dded44bf..30a383cb 100644 --- a/lib/wraith/wraith.rb +++ b/lib/wraith/wraith.rb @@ -82,6 +82,10 @@ def engine engine end + def selenium_options + @config["selenium_options"].to_a + end + def snap_file @config["snap_file"] ? convert_to_absolute(@config["snap_file"]) : snap_file_from_engine(engine) end diff --git a/spec/config_spec.rb b/spec/config_spec.rb index 14fdf3e4..c2fe5cb3 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -149,4 +149,18 @@ expect(wraith.resize).to eq false end end + + describe "chrome config" do + let(:config_name) { get_path_relative_to __FILE__, "./configs/test_config--chrome-mobile.yaml" } + let(:wraith) { Wraith::Wraith.new(config_name) } + + it "sets the engine correctly" do + expect(wraith.engine).to eq("chrome") + end + + it "creates selenium options" do + expect(wraith.selenium_options).to include("--user-agent=Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25") + end + end + end diff --git a/spec/configs/test_config--chrome-mobile.yaml b/spec/configs/test_config--chrome-mobile.yaml new file mode 100644 index 00000000..de909c63 --- /dev/null +++ b/spec/configs/test_config--chrome-mobile.yaml @@ -0,0 +1,52 @@ +########## +### NB: the paths in this YAML config are relative to the root of the Wraith directory, +### as `bundle exec rspec` is run from the root. +########## + +# Headless browser option +browser: "chrome" + +# Add a user agent if you want or any other selenium options +selenium_options: + - "--user-agent=Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25" + +# Type the name of the directory that shots will be stored in +directory: 'shots_chrome' + +# Add only 2 domains, key will act as a label +domains: + brazil: "http://m.huffpostbrasil.com/" + canada: "http://m.huffingtonpost.ca" + +#Type screen widths below, here are a couple of examples +screen_widths: + - 320 + - 480 + +#Type page URL paths below, here are a couple of examples +paths: + home: / + +# (optional) JavaScript file to execute before taking screenshot of every path. Default: nil + # before_capture: 'javascript/interact--chrome.js' + # before_capture: 'javascript/wait--chrome.js' + +#Amount of fuzz ImageMagick will use +fuzz: '20%' + +# (optional) The maximum acceptable level of difference (in %) between two images before Wraith reports a failure. Default: 0 +threshold: 5 + +# (optional) Specify the template (and generated thumbnail sizes) for the gallery output. +gallery: + template: 'slideshow_template' # Examples: 'basic_template' (default), 'slideshow_template' + thumb_width: 200 + thumb_height: 200 + +# (optional) Choose which results are displayed in the gallery, and in what order. Default: alphanumeric +# Options: +# alphanumeric - all paths (with or without a difference) are shown, sorted by path +# diffs_first - all paths (with or without a difference) are shown, sorted by difference size (largest first) +# diffs_only - only paths with a difference are shown, sorted by difference size (largest first) +# Note: different screen widths are always grouped together. +mode: diffs_first diff --git a/spec/configs/test_config--chrome.yaml b/spec/configs/test_config--chrome.yaml index c939d9d8..ab79b234 100644 --- a/spec/configs/test_config--chrome.yaml +++ b/spec/configs/test_config--chrome.yaml @@ -4,8 +4,7 @@ ########## #Headless browser option -browser: - phantomjs: "chrome" +browser: "chrome" # Type the name of the directory that shots will be stored in directory: 'shots_chrome' diff --git a/spec/save_images_spec.rb b/spec/save_images_spec.rb index 5e50e1a2..7f429908 100644 --- a/spec/save_images_spec.rb +++ b/spec/save_images_spec.rb @@ -4,17 +4,22 @@ describe Wraith do let(:config_name) { get_path_relative_to __FILE__, "./configs/test_config--phantom.yaml" } let(:config_chrome) { get_path_relative_to __FILE__, "./configs/test_config--chrome.yaml" } + let(:config_chrome_mobile) { get_path_relative_to __FILE__, "./configs/test_config--chrome-mobile.yaml" } let(:test_url1) { "http://www.bbc.com/afrique" } let(:test_url2) { "http://www.bbc.com/russian" } + let(:test_hp_url1) { "http://m.huffingtonpost.gr" } + let(:test_hp_url2) { "http://m.huffingtonpost.ca" } let(:test_image1) { "shots/test/test1.png" } let(:test_image_chrome) { "shots_chrome/test/test_chrome.png" } let(:test_image_chrome_selector) { "shots_chrome/test/test_chrome_selector.png" } + let(:test_image_chrome_mobile) { "shots_chrome/test/test_chrome_mobile.png" } let(:test_image2) { "shots/test/test(2).png" } let(:diff_image) { "shots/test/test_diff.png" } let(:data_txt) { "shots/test/test.txt" } let(:selector) { "" } let(:saving) { Wraith::SaveImages.new(config_name) } let(:saving_chrome) { Wraith::SaveImages.new(config_chrome) } + let(:saving_chrome_mobile) { Wraith::SaveImages.new(config_chrome_mobile) } let(:wraith) { Wraith::Wraith.new(config_name) } before(:each) do @@ -44,6 +49,12 @@ expect(image_size_chrome_selector[0]).to eq 673 expect(image_size_chrome_selector[1]).to eq 40 end + + it "loads mobile user agent and captures page" do + capture_image = saving_chrome_mobile.capture_image_selenium(320, test_hp_url1, test_image_chrome_mobile, selector, false, false) + image_size_chrome_mobile = ImageSize.path(test_image_chrome_mobile).size + expect(image_size_chrome_mobile[0]).to eq(320) + end end describe "When comparing images" do diff --git a/templates/configs/capture.yaml b/templates/configs/capture.yaml index 5c177e54..5eeaa576 100644 --- a/templates/configs/capture.yaml +++ b/templates/configs/capture.yaml @@ -12,8 +12,12 @@ ############################################################## ############################################################## -# (required) The engine to run Wraith with. Examples: 'phantomjs', 'casperjs', 'slimerjs' -browser: "phantomjs" +# (required) The engine to run Wraith with. Examples: 'phantomjs', 'casperjs', 'slimerjs', 'chrome' +browser: "chrome" + +# Add a user agent if you want or any other selenium options +selenium_options: + - "--user-agent=Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25" # (required) The domains to take screenshots of. domains: From acee724a12f3abfbc8e8a0abb41bb62f8f6d1f6e Mon Sep 17 00:00:00 2001 From: shmargum Date: Thu, 1 Mar 2018 17:46:10 -0500 Subject: [PATCH 2/3] bump selenium and chromedriver versions --- wraith.gemspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wraith.gemspec b/wraith.gemspec index dba2c1ec..951ab10a 100644 --- a/wraith.gemspec +++ b/wraith.gemspec @@ -30,6 +30,6 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency 'log4r' spec.add_runtime_dependency 'thor' spec.add_runtime_dependency 'parallel' - spec.add_runtime_dependency 'selenium-webdriver', "~> 3.5" - spec.add_runtime_dependency 'chromedriver-helper', "~> 1.1" + spec.add_runtime_dependency 'selenium-webdriver', "~> 3.9" + spec.add_runtime_dependency 'chromedriver-helper', "~> 1.2" end From 632cc0352c51445c7220109df024c872cff63477 Mon Sep 17 00:00:00 2001 From: shmargum Date: Thu, 1 Mar 2018 18:00:25 -0500 Subject: [PATCH 3/3] require sudo --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 66d760d7..15a359af 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,4 @@ -sudo: false +sudo: required addons: apt: packages: