Skip to content

Commit f4859e5

Browse files
authored
Merge pull request #578 from BBC-News/chromification
Chromification
2 parents 0fcd526 + bf07cfd commit f4859e5

18 files changed

+97
-76
lines changed

assets/invalid.jpg

-17.8 KB
Binary file not shown.

assets/invalid1.jpg

2.43 MB
Loading

assets/invalid2.jpg

2.2 MB
Loading

lib/wraith/compare_images.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def percentage(img_size, px_value, info)
3131
end
3232

3333
def compare_task(base, compare, output, info)
34-
cmdline = "compare -dissimilarity-threshold 1 -fuzz #{wraith.fuzz} -metric AE -highlight-color #{wraith.highlight_color} #{base} #{compare.shellescape} #{output}"
34+
cmdline = "compare -fuzz #{wraith.fuzz} -metric AE -highlight-color #{wraith.highlight_color} #{base} #{compare.shellescape} #{output}"
3535
px_value = Open3.popen3(cmdline) { |_stdin, _stdout, stderr, _wait_thr| stderr.read }.to_f
3636
begin
3737
img_size = ImageSize.path(output).size.inject(:*)

lib/wraith/helpers/save_metadata.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "wraith"
2+
require "fileutils"
23

34
class SaveMetadata
45
attr_reader :wraith, :history
@@ -14,6 +15,8 @@ def history_label
1415

1516
def file_names(width, label, domain_label)
1617
width = "MULTI" if width.is_a? Array
18+
19+
FileUtils::mkdir_p "#{wraith.directory}/#{label}" # ensure the directory exists
1720
"#{wraith.directory}/#{label}/#{width}_#{engine}_#{domain_label}.png"
1821
end
1922

lib/wraith/save_images.rb

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ def define_individual_job(label, settings, width)
5353
compare_file_name = meta.file_names(width, label, meta.compare_label)
5454

5555
jobs = []
56-
jobs << [label, settings.path, prepare_widths_for_cli(width), settings.base_url, base_file_name, settings.selector, wraith.before_capture, settings.before_capture]
57-
jobs << [label, settings.path, prepare_widths_for_cli(width), settings.compare_url, compare_file_name, settings.selector, wraith.before_capture, settings.before_capture] unless settings.compare_url.nil?
56+
jobs << [label, settings.path, prepare_widths_for_cli(width), settings.base_url, base_file_name, settings.selector, wraith.before_capture, settings.before_capture, 'invalid1.jpg']
57+
jobs << [label, settings.path, prepare_widths_for_cli(width), settings.compare_url, compare_file_name, settings.selector, wraith.before_capture, settings.before_capture, 'invalid2.jpg'] unless settings.compare_url.nil?
5858

5959
jobs
6060
end
@@ -76,7 +76,7 @@ def run_command(command)
7676
end
7777

7878
def parallel_task(jobs)
79-
Parallel.each(jobs, :in_threads => wraith.save_image_threads) do |_label, _path, width, url, filename, selector, global_before_capture, path_before_capture|
79+
Parallel.each(jobs, :in_threads => wraith.threads) do |_label, _path, width, url, filename, selector, global_before_capture, path_before_capture|
8080
begin
8181
if meta.engine == "chrome"
8282
capture_image_selenium(width, url, filename, selector, global_before_capture, path_before_capture)
@@ -85,8 +85,8 @@ def parallel_task(jobs)
8585
attempt_image_capture(command, filename)
8686
end
8787
rescue => e
88-
logger.error e
89-
create_invalid_image(filename, width)
88+
logger.error "#{e}\n URL = #{url}"
89+
create_invalid_image(filename, width, invalid_image_name)
9090
end
9191
end
9292
end
@@ -96,12 +96,16 @@ def get_driver
9696
case meta.engine
9797
when "chrome"
9898
options = Selenium::WebDriver::Chrome::Options.new
99-
options.add_argument('--disable-gpu')
100-
options.add_argument('--headless')
101-
options.add_argument('--device-scale-factor=1') # have to change cropping for 2x. also this is faster
102-
options.add_argument('--force-device-scale-factor')
103-
options.add_argument("--window-size=1200,1500") # resize later so we can reuse drivers
104-
options.add_argument("--hide-scrollbars") # hide scrollbars from screenshots
99+
[
100+
'disable-gpu',
101+
'headless',
102+
'no-sandbox',
103+
'device-scale-factor=1',
104+
'force-device-scale-factor',
105+
'window-size=1200,1500',
106+
'hide-scrollbars',
107+
'ignore-certificate-errors'
108+
].each { |arg| options.add_argument("--#{arg}") }
105109
Selenium::WebDriver.for :chrome, options: options
106110
end
107111
end
@@ -111,7 +115,7 @@ def resize_to_fit_page driver
111115
width = driver.execute_script("return Math.max(document.body.scrollWidth, document.body.offsetWidth, document.documentElement.clientWidth, document.documentElement.scrollWidth, document.documentElement.offsetWidth);")
112116
height = driver.execute_script("return Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight);")
113117
driver.manage.window.resize_to(width, height)
114-
end
118+
end
115119

116120
# crop an image around the coordinates of an element
117121
def crop_selector driver, selector, image_location
@@ -123,16 +127,25 @@ def crop_selector driver, selector, image_location
123127

124128
def capture_image_selenium(screen_sizes, url, file_name, selector, global_before_capture, path_before_capture)
125129
driver = get_driver
130+
driver.manage.timeouts.implicit_wait = 10;
126131
screen_sizes.to_s.split(",").each do |screen_size|
127-
width, height = screen_size.split("x")
128-
new_file_name = file_name.sub('MULTI', screen_size)
129-
driver.manage.window.resize_to(width, height || 1500)
130-
driver.navigate.to url
131-
driver.execute_async_script(File.read(global_before_capture)) if global_before_capture
132-
driver.execute_async_script(File.read(path_before_capture)) if path_before_capture
133-
resize_to_fit_page(driver) unless height
134-
driver.save_screenshot(new_file_name)
135-
crop_selector(driver, selector, new_file_name) if selector && selector.length > 0
132+
for attempt in 1..3 do
133+
begin
134+
width, height = screen_size.split("x")
135+
new_file_name = file_name.sub('MULTI', screen_size)
136+
driver.manage.window.resize_to(width, height || 1500)
137+
driver.navigate.to url
138+
driver.manage.timeouts.implicit_wait = wraith.settle
139+
driver.execute_script(File.read(global_before_capture)) if global_before_capture
140+
driver.execute_script(File.read(path_before_capture)) if path_before_capture
141+
resize_to_fit_page(driver) unless height
142+
driver.save_screenshot(new_file_name)
143+
crop_selector(driver, selector, new_file_name) if selector && selector.length > 0
144+
break
145+
rescue Net::ReadTimeout => e
146+
logger.error "Got #{e} on attempt #{attempt} at screen size #{screensize}. URL = #{url}"
147+
end
148+
end
136149
end
137150
driver.quit
138151
end
@@ -164,9 +177,9 @@ def image_was_created(filename)
164177
wraith.resize or File.exist? filename
165178
end
166179

167-
def create_invalid_image(filename, width)
180+
def create_invalid_image(filename, width, invalid_image_name)
168181
logger.warn "Using fallback image instead"
169-
invalid = File.expand_path("../../assets/invalid.jpg", File.dirname(__FILE__))
182+
invalid = File.expand_path("../../assets/#{invalid_image_name}", File.dirname(__FILE__))
170183
FileUtils.cp invalid, filename
171184

172185
set_image_width(filename, width)

lib/wraith/wraith.rb

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def directory
7272
end
7373

7474
def history_dir
75-
@config["history_dir"] || false
75+
@config.fetch('history_dir', false)
7676
end
7777

7878
def engine
@@ -109,7 +109,7 @@ def widths
109109

110110
def resize
111111
# @TODO make this default to true, once it's been tested a bit more thoroughly
112-
@config["resize_or_reload"] ? (@config["resize_or_reload"] == "resize") : false
112+
@config.fetch('resize_or_reload', 'reload') == "resize"
113113
end
114114

115115
def domains
@@ -132,8 +132,16 @@ def comp_domain_label
132132
domains.keys[1]
133133
end
134134

135+
def settle
136+
@config.fetch('settle', 10)
137+
end
138+
139+
def threads
140+
@config.fetch('threads', '8').to_i
141+
end
142+
135143
def spider_file
136-
@config["spider_file"] ? @config["spider_file"] : "spider.txt"
144+
@config.fetch('spider_file', 'spider.txt')
137145
end
138146

139147
def spider_days
@@ -157,7 +165,7 @@ def fuzz
157165
end
158166

159167
def highlight_color
160-
@config["highlight_color"] ? @config["highlight_color"] : "blue"
168+
@config.fetch('highlight_color', 'blue')
161169
end
162170

163171
def mode
@@ -169,50 +177,31 @@ def mode
169177
end
170178

171179
def threshold
172-
@config["threshold"] ? @config["threshold"] : 0
180+
@config.fetch('threshold', 0)
173181
end
174182

175183
def gallery_template
176-
default = "basic_template"
177-
if @config["gallery"].nil?
178-
default
179-
else
180-
@config["gallery"]["template"] || default
181-
end
184+
@config.fetch('gallery', {}).fetch('template', 'basic_template')
182185
end
183186

184187
def thumb_height
185-
default = 200
186-
if @config["gallery"].nil?
187-
default
188-
else
189-
@config["gallery"]["thumb_height"] || default
190-
end
188+
@config.fetch('gallery', {}).fetch('thumb_height', 200)
191189
end
192190

193191
def thumb_width
194-
default = 200
195-
if @config["gallery"].nil?
196-
default
197-
else
198-
@config["gallery"]["thumb_width"] || default
199-
end
192+
@config.fetch('gallery', {}).fetch('thumb_width', 200)
200193
end
201194

202195
def phantomjs_options
203196
@config["phantomjs_options"]
204197
end
205198

206199
def imports
207-
@config['imports'] || false
200+
@config.fetch('imports', false)
208201
end
209202

210203
def verbose
211204
# @TODO - also add a `--verbose` CLI flag which overrides whatever you have set in the config
212-
@config["verbose"] || false
213-
end
214-
215-
def save_image_threads
216-
(@config["save_image_threads"] || 8).to_i
205+
@config.fetch('verbose', false)
217206
end
218207
end

spec/_helpers.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
require "rspec"
22
require "./lib/wraith/cli"
3-
require "pry"
43

54
def create_diff_image
65
capture_image = saving.construct_command(320, test_url1, test_image1, selector, false, false)

spec/config_spec.rb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,26 @@
1717
expect(wraith.config).to include "directory" => "shots"
1818
end
1919

20+
it 'returns default values for threads' do
21+
expect(wraith.threads).to eq 8
22+
end
23+
it 'returns default values for settle' do
24+
expect(wraith.settle).to eq 10
25+
end
26+
27+
context 'non-standard config values' do
28+
let(:config) { YAML.load "browser: phantomjs\nthreads: 2\nsettle: 5"}
29+
let(:non_standard_wraith) { Wraith::Wraith.new( config, { yaml_passed: true }) }
30+
31+
it 'returns overridden value when threads is specified in config' do
32+
expect(non_standard_wraith.threads).to eq 2
33+
end
34+
35+
it 'returns overridden value when settle is specified in config' do
36+
expect(non_standard_wraith.settle).to eq 5
37+
end
38+
end
39+
2040
it "should be able to import other configs" do
2141
config_name = get_path_relative_to __FILE__, "./configs/test_config--imports.yaml"
2242
wraith = Wraith::Wraith.new(config_name)
@@ -76,10 +96,6 @@
7696
it "include compare label" do
7797
expect(wraith.paths).to eq("home" => "/", "uk_index" => "/uk")
7898
end
79-
80-
it "include save image threads" do
81-
expect(wraith.save_image_threads).to eq(7)
82-
end
8399
end
84100

85101
describe "different ways of initialising browser engine" do

spec/configs/test_config--casper.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ paths:
3030
fuzz: '20%'
3131

3232
# Number of threads used when saving images
33-
save_image_threads: 7
33+
threads: 7

0 commit comments

Comments
 (0)