Skip to content

Commit 855ed76

Browse files
committed
chore: use blacklist/whitelist and proxy from ferrum
1 parent 2fe6236 commit 855ed76

File tree

7 files changed

+76
-99
lines changed

7 files changed

+76
-99
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
fail-fast: false
1313
matrix:
1414
gemfile: [websocket-driver-0.6.x, websocket-driver-0.7.x]
15-
ruby: [2.7, 3.0, 3.1]
15+
ruby: [2.6, 2.7, 3.0, 3.1]
1616
runs-on: ubuntu-latest
1717
env:
1818
FERRUM_PROCESS_TIMEOUT: 20

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ Besides capybara screenshot method you can get image as Base64:
183183
## Authorization
184184

185185
* `page.driver.basic_authorize(user, password)`
186+
187+
## Proxy
188+
186189
* `page.driver.set_proxy(ip, port, type, user, password)`
187190

188191

@@ -192,14 +195,14 @@ Cuprite supports URL blacklisting, which allows you to prevent scripts from
192195
running on designated domains:
193196

194197
```ruby
195-
page.driver.browser.url_blacklist = ["http://www.example.com"]
198+
page.driver.browser.url_blacklist = %r{http://www.example.com}
196199
```
197200

198201
and also URL whitelisting, which allows scripts to only run on designated
199202
domains:
200203

201204
```ruby
202-
page.driver.browser.url_whitelist = ["http://www.example.com"]
205+
page.driver.browser.url_whitelist = %r{http://www.example.com}
203206
```
204207

205208
If you are experiencing slower run times, consider creating a URL whitelist of

lib/capybara/cuprite/browser.rb

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ def quit
4646

4747
def url_whitelist=(patterns)
4848
@url_whitelist = prepare_wildcards(patterns)
49-
page.network.intercept if @client && !@url_whitelist.empty?
49+
page.network.whitelist = @url_whitelist if @client && @url_whitelist.any?
5050
end
51+
alias url_allowlist= url_whitelist=
5152

5253
def url_blacklist=(patterns)
5354
@url_blacklist = prepare_wildcards(patterns)
54-
page.network.intercept if @client && !@url_blacklist.empty?
55+
page.network.blacklist = @url_blacklist if @client && @url_blacklist.any?
5556
end
57+
alias url_blocklist= url_blacklist=
5658

5759
def visit(*args)
5860
goto(*args)
@@ -199,15 +201,22 @@ def find_all(method, selector, within = nil)
199201
raise
200202
end
201203

202-
def prepare_wildcards(wc)
203-
Array(wc).map do |wildcard|
204-
if wildcard.is_a?(Regexp)
205-
wildcard
204+
def prepare_wildcards(patterns)
205+
string_passed = false
206+
207+
Array(patterns).map do |pattern|
208+
if pattern.is_a?(Regexp)
209+
pattern
206210
else
207-
wildcard = wildcard.gsub("*", ".*")
208-
Regexp.new(wildcard, Regexp::IGNORECASE)
211+
string_passed = true
212+
pattern = pattern.gsub("*", ".*")
213+
Regexp.new(pattern, Regexp::IGNORECASE)
209214
end
210215
end
216+
ensure
217+
if string_passed
218+
warn "Passing strings to blacklist/whitelist is deprecated, pass regexp at #{caller(4..4).first}"
219+
end
211220
end
212221

213222
def attach_page(target_id = nil)

lib/capybara/cuprite/driver.rb

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
require "uri"
44
require "forwardable"
55

6+
# rubocop:disable Metrics/ClassLength
67
module Capybara
78
module Cuprite
8-
# rubocop:disable Metrics/ClassLength
99
class Driver < Capybara::Driver::Base
1010
DEFAULT_MAXIMIZE_SCREEN_SIZE = [1366, 768].freeze
1111
EXTENSION = File.expand_path("javascripts/index.js", __dir__)
@@ -205,16 +205,7 @@ def clear_network_traffic
205205
end
206206

207207
def set_proxy(host, port, user = nil, password = nil, bypass = nil)
208-
browser_options = @options.fetch(:browser_options, {})
209-
browser_options = browser_options.merge("proxy-server" => "#{host}:#{port}")
210-
browser_options = browser_options.merge("proxy-bypass-list" => bypass) if bypass
211-
@options[:browser_options] = browser_options
212-
213-
return unless user && password
214-
215-
browser.network.authorize(user: user, password: password, type: :proxy) do |request, _index, _total|
216-
request.continue
217-
end
208+
@options.merge!(proxy: { host: host, port: port, user: user, password: password, bypass: bypass })
218209
end
219210

220211
def headers
@@ -422,6 +413,6 @@ def pdf?(path, options)
422413
options[:format].to_s == "pdf"
423414
end
424415
end
425-
# rubocop:enable Metrics/ClassLength
426416
end
427417
end
418+
# rubocop:enable Metrics/ClassLength

lib/capybara/cuprite/page.rb

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def initialize(*args)
1717
@frame_stack = []
1818
@accept_modal = []
1919
@modal_messages = []
20+
@modal_response = nil
2021
super
2122
end
2223

@@ -130,35 +131,13 @@ def title
130131

131132
private
132133

133-
# rubocop:disable Metrics/CyclomaticComplexity
134-
# rubocop:disable Metrics/PerceivedComplexity
135-
# rubocop:disable Style/GuardClause
136134
def prepare_page
137135
super
138136

139-
network.intercept if !Array(@browser.url_whitelist).empty? ||
140-
!Array(@browser.url_blacklist).empty?
141-
142-
on(:request) do |request, index, total|
143-
if @browser.url_blacklist && !@browser.url_blacklist.empty?
144-
if @browser.url_blacklist.any? { |r| request.match?(r) }
145-
request.abort and next
146-
else
147-
request.continue and next
148-
end
149-
elsif @browser.url_whitelist && !@browser.url_whitelist.empty?
150-
if @browser.url_whitelist.any? { |r| request.match?(r) }
151-
request.continue and next
152-
else
153-
request.abort and next
154-
end
155-
elsif index + 1 < total
156-
# There are other callbacks that may handle this request
157-
next
158-
else
159-
# If there are no callbacks then just continue
160-
request.continue
161-
end
137+
if @browser.url_blacklist.any?
138+
network.blacklist = @browser.url_blacklist
139+
elsif @browser.url_whitelist.any?
140+
network.whitelist = @browser.url_whitelist
162141
end
163142

164143
on("Page.javascriptDialogOpening") do |params|
@@ -181,9 +160,6 @@ def prepare_page
181160
command("Page.handleJavaScriptDialog", **options)
182161
end
183162
end
184-
# rubocop:enable Metrics/CyclomaticComplexity
185-
# rubocop:enable Metrics/PerceivedComplexity
186-
# rubocop:enable Style/GuardClause
187163

188164
def find_position(node, **options)
189165
node.find_position(**options)

0 commit comments

Comments
 (0)