Skip to content

Commit 3fa3151

Browse files
authored
chore: keep up with latest ferrum (#250)
1 parent d31b926 commit 3fa3151

File tree

8 files changed

+97
-42
lines changed

8 files changed

+97
-42
lines changed

.rspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
--color
2-
--fail-fast
32
--format=doc
43
--require spec_helper

bin/rake

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
#
5+
# This file was generated by Bundler.
6+
#
7+
# The application 'rake' is installed as part of a gem, and
8+
# this file is here to facilitate running it.
9+
#
10+
11+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
12+
13+
bundle_binstub = File.expand_path("bundle", __dir__)
14+
15+
if File.file?(bundle_binstub)
16+
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
17+
load(bundle_binstub)
18+
else
19+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
20+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
21+
end
22+
end
23+
24+
require "rubygems"
25+
require "bundler/setup"
26+
27+
load Gem.bin_path("rake", "rake")

bin/rspec

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
#
5+
# This file was generated by Bundler.
6+
#
7+
# The application 'rspec' is installed as part of a gem, and
8+
# this file is here to facilitate running it.
9+
#
10+
11+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
12+
13+
bundle_binstub = File.expand_path("bundle", __dir__)
14+
15+
if File.file?(bundle_binstub)
16+
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
17+
load(bundle_binstub)
18+
else
19+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
20+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
21+
end
22+
end
23+
24+
require "rubygems"
25+
require "bundler/setup"
26+
27+
load Gem.bin_path("rspec-core", "rspec")

cuprite.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ Gem::Specification.new do |s|
2525
s.required_ruby_version = ">= 2.7.0"
2626

2727
s.add_runtime_dependency "capybara", "~> 3.0"
28-
s.add_runtime_dependency "ferrum", "~> 0.13.0"
28+
s.add_runtime_dependency "ferrum", "~> 0.14.0"
2929
end

lib/capybara/cuprite/driver.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Driver < Capybara::Driver::Base
1414

1515
delegate %i[restart quit status_code timeout timeout= current_url title body
1616
window_handles close_window switch_to_window within_window window_handle
17-
back forward refresh wait_for_reload] => :browser
17+
back forward refresh wait_for_reload viewport_size device_pixel_ratio] => :browser
1818
alias html body
1919
alias current_window_handle window_handle
2020
alias go_back back

spec/features/driver_spec.rb

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# frozen_string_literal: true
22

3-
require "spec_helper"
43
require "image_size"
54
require "pdf/reader"
65
require "chunky_png"
76

87
module Capybara
98
module Cuprite
109
describe Driver do
10+
let(:device_pixel_ratio) { @driver.device_pixel_ratio }
11+
1112
include Spec::Support::ExternalBrowser
1213

1314
around do |example|
@@ -24,11 +25,11 @@ def session_url(path)
2425
end
2526

2627
it "supports a custom path" do
27-
original_path = "#{CUPRITE_ROOT}/spec/support/chrome_path"
28+
original_path = "#{PROJECT_ROOT}/spec/support/chrome_path"
2829
File.write(original_path, @driver.browser.process.path)
2930

30-
file = "#{CUPRITE_ROOT}/spec/support/custom_chrome_called"
31-
path = "#{CUPRITE_ROOT}/spec/support/custom_chrome"
31+
file = "#{PROJECT_ROOT}/spec/support/custom_chrome_called"
32+
path = "#{PROJECT_ROOT}/spec/support/custom_chrome"
3233

3334
driver = Driver.new(nil, browser_path: path)
3435
driver.browser
@@ -146,16 +147,15 @@ def session_url(path)
146147

147148
create_screenshot file
148149
File.open(file, "rb") do |f|
149-
expect(ImageSize.new(f.read).size).to eq(
150-
@driver.evaluate_script("[window.innerWidth, window.innerHeight]")
151-
)
150+
expect(ImageSize.new(f.read).size).to eq(@driver.viewport_size.map { |s| s * @driver.device_pixel_ratio })
152151
end
153152

154153
create_screenshot file, full: true
155154
File.open(file, "rb") do |f|
156-
expect(ImageSize.new(f.read).size).to eq(
157-
@driver.evaluate_script("[document.documentElement.scrollWidth, document.documentElement.scrollHeight]")
158-
)
155+
size = @driver.evaluate_script <<-JS
156+
[document.documentElement.scrollWidth, document.documentElement.scrollHeight]
157+
JS
158+
expect(ImageSize.new(f.read).size).to eq(size.map { |s| s * device_pixel_ratio })
159159
end
160160
end
161161

@@ -164,9 +164,7 @@ def session_url(path)
164164

165165
create_screenshot file, full: true
166166
File.open(file, "rb") do |f|
167-
expect(ImageSize.new(f.read).size).to eq(
168-
@driver.evaluate_script("[window.innerWidth, window.innerHeight]")
169-
)
167+
expect(ImageSize.new(f.read).size).to eq(@driver.viewport_size.map { |s| s * @driver.device_pixel_ratio })
170168
end
171169
end
172170

@@ -183,7 +181,7 @@ def session_url(path)
183181
return [rect.width, rect.height];
184182
}();
185183
JS
186-
expect(ImageSize.new(f.read).size).to eq(size)
184+
expect(ImageSize.new(f.read).size).to eq(size.map { |s| s * @driver.device_pixel_ratio })
187185
end
188186
end
189187

@@ -194,9 +192,10 @@ def session_url(path)
194192
create_screenshot file, full: true, selector: "#penultimate"
195193

196194
File.open(file, "rb") do |f|
197-
expect(ImageSize.new(f.read).size).to eq(
198-
@driver.evaluate_script("[document.documentElement.scrollWidth, document.documentElement.scrollHeight]")
199-
)
195+
size = @driver.evaluate_script <<-JS
196+
[document.documentElement.scrollWidth, document.documentElement.scrollHeight]
197+
JS
198+
expect(ImageSize.new(f.read).size).to eq(size.map { |s| s * device_pixel_ratio })
200199
end
201200
end
202201

@@ -214,11 +213,11 @@ def session_url(path)
214213

215214
describe "#save_screenshot" do
216215
let(:format) { :png }
217-
let(:file) { "#{CUPRITE_ROOT}/spec/tmp/screenshot.#{format}" }
216+
let(:file) { "#{PROJECT_ROOT}/spec/tmp/screenshot.#{format}" }
218217

219218
after do
220-
FileUtils.rm_f("#{CUPRITE_ROOT}/spec/tmp/screenshot.pdf")
221-
FileUtils.rm_f("#{CUPRITE_ROOT}/spec/tmp/screenshot.png")
219+
FileUtils.rm_f("#{PROJECT_ROOT}/spec/tmp/screenshot.pdf")
220+
FileUtils.rm_f("#{PROJECT_ROOT}/spec/tmp/screenshot.png")
222221
end
223222

224223
def create_screenshot(file, *args)
@@ -242,7 +241,7 @@ def create_screenshot(file, *args)
242241
end
243242

244243
it "supports rendering the page to file without extension when format is specified" do
245-
file = "#{CUPRITE_ROOT}/spec/tmp/screenshot"
244+
file = "#{PROJECT_ROOT}/spec/tmp/screenshot"
246245
@session.visit("/")
247246

248247
@driver.save_screenshot(file, format: "jpg")
@@ -253,19 +252,23 @@ def create_screenshot(file, *args)
253252
end
254253

255254
it "supports rendering the page with different quality settings" do
256-
file2 = "#{CUPRITE_ROOT}/spec/tmp/screenshot2.jpeg"
257-
file3 = "#{CUPRITE_ROOT}/spec/tmp/screenshot3.jpeg"
258-
FileUtils.rm_f([file2, file3])
255+
file2 = "#{PROJECT_ROOT}/spec/tmp/screenshot2.jpeg"
256+
file3 = "#{PROJECT_ROOT}/spec/tmp/screenshot3.jpeg"
257+
file4 = "#{PROJECT_ROOT}/spec/tmp/screenshot4.#{format}"
258+
FileUtils.rm_f([file2, file3, file4])
259259

260260
begin
261261
@session.visit("/")
262+
262263
@driver.save_screenshot(file, quality: 0) # ignored for png
263264
@driver.save_screenshot(file2) # defaults to a quality of 75
264265
@driver.save_screenshot(file3, quality: 100)
265-
expect(File.size(file)).to be > File.size(file2) # png by defult is bigger
266+
@driver.save_screenshot(file4, quality: 60) # ignored for png
267+
268+
expect(File.size(file)).to eq(File.size(file4))
266269
expect(File.size(file2)).to be < File.size(file3)
267270
ensure
268-
FileUtils.rm_f([file2, file3])
271+
FileUtils.rm_f([file2, file3, file4])
269272
end
270273
end
271274

@@ -322,7 +325,7 @@ def create_screenshot(file, *args)
322325
end
323326

324327
describe "#render_base64" do
325-
let(:file) { "#{CUPRITE_ROOT}/spec/tmp/screenshot.#{format}" }
328+
let(:file) { "#{PROJECT_ROOT}/spec/tmp/screenshot.#{format}" }
326329

327330
def create_screenshot(file, *args)
328331
image = @driver.render_base64(format, *args)
@@ -644,7 +647,7 @@ def create_screenshot(file, *args)
644647
expect { @session.visit(url) }
645648
.to raise_error(
646649
Ferrum::StatusError,
647-
%(Request to #{url} failed to reach server, check DNS and server status)
650+
%r{Request to http://nope:\d+/ failed \(net::ERR_NAME_NOT_RESOLVED\)}
648651
)
649652
end
650653

@@ -763,9 +766,9 @@ def create_screenshot(file, *args)
763766
@session.visit("/cuprite/with_js")
764767
responses = @driver.network_traffic.map(&:response)
765768
resources_size = {
766-
%r{/cuprite/jquery.min.js$} => File.size("#{CUPRITE_ROOT}/spec/support/public/jquery-1.11.3.min.js"),
767-
%r{/cuprite/jquery-ui.min.js$} => File.size("#{CUPRITE_ROOT}/spec/support/public/jquery-ui-1.11.4.min.js"),
768-
%r{/cuprite/test.js$} => File.size("#{CUPRITE_ROOT}/spec/support/public/test.js")
769+
%r{/cuprite/jquery.min.js$} => File.size("#{PROJECT_ROOT}/spec/support/public/jquery-1.11.3.min.js"),
770+
%r{/cuprite/jquery-ui.min.js$} => File.size("#{PROJECT_ROOT}/spec/support/public/jquery-ui-1.11.4.min.js"),
771+
%r{/cuprite/test.js$} => File.size("#{PROJECT_ROOT}/spec/support/public/test.js")
769772
}
770773

771774
resources_size.each do |resource, size|

spec/features/session_spec.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# frozen_string_literal: true
22

3-
require "spec_helper"
4-
53
Capybara::SpecHelper.run_specs TestSessions::Cuprite, "Cuprite"
64

75
describe Capybara::Session do

spec/spec_helper.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# frozen_string_literal: true
22

3-
CUPRITE_ROOT = File.expand_path("..", __dir__)
4-
$LOAD_PATH.unshift("#{CUPRITE_ROOT}/lib")
3+
require "bundler/setup"
4+
require "rspec"
5+
6+
PROJECT_ROOT = File.expand_path("..", __dir__)
7+
%w[/lib /spec].each { |p| $LOAD_PATH.unshift(p) }
58

69
require "fileutils"
710
require "shellwords"
8-
require "bundler/setup"
9-
require "rspec"
1011

1112
require "capybara/spec/spec_helper"
1213
require "capybara/cuprite"
@@ -19,7 +20,7 @@
1920
puts `'#{Shellwords.escape(command.path)}' --version`
2021
puts ""
2122

22-
Capybara.save_path = File.join(CUPRITE_ROOT, "spec", "tmp", "save_path")
23+
Capybara.save_path = File.join(PROJECT_ROOT, "spec", "tmp", "save_path")
2324

2425
Capybara.register_driver(:cuprite) do |app|
2526
options = {}
@@ -131,7 +132,7 @@ def save_exception_log(browser, filename, line_number, timestamp)
131132
end
132133

133134
def remove_temporary_folders
134-
FileUtils.rm_rf(File.join(CUPRITE_ROOT, "spec", "tmp", "screenshots"))
135+
FileUtils.rm_rf(File.join(PROJECT_ROOT, "spec", "tmp", "screenshots"))
135136
FileUtils.rm_rf(Capybara.save_path)
136137
end
137138
end

0 commit comments

Comments
 (0)