Skip to content

Commit

Permalink
chore: keep up with latest ferrum (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
route authored Nov 4, 2023
1 parent d31b926 commit 3fa3151
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 42 deletions.
1 change: 0 additions & 1 deletion .rspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
--color
--fail-fast
--format=doc
--require spec_helper
27 changes: 27 additions & 0 deletions bin/rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

#
# This file was generated by Bundler.
#
# The application 'rake' is installed as part of a gem, and
# this file is here to facilitate running it.
#

ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)

bundle_binstub = File.expand_path("bundle", __dir__)

if File.file?(bundle_binstub)
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
load(bundle_binstub)
else
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
end
end

require "rubygems"
require "bundler/setup"

load Gem.bin_path("rake", "rake")
27 changes: 27 additions & 0 deletions bin/rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

#
# This file was generated by Bundler.
#
# The application 'rspec' is installed as part of a gem, and
# this file is here to facilitate running it.
#

ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)

bundle_binstub = File.expand_path("bundle", __dir__)

if File.file?(bundle_binstub)
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
load(bundle_binstub)
else
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
end
end

require "rubygems"
require "bundler/setup"

load Gem.bin_path("rspec-core", "rspec")
2 changes: 1 addition & 1 deletion cuprite.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ Gem::Specification.new do |s|
s.required_ruby_version = ">= 2.7.0"

s.add_runtime_dependency "capybara", "~> 3.0"
s.add_runtime_dependency "ferrum", "~> 0.13.0"
s.add_runtime_dependency "ferrum", "~> 0.14.0"
end
2 changes: 1 addition & 1 deletion lib/capybara/cuprite/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Driver < Capybara::Driver::Base

delegate %i[restart quit status_code timeout timeout= current_url title body
window_handles close_window switch_to_window within_window window_handle
back forward refresh wait_for_reload] => :browser
back forward refresh wait_for_reload viewport_size device_pixel_ratio] => :browser
alias html body
alias current_window_handle window_handle
alias go_back back
Expand Down
65 changes: 34 additions & 31 deletions spec/features/driver_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# frozen_string_literal: true

require "spec_helper"
require "image_size"
require "pdf/reader"
require "chunky_png"

module Capybara
module Cuprite
describe Driver do
let(:device_pixel_ratio) { @driver.device_pixel_ratio }

include Spec::Support::ExternalBrowser

around do |example|
Expand All @@ -24,11 +25,11 @@ def session_url(path)
end

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

file = "#{CUPRITE_ROOT}/spec/support/custom_chrome_called"
path = "#{CUPRITE_ROOT}/spec/support/custom_chrome"
file = "#{PROJECT_ROOT}/spec/support/custom_chrome_called"
path = "#{PROJECT_ROOT}/spec/support/custom_chrome"

driver = Driver.new(nil, browser_path: path)
driver.browser
Expand Down Expand Up @@ -146,16 +147,15 @@ def session_url(path)

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

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

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

create_screenshot file, full: true
File.open(file, "rb") do |f|
expect(ImageSize.new(f.read).size).to eq(
@driver.evaluate_script("[window.innerWidth, window.innerHeight]")
)
expect(ImageSize.new(f.read).size).to eq(@driver.viewport_size.map { |s| s * @driver.device_pixel_ratio })
end
end

Expand All @@ -183,7 +181,7 @@ def session_url(path)
return [rect.width, rect.height];
}();
JS
expect(ImageSize.new(f.read).size).to eq(size)
expect(ImageSize.new(f.read).size).to eq(size.map { |s| s * @driver.device_pixel_ratio })
end
end

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

File.open(file, "rb") do |f|
expect(ImageSize.new(f.read).size).to eq(
@driver.evaluate_script("[document.documentElement.scrollWidth, document.documentElement.scrollHeight]")
)
size = @driver.evaluate_script <<-JS
[document.documentElement.scrollWidth, document.documentElement.scrollHeight]
JS
expect(ImageSize.new(f.read).size).to eq(size.map { |s| s * device_pixel_ratio })
end
end

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

describe "#save_screenshot" do
let(:format) { :png }
let(:file) { "#{CUPRITE_ROOT}/spec/tmp/screenshot.#{format}" }
let(:file) { "#{PROJECT_ROOT}/spec/tmp/screenshot.#{format}" }

after do
FileUtils.rm_f("#{CUPRITE_ROOT}/spec/tmp/screenshot.pdf")
FileUtils.rm_f("#{CUPRITE_ROOT}/spec/tmp/screenshot.png")
FileUtils.rm_f("#{PROJECT_ROOT}/spec/tmp/screenshot.pdf")
FileUtils.rm_f("#{PROJECT_ROOT}/spec/tmp/screenshot.png")
end

def create_screenshot(file, *args)
Expand All @@ -242,7 +241,7 @@ def create_screenshot(file, *args)
end

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

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

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

begin
@session.visit("/")

@driver.save_screenshot(file, quality: 0) # ignored for png
@driver.save_screenshot(file2) # defaults to a quality of 75
@driver.save_screenshot(file3, quality: 100)
expect(File.size(file)).to be > File.size(file2) # png by defult is bigger
@driver.save_screenshot(file4, quality: 60) # ignored for png

expect(File.size(file)).to eq(File.size(file4))
expect(File.size(file2)).to be < File.size(file3)
ensure
FileUtils.rm_f([file2, file3])
FileUtils.rm_f([file2, file3, file4])
end
end

Expand Down Expand Up @@ -322,7 +325,7 @@ def create_screenshot(file, *args)
end

describe "#render_base64" do
let(:file) { "#{CUPRITE_ROOT}/spec/tmp/screenshot.#{format}" }
let(:file) { "#{PROJECT_ROOT}/spec/tmp/screenshot.#{format}" }

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

Expand Down Expand Up @@ -763,9 +766,9 @@ def create_screenshot(file, *args)
@session.visit("/cuprite/with_js")
responses = @driver.network_traffic.map(&:response)
resources_size = {
%r{/cuprite/jquery.min.js$} => File.size("#{CUPRITE_ROOT}/spec/support/public/jquery-1.11.3.min.js"),
%r{/cuprite/jquery-ui.min.js$} => File.size("#{CUPRITE_ROOT}/spec/support/public/jquery-ui-1.11.4.min.js"),
%r{/cuprite/test.js$} => File.size("#{CUPRITE_ROOT}/spec/support/public/test.js")
%r{/cuprite/jquery.min.js$} => File.size("#{PROJECT_ROOT}/spec/support/public/jquery-1.11.3.min.js"),
%r{/cuprite/jquery-ui.min.js$} => File.size("#{PROJECT_ROOT}/spec/support/public/jquery-ui-1.11.4.min.js"),
%r{/cuprite/test.js$} => File.size("#{PROJECT_ROOT}/spec/support/public/test.js")
}

resources_size.each do |resource, size|
Expand Down
2 changes: 0 additions & 2 deletions spec/features/session_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "spec_helper"

Capybara::SpecHelper.run_specs TestSessions::Cuprite, "Cuprite"

describe Capybara::Session do
Expand Down
13 changes: 7 additions & 6 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# frozen_string_literal: true

CUPRITE_ROOT = File.expand_path("..", __dir__)
$LOAD_PATH.unshift("#{CUPRITE_ROOT}/lib")
require "bundler/setup"
require "rspec"

PROJECT_ROOT = File.expand_path("..", __dir__)
%w[/lib /spec].each { |p| $LOAD_PATH.unshift(p) }

require "fileutils"
require "shellwords"
require "bundler/setup"
require "rspec"

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

Capybara.save_path = File.join(CUPRITE_ROOT, "spec", "tmp", "save_path")
Capybara.save_path = File.join(PROJECT_ROOT, "spec", "tmp", "save_path")

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

def remove_temporary_folders
FileUtils.rm_rf(File.join(CUPRITE_ROOT, "spec", "tmp", "screenshots"))
FileUtils.rm_rf(File.join(PROJECT_ROOT, "spec", "tmp", "screenshots"))
FileUtils.rm_rf(Capybara.save_path)
end
end

0 comments on commit 3fa3151

Please sign in to comment.