Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for setting user agent and other selenium options #563

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
sudo: false
sudo: required
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is needed to get chrome working on travis

addons:
apt:
packages:
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions lib/wraith/helpers/save_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ def compare_label
def engine
wraith.engine
end

def selenium_options
wraith.selenium_options
end
end
3 changes: 3 additions & 0 deletions lib/wraith/save_images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/wraith/wraith.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions spec/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
52 changes: 52 additions & 0 deletions spec/configs/test_config--chrome-mobile.yaml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 1 addition & 2 deletions spec/configs/test_config--chrome.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
11 changes: 11 additions & 0 deletions spec/save_images_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions templates/configs/capture.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions wraith.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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