Skip to content

Commit

Permalink
ref: Compatibility with latest Ferrum
Browse files Browse the repository at this point in the history
  • Loading branch information
route committed Jan 3, 2024
1 parent c5ff466 commit 25da084
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .rspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
--color
--format=doc
--format=progress
--require spec_helper
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
### Added

### Changed
- `@window_size` attribute is moved from Ferrum viewport size is still inherited [#253]
- `@window_size` attribute is moved from Ferrum, viewport size is still inherited [#253]

### Fixed
- Detect whether element is in the viewport and clickable before click [#251]
Expand Down
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ gem "byebug", "~> 11.1", platforms: %i[mri mingw x64_mingw]
gem "chunky_png", "~> 1.4"
gem "image_size", "~> 3.0"
gem "launchy", "~> 2.5"
gem "pdf-reader", "~> 2.5"
gem "pdf-reader", "~> 2.12"
gem "puma", ">= 5.6.7"
gem "rake", "~> 13.0"
gem "rspec", "~> 3.10"
gem "rubocop", "~> 1.22"
gem "rubocop-rake", require: false
gem "sinatra", "~> 2.1"
gem "sinatra", "~> 3.2"

gemspec
1 change: 1 addition & 0 deletions lib/capybara/cuprite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require "capybara/cuprite/driver"
require "capybara/cuprite/browser"
require "capybara/cuprite/page"
require "capybara/cuprite/options"
require "capybara/cuprite/node"
require "capybara/cuprite/errors"

Expand Down
44 changes: 25 additions & 19 deletions lib/capybara/cuprite/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,20 @@ class Browser < Ferrum::Browser
find_modal accept_confirm dismiss_confirm accept_prompt
dismiss_prompt reset_modals] => :page

attr_reader :url_blacklist, :url_whitelist, :window_size
alias url_blocklist url_blacklist
alias url_allowlist url_whitelist

def initialize(options = nil)
options ||= {}
@client = nil
self.url_blacklist = options[:url_blacklist]
self.url_whitelist = options[:url_whitelist]

super
@window_size = @options.window_size

@options.url_blacklist = prepare_wildcards(options&.dig(:url_blacklist))
@options.url_whitelist = prepare_wildcards(options&.dig(:url_whitelist))

@page = false
end

def timeout=(value)
def command(...)
super
@page.timeout = value unless @page.nil?
rescue Ferrum::DeadBrowserError
restart
raise
end

def page
Expand All @@ -39,7 +35,7 @@ def page

def reset
super
@window_size = options.window_size
@options.reset_window_size
@page = attach_page
end

Expand All @@ -49,19 +45,29 @@ def quit
end

def resize(**options)
@window_size = [options[:width], options[:height]]
@options.window_size = [options[:width], options[:height]]
super
end

def url_whitelist
@options.url_whitelist
end
alias url_allowlist url_whitelist

def url_whitelist=(patterns)
@url_whitelist = prepare_wildcards(patterns)
page.network.whitelist = @url_whitelist if @client && @url_whitelist.any?
@options.url_whitelist = prepare_wildcards(patterns)
page.network.whitelist = @options.url_whitelist if @client && @options.url_whitelist.any?
end
alias url_allowlist= url_whitelist=

def url_blacklist
@options.url_blacklist
end
alias url_blocklist url_blacklist

def url_blacklist=(patterns)
@url_blacklist = prepare_wildcards(patterns)
page.network.blacklist = @url_blacklist if @client && @url_blacklist.any?
@options.url_blacklist = prepare_wildcards(patterns)
page.network.blacklist = @options.url_blacklist if @client && @options.url_blacklist.any?
end
alias url_blocklist= url_blacklist=

Expand Down Expand Up @@ -234,7 +240,7 @@ def attach_page(target_id = nil)
return target.page if target.attached?

target.maybe_sleep_if_new_window
target.page = Page.new(target.id, self)
target.page = Page.new(client, context_id: target.context_id, target_id: target.id)
target.page
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/capybara/cuprite/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def switch_to_frame(locator)
def open_new_window
target = browser.default_context.create_target
target.maybe_sleep_if_new_window
target.page = Page.new(target.id, browser)
target.page = Page.new(browser.client, context_id: target.context_id, target_id: target.id)
target.page
end

Expand Down
14 changes: 14 additions & 0 deletions lib/capybara/cuprite/options.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module Ferrum
class Browser
class Options
attr_writer :window_size
attr_accessor :url_blacklist, :url_whitelist

def reset_window_size
@window_size = @options[:window_size]
end
end
end
end
16 changes: 8 additions & 8 deletions lib/capybara/cuprite/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Page < Ferrum::Page
current_url current_title body execution_id execution_id!
evaluate evaluate_on evaluate_async execute] => :active_frame

def initialize(*args)
def initialize(...)
@frame_stack = []
@accept_modal = []
@modal_messages = []
Expand Down Expand Up @@ -70,17 +70,17 @@ def dismiss_prompt

def find_modal(options)
start = Ferrum::Utils::ElapsedTime.monotonic_time
timeout = options.fetch(:wait, browser.timeout)
expect_text = options[:text]
expect_regexp = expect_text.is_a?(Regexp) ? expect_text : Regexp.escape(expect_text.to_s)
not_found_msg = "Unable to find modal dialog"
not_found_msg += " with #{expect_text}" if expect_text
wait = options.fetch(:wait, timeout)

begin
modal_text = @modal_messages.shift
raise Capybara::ModalNotFound if modal_text.nil? || (expect_text && !modal_text.match(expect_regexp))
rescue Capybara::ModalNotFound => e
raise e, not_found_msg if Ferrum::Utils::ElapsedTime.timeout?(start, timeout)
raise e, not_found_msg if Ferrum::Utils::ElapsedTime.timeout?(start, wait)

sleep(MODAL_WAIT)
retry
Expand Down Expand Up @@ -134,13 +134,13 @@ def title
def prepare_page
super

width, height = @browser.window_size
width, height = @options.window_size
resize(width: width, height: height)

if @browser.url_blacklist.any?
network.blacklist = @browser.url_blacklist
elsif @browser.url_whitelist.any?
network.whitelist = @browser.url_whitelist
if @options.url_blacklist.any?
network.blacklist = @options.url_blacklist
elsif @options.url_whitelist.any?
network.whitelist = @options.url_whitelist
end

on("Page.javascriptDialogOpening") do |params|
Expand Down

0 comments on commit 25da084

Please sign in to comment.