Skip to content

Commit 7f09274

Browse files
authored
ref: Compatibility with latest Ferrum (#254)
* ref: Compatibility with latest Ferrum * chore: add CHANGELOG entry * chore: update ferrum * chore: get rid of websocket-driver-0.6.x * chore: fix client * fix: CreateArtifact: Received non-retryable error: Failed request: (409) Conflict: an artifact with this name already exists on the workflow run * chore: skip broken capybara test * chore: check artifact * fix: artifacts * fix: build * chore: update CHANGELOG
1 parent c5ff466 commit 7f09274

File tree

13 files changed

+68
-59
lines changed

13 files changed

+68
-59
lines changed

.github/gemfiles/websocket-driver-0.6.x.gemfile

Lines changed: 0 additions & 11 deletions
This file was deleted.

.github/gemfiles/websocket-driver-0.7.x.gemfile

Lines changed: 0 additions & 11 deletions
This file was deleted.

.github/workflows/tests.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@ jobs:
1111
strategy:
1212
fail-fast: false
1313
matrix:
14-
gemfile: [websocket-driver-0.6.x, websocket-driver-0.7.x]
1514
ruby: [2.7, "3.0", 3.1, 3.2, 3.3]
1615
runs-on: ubuntu-latest
1716
env:
1817
FERRUM_PROCESS_TIMEOUT: 25
1918
FERRUM_DEFAULT_TIMEOUT: 15
20-
BUNDLE_GEMFILE: .github/gemfiles/${{ matrix.gemfile }}.gemfile
2119
steps:
2220
- name: Checkout code
2321
uses: actions/checkout@v4
@@ -42,5 +40,5 @@ jobs:
4240
uses: actions/upload-artifact@v4
4341
if: ${{ failure() }}
4442
with:
45-
name: footprints
43+
name: artifacts-ruby-v${{ matrix.ruby }}
4644
path: /tmp/cuprite/

.rspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
--color
2-
--format=doc
2+
--format=progress
33
--require spec_helper

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,17 @@
33
### Added
44

55
### Changed
6-
- `@window_size` attribute is moved from Ferrum viewport size is still inherited [#253]
6+
- `@window_size` attribute is moved from Ferrum, viewport size is still inherited [#253]
7+
- Compatibility with latest Ferrum. Browser instance is not passed everywhere now [#254]
8+
- `Cuprite::Browser` methods are located in `Options`.
9+
- `#window_size`
10+
- `#url_blacklist`
11+
- `#url_whitelist`
12+
- `#timeout`
13+
- `Page#new` arguments are changed to `client, context_id:, target_id:`
14+
- `Target#attached?` renamed to `Target#connected?`
15+
- Ferrum doesn't restart browser automatically, Cuprite does
16+
- `Browser#close_window` removes target id asap from the target list
717

818
### Fixed
919
- Detect whether element is in the viewport and clickable before click [#251]

Gemfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ gem "byebug", "~> 11.1", platforms: %i[mri mingw x64_mingw]
66
gem "chunky_png", "~> 1.4"
77
gem "image_size", "~> 3.0"
88
gem "launchy", "~> 2.5"
9-
gem "pdf-reader", "~> 2.5"
9+
gem "pdf-reader", "~> 2.12"
1010
gem "puma", ">= 5.6.7"
1111
gem "rake", "~> 13.0"
1212
gem "rspec", "~> 3.10"
1313
gem "rubocop", "~> 1.22"
1414
gem "rubocop-rake", require: false
15-
gem "sinatra", "~> 2.1"
15+
gem "sinatra", "~> 3.2"
1616

1717
gemspec

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.14.0"
28+
s.add_runtime_dependency "ferrum", "~> 0.15.0"
2929
end

lib/capybara/cuprite.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require "capybara/cuprite/driver"
66
require "capybara/cuprite/browser"
77
require "capybara/cuprite/page"
8+
require "capybara/cuprite/options"
89
require "capybara/cuprite/node"
910
require "capybara/cuprite/errors"
1011

lib/capybara/cuprite/browser.rb

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,20 @@ class Browser < Ferrum::Browser
1111
find_modal accept_confirm dismiss_confirm accept_prompt
1212
dismiss_prompt reset_modals] => :page
1313

14-
attr_reader :url_blacklist, :url_whitelist, :window_size
15-
alias url_blocklist url_blacklist
16-
alias url_allowlist url_whitelist
17-
1814
def initialize(options = nil)
19-
options ||= {}
20-
@client = nil
21-
self.url_blacklist = options[:url_blacklist]
22-
self.url_whitelist = options[:url_whitelist]
23-
2415
super
25-
@window_size = @options.window_size
16+
17+
@options.url_blacklist = prepare_wildcards(options&.dig(:url_blacklist))
18+
@options.url_whitelist = prepare_wildcards(options&.dig(:url_whitelist))
19+
2620
@page = false
2721
end
2822

29-
def timeout=(value)
23+
def command(...)
3024
super
31-
@page.timeout = value unless @page.nil?
25+
rescue Ferrum::DeadBrowserError
26+
restart
27+
raise
3228
end
3329

3430
def page
@@ -39,7 +35,7 @@ def page
3935

4036
def reset
4137
super
42-
@window_size = options.window_size
38+
@options.reset_window_size
4339
@page = attach_page
4440
end
4541

@@ -49,19 +45,29 @@ def quit
4945
end
5046

5147
def resize(**options)
52-
@window_size = [options[:width], options[:height]]
48+
@options.window_size = [options[:width], options[:height]]
5349
super
5450
end
5551

52+
def url_whitelist
53+
@options.url_whitelist
54+
end
55+
alias url_allowlist url_whitelist
56+
5657
def url_whitelist=(patterns)
57-
@url_whitelist = prepare_wildcards(patterns)
58-
page.network.whitelist = @url_whitelist if @client && @url_whitelist.any?
58+
@options.url_whitelist = prepare_wildcards(patterns)
59+
page.network.whitelist = @options.url_whitelist if @client && @options.url_whitelist.any?
5960
end
6061
alias url_allowlist= url_whitelist=
6162

63+
def url_blacklist
64+
@options.url_blacklist
65+
end
66+
alias url_blocklist url_blacklist
67+
6268
def url_blacklist=(patterns)
63-
@url_blacklist = prepare_wildcards(patterns)
64-
page.network.blacklist = @url_blacklist if @client && @url_blacklist.any?
69+
@options.url_blacklist = prepare_wildcards(patterns)
70+
page.network.blacklist = @options.url_blacklist if @client && @options.url_blacklist.any?
6571
end
6672
alias url_blocklist= url_blacklist=
6773

@@ -118,6 +124,7 @@ def close_window(target_id)
118124

119125
@page = nil if @page.target_id == target.id
120126
target.page.close
127+
targets.delete(target_id) # page.close is async, delete target asap
121128
end
122129

123130
def browser_error
@@ -231,10 +238,10 @@ def prepare_wildcards(patterns)
231238
def attach_page(target_id = nil)
232239
target = targets[target_id] if target_id
233240
target ||= default_context.default_target
234-
return target.page if target.attached?
241+
return target.page if target.connected?
235242

236243
target.maybe_sleep_if_new_window
237-
target.page = Page.new(target.id, self)
244+
target.page = Page.new(target.client, context_id: target.context_id, target_id: target.id)
238245
target.page
239246
end
240247
end

lib/capybara/cuprite/driver.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def switch_to_frame(locator)
115115
def open_new_window
116116
target = browser.default_context.create_target
117117
target.maybe_sleep_if_new_window
118-
target.page = Page.new(target.id, browser)
118+
target.page = Page.new(target.client, context_id: target.context_id, target_id: target.id)
119119
target.page
120120
end
121121

lib/capybara/cuprite/options.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
module Ferrum
4+
class Browser
5+
class Options
6+
attr_writer :window_size
7+
attr_accessor :url_blacklist, :url_whitelist
8+
9+
def reset_window_size
10+
@window_size = @options[:window_size]
11+
end
12+
end
13+
end
14+
end

lib/capybara/cuprite/page.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Page < Ferrum::Page
1313
current_url current_title body execution_id execution_id!
1414
evaluate evaluate_on evaluate_async execute] => :active_frame
1515

16-
def initialize(*args)
16+
def initialize(...)
1717
@frame_stack = []
1818
@accept_modal = []
1919
@modal_messages = []
@@ -70,17 +70,17 @@ def dismiss_prompt
7070

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

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

8585
sleep(MODAL_WAIT)
8686
retry
@@ -134,13 +134,13 @@ def title
134134
def prepare_page
135135
super
136136

137-
width, height = @browser.window_size
137+
width, height = @options.window_size
138138
resize(width: width, height: height)
139139

140-
if @browser.url_blacklist.any?
141-
network.blacklist = @browser.url_blacklist
142-
elsif @browser.url_whitelist.any?
143-
network.whitelist = @browser.url_whitelist
140+
if @options.url_blacklist.any?
141+
network.blacklist = @options.url_blacklist
142+
elsif @options.url_whitelist.any?
143+
network.whitelist = @options.url_whitelist
144144
end
145145

146146
on("Page.javascriptDialogOpening") do |params|

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ module TestSessions
8484
#has_css? with spatial requirements accepts spatial options
8585
#has_css? with spatial requirements supports spatial sugar
8686
#fill_in should fill in a textarea in a reasonable time by default
87+
#has_element? should be true if the given element is on the page
8788
REGEXP
8889

8990
metadata[:skip] = true if metadata[:full_description].match(/#{regexes}/)

0 commit comments

Comments
 (0)