Skip to content

Commit

Permalink
Merge pull request #173 from rubycdp/latest-ferrum
Browse files Browse the repository at this point in the history
Latest ferrum
  • Loading branch information
route committed Nov 20, 2022
2 parents 2fe6236 + 7374471 commit 3c153b3
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 100 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
AllCops:
TargetRubyVersion: 2.6
TargetRubyVersion: 2.7
NewCops: enable
SuggestExtensions: false

Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ Besides capybara screenshot method you can get image as Base64:
## Authorization

* `page.driver.basic_authorize(user, password)`

## Proxy

* `page.driver.set_proxy(ip, port, type, user, password)`


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

```ruby
page.driver.browser.url_blacklist = ["http://www.example.com"]
page.driver.browser.url_blacklist = %r{http://www.example.com}
```

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

```ruby
page.driver.browser.url_whitelist = ["http://www.example.com"]
page.driver.browser.url_whitelist = %r{http://www.example.com}
```

If you are experiencing slower run times, consider creating a URL whitelist of
Expand Down
2 changes: 1 addition & 1 deletion cuprite.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Gem::Specification.new do |s|
"rubygems_mfa_required" => "true"
}

s.required_ruby_version = ">= 2.6.0"
s.required_ruby_version = ">= 2.7.0"

s.add_runtime_dependency "capybara", "~> 3.0"
s.add_runtime_dependency "ferrum", "~> 0.13.0"
Expand Down
25 changes: 17 additions & 8 deletions lib/capybara/cuprite/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ def quit

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

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

def visit(*args)
goto(*args)
Expand Down Expand Up @@ -199,15 +201,22 @@ def find_all(method, selector, within = nil)
raise
end

def prepare_wildcards(wc)
Array(wc).map do |wildcard|
if wildcard.is_a?(Regexp)
wildcard
def prepare_wildcards(patterns)
string_passed = false

Array(patterns).map do |pattern|
if pattern.is_a?(Regexp)
pattern
else
wildcard = wildcard.gsub("*", ".*")
Regexp.new(wildcard, Regexp::IGNORECASE)
string_passed = true
pattern = pattern.gsub("*", ".*")
Regexp.new(pattern, Regexp::IGNORECASE)
end
end
ensure
if string_passed
warn "Passing strings to blacklist/whitelist is deprecated, pass regexp at #{caller(4..4).first}"
end
end

def attach_page(target_id = nil)
Expand Down
15 changes: 3 additions & 12 deletions lib/capybara/cuprite/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
require "uri"
require "forwardable"

# rubocop:disable Metrics/ClassLength
module Capybara
module Cuprite
# rubocop:disable Metrics/ClassLength
class Driver < Capybara::Driver::Base
DEFAULT_MAXIMIZE_SCREEN_SIZE = [1366, 768].freeze
EXTENSION = File.expand_path("javascripts/index.js", __dir__)
Expand Down Expand Up @@ -205,16 +205,7 @@ def clear_network_traffic
end

def set_proxy(host, port, user = nil, password = nil, bypass = nil)
browser_options = @options.fetch(:browser_options, {})
browser_options = browser_options.merge("proxy-server" => "#{host}:#{port}")
browser_options = browser_options.merge("proxy-bypass-list" => bypass) if bypass
@options[:browser_options] = browser_options

return unless user && password

browser.network.authorize(user: user, password: password, type: :proxy) do |request, _index, _total|
request.continue
end
@options.merge!(proxy: { host: host, port: port, user: user, password: password, bypass: bypass })
end

def headers
Expand Down Expand Up @@ -422,6 +413,6 @@ def pdf?(path, options)
options[:format].to_s == "pdf"
end
end
# rubocop:enable Metrics/ClassLength
end
end
# rubocop:enable Metrics/ClassLength
34 changes: 5 additions & 29 deletions lib/capybara/cuprite/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def initialize(*args)
@frame_stack = []
@accept_modal = []
@modal_messages = []
@modal_response = nil
super
end

Expand Down Expand Up @@ -130,35 +131,13 @@ def title

private

# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity
# rubocop:disable Style/GuardClause
def prepare_page
super

network.intercept if !Array(@browser.url_whitelist).empty? ||
!Array(@browser.url_blacklist).empty?

on(:request) do |request, index, total|
if @browser.url_blacklist && !@browser.url_blacklist.empty?
if @browser.url_blacklist.any? { |r| request.match?(r) }
request.abort and next
else
request.continue and next
end
elsif @browser.url_whitelist && !@browser.url_whitelist.empty?
if @browser.url_whitelist.any? { |r| request.match?(r) }
request.continue and next
else
request.abort and next
end
elsif index + 1 < total
# There are other callbacks that may handle this request
next
else
# If there are no callbacks then just continue
request.continue
end
if @browser.url_blacklist.any?
network.blacklist = @browser.url_blacklist
elsif @browser.url_whitelist.any?
network.whitelist = @browser.url_whitelist
end

on("Page.javascriptDialogOpening") do |params|
Expand All @@ -181,9 +160,6 @@ def prepare_page
command("Page.handleJavaScriptDialog", **options)
end
end
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/PerceivedComplexity
# rubocop:enable Style/GuardClause

def find_position(node, **options)
node.find_position(**options)
Expand Down
Loading

0 comments on commit 3c153b3

Please sign in to comment.