capybara-lightpanda 0.19.0 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,10 +2,13 @@
2
2
 
3
3
  module Capybara
4
4
  module Lightpanda
5
- # Exposes the gem's rake tasks (lightpanda:binary:*) inside Rails apps.
6
- # Without this, the BinaryError remediation hint ("bundle exec rake
7
- # lightpanda:binary:remove lightpanda:binary:update") pointed at tasks
8
- # that didn't exist in the app's rake namespace.
5
+ # Exposes the gem's rake tasks (lightpanda:binary:version, :update,
6
+ # :remove) inside Rails apps, so `bin/rails lightpanda:binary:update`
7
+ # works without a Rakefile edit. Rails only loads the gem (and so this
8
+ # Railtie) in the Gemfile groups the current environment requires — for
9
+ # the usual :test group that means RAILS_ENV=test — which is why
10
+ # Binary's too-old-binary hint suggests a require-the-gem one-liner
11
+ # instead of these tasks.
9
12
  class Railtie < Rails::Railtie
10
13
  rake_tasks do
11
14
  load File.expand_path("tasks/binary.rake", __dir__)
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Capybara
4
+ module Lightpanda
5
+ class RequestHeaders
6
+ attr_reader :referrer
7
+
8
+ def initialize(browser)
9
+ @browser = browser
10
+ end
11
+
12
+ def get
13
+ @browser.network.extra_headers.dup
14
+ end
15
+
16
+ def set(headers) # rubocop:disable Naming/PredicateMethod
17
+ apply(headers)
18
+ @referrer = headers[header_key(headers, "Referer")]
19
+ true
20
+ end
21
+
22
+ # Ferrum applies a non-permanent Referer to Page.navigate calls only.
23
+ def add(headers, permanent: true) # rubocop:disable Naming/PredicateMethod
24
+ additions = headers.dup
25
+ key = header_key(additions, "Referer")
26
+ referrer = additions[key]
27
+ additions.delete(key) unless permanent
28
+ merged = get.reject { |name, _| header_key(additions, name) }
29
+ apply(merged.merge(additions))
30
+ @referrer = referrer if key
31
+ true
32
+ end
33
+
34
+ def clear
35
+ set({})
36
+ end
37
+
38
+ def reset
39
+ @navigator_defaults = nil
40
+ @referrer = nil
41
+ end
42
+
43
+ private
44
+
45
+ def header_key(headers, name)
46
+ headers.keys.find { |key| key.to_s.casecmp?(name.to_s) }
47
+ end
48
+
49
+ def apply(headers)
50
+ agent = headers[header_key(headers, "User-Agent")]
51
+ language = headers[header_key(headers, "Accept-Language")]
52
+ if Network.refused_user_agent?(agent)
53
+ raise ArgumentError, 'Lightpanda refuses a User-Agent containing "Mozilla"'
54
+ end
55
+
56
+ previous = get
57
+ if agent || language || @navigator_defaults
58
+ @navigator_defaults ||= @browser.evaluate("[navigator.userAgent, navigator.language]")
59
+ end
60
+ @browser.network.headers = headers
61
+ update_navigator(agent, language, previous) if @navigator_defaults
62
+ end
63
+
64
+ def update_navigator(agent, language, previous)
65
+ begin
66
+ @browser.page_command("Emulation.setUserAgentOverride", userAgent: agent || @navigator_defaults.first,
67
+ acceptLanguage: language || @navigator_defaults.last)
68
+ rescue BrowserError
69
+ @browser.network.headers = previous
70
+ raise
71
+ end
72
+ @navigator_defaults = nil unless agent || language
73
+ end
74
+ end
75
+ end
76
+ end
@@ -29,9 +29,10 @@ module Capybara
29
29
  last_error = e
30
30
  end
31
31
 
32
- break if monotonic_time > deadline
32
+ left = deadline - monotonic_time
33
+ break unless left.positive?
33
34
 
34
- sleep interval
35
+ sleep [interval, left].min
35
36
  end
36
37
 
37
38
  msg = message || "timed out after #{timeout}s"
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Capybara
4
4
  module Lightpanda
5
- VERSION = "0.19.0"
5
+ VERSION = "0.20.0"
6
6
  end
7
7
  end
@@ -13,6 +13,7 @@ require_relative "capybara/lightpanda/utils/attempt"
13
13
  require_relative "capybara/lightpanda/utils/wait"
14
14
  require_relative "capybara/lightpanda/client"
15
15
  require_relative "capybara/lightpanda/headers"
16
+ require_relative "capybara/lightpanda/request_headers"
16
17
  require_relative "capybara/lightpanda/network"
17
18
  require_relative "capybara/lightpanda/downloads"
18
19
  require_relative "capybara/lightpanda/cookies"
@@ -22,6 +23,7 @@ require_relative "capybara/lightpanda/auto_scripts"
22
23
  require_relative "capybara/lightpanda/node"
23
24
  require_relative "capybara/lightpanda/element_extension"
24
25
  require_relative "capybara/lightpanda/driver"
26
+ require_relative "capybara/lightpanda/cuprite_driver"
25
27
  require_relative "capybara/lightpanda/railtie" if defined?(Rails::Railtie)
26
28
 
27
29
  module Capybara
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capybara-lightpanda
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.19.0
4
+ version: 0.20.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Navid Emad
@@ -57,9 +57,9 @@ dependencies:
57
57
  - - "~>"
58
58
  - !ruby/object:Gem::Version
59
59
  version: '0.8'
60
- description: A Capybara driver for Lightpanda, the fast headless browser built in
61
- Zig. Provides a production-ready driver with XPath polyfill, reliable navigation,
62
- and cookie management — ready for real-world Rails test suites.
60
+ description: 'A Capybara driver for Lightpanda, the fast headless browser built in
61
+ Zig. Same Capybara DSL, a built-in CDP client, Turbo-aware waits, and no Chrome
62
+ to install: the gem downloads the browser it was validated against.'
63
63
  email:
64
64
  - design.navid@gmail.com
65
65
  executables:
@@ -87,6 +87,9 @@ files:
87
87
  - lib/capybara/lightpanda/client/subscriber.rb
88
88
  - lib/capybara/lightpanda/client/web_socket.rb
89
89
  - lib/capybara/lightpanda/cookies.rb
90
+ - lib/capybara/lightpanda/cuprite_cookies.rb
91
+ - lib/capybara/lightpanda/cuprite_driver.rb
92
+ - lib/capybara/lightpanda/cuprite_page.rb
90
93
  - lib/capybara/lightpanda/downloads.rb
91
94
  - lib/capybara/lightpanda/driver.rb
92
95
  - lib/capybara/lightpanda/element_extension.rb
@@ -99,10 +102,12 @@ files:
99
102
  - lib/capybara/lightpanda/keyboard.rb
100
103
  - lib/capybara/lightpanda/logger.rb
101
104
  - lib/capybara/lightpanda/network.rb
105
+ - lib/capybara/lightpanda/network/exchange.rb
102
106
  - lib/capybara/lightpanda/node.rb
103
107
  - lib/capybara/lightpanda/options.rb
104
108
  - lib/capybara/lightpanda/process.rb
105
109
  - lib/capybara/lightpanda/railtie.rb
110
+ - lib/capybara/lightpanda/request_headers.rb
106
111
  - lib/capybara/lightpanda/tasks/binary.rake
107
112
  - lib/capybara/lightpanda/utils/attempt.rb
108
113
  - lib/capybara/lightpanda/utils/event.rb