Skip to content

Commit 4f26e3c

Browse files
committed
Merge remote-tracking branch 'upstream/master' into position
2 parents fa0429d + c356477 commit 4f26e3c

File tree

18 files changed

+248
-20
lines changed

18 files changed

+248
-20
lines changed

.github/workflows/tests.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
name: Tests
2-
on: [push]
2+
on:
3+
push:
4+
branches:
5+
- master
6+
pull_request:
37

48
jobs:
59
tests:

.rubocop.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
inherit_from: .rubocop_todo.yml
2+
3+
AllCops:
4+
TargetRubyVersion: 2.4
5+
NewCops: enable
6+
7+
Layout/FirstArrayElementIndentation:
8+
EnforcedStyle: consistent
9+
10+
Style/StringLiterals:
11+
EnforcedStyle: double_quotes
12+
13+
Metrics/BlockLength:
14+
Exclude:
15+
- spec/**/*
16+
17+
Naming/MethodParameterName:
18+
AllowedNames:
19+
- x
20+
- y
21+
- id
22+
- to
23+
24+
Naming/FileName:
25+
Exclude:
26+
- 'gemfiles/websocket-driver-*.gemfile'

.rubocop_todo.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# This configuration was generated by
2+
# `rubocop --auto-gen-config`
3+
# on 2020-08-25 19:54:53 UTC using RuboCop version 0.89.1.
4+
# The point is for the user to remove these configuration records
5+
# one by one as the offenses are removed from the code base.
6+
# Note that changes in the inspected code, or installation of new
7+
# versions of RuboCop, may require this file to be generated again.
8+
9+
# Offense count: 24
10+
# Configuration parameters: IgnoredMethods.
11+
Metrics/AbcSize:
12+
Max: 66
13+
14+
# Offense count: 5
15+
# Configuration parameters: CountComments, CountAsOne.
16+
Metrics/ClassLength:
17+
Max: 258
18+
19+
# Offense count: 6
20+
# Configuration parameters: IgnoredMethods.
21+
Metrics/CyclomaticComplexity:
22+
Max: 14
23+
24+
# Offense count: 35
25+
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods.
26+
Metrics/MethodLength:
27+
Max: 52
28+
29+
# Offense count: 9
30+
# Configuration parameters: CountComments, CountAsOne.
31+
Metrics/ModuleLength:
32+
Max: 602
33+
34+
# Offense count: 5
35+
# Configuration parameters: IgnoredMethods.
36+
Metrics/PerceivedComplexity:
37+
Max: 14
38+
39+
# Offense count: 2
40+
# Cop supports --auto-correct.
41+
# Configuration parameters: Keywords.
42+
# Keywords: TODO, FIXME, OPTIMIZE, HACK, REVIEW
43+
Style/CommentAnnotation:
44+
Exclude:
45+
- 'spec/node_spec.rb'
46+
47+
# Offense count: 45
48+
Style/Documentation:
49+
Enabled: false
50+
51+
# Offense count: 7
52+
# Cop supports --auto-correct.
53+
# Configuration parameters: EnforcedStyle.
54+
# SupportedStyles: compact, exploded
55+
Style/RaiseArgs:
56+
Exclude:
57+
- 'lib/ferrum/browser/client.rb'
58+
- 'lib/ferrum/browser/command.rb'
59+
- 'lib/ferrum/browser/process.rb'
60+
- 'lib/ferrum/browser/xvfb.rb'
61+
- 'lib/ferrum/frame/runtime.rb'

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,20 @@ simple value.
740740
browser.execute(%(1 + 1)) # => true
741741
```
742742

743+
#### evaluate_on_new_document(expression)
744+
745+
Evaluate JavaScript to modify things before a page load
746+
747+
* expression `String` should be valid JavaScript
748+
749+
```ruby
750+
browser.evaluate_on_new_document <<~JS
751+
Object.defineProperty(navigator, "languages", {
752+
get: function() { return ["tlh"]; }
753+
});
754+
JS
755+
```
756+
743757
#### add_script_tag(\*\*options) : `Boolean`
744758

745759
* options `Hash`

lib/ferrum/browser.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ def extensions
7777
end
7878
end
7979

80+
def evaluate_on_new_document(expression)
81+
extensions << expression
82+
end
83+
8084
def timeout
8185
@timeout || DEFAULT_TIMEOUT
8286
end

lib/ferrum/browser/options/chrome.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class Chrome < Base
3636
"metrics-recording-only" => nil,
3737
"safebrowsing-disable-auto-update" => nil,
3838
"password-store" => "basic",
39+
"no-startup-window" => nil,
3940
# Note: --no-sandbox is not needed if you properly setup a user in the container.
4041
# https://github.com/ebidel/lighthouse-ci/blob/master/builder/Dockerfile#L35-L40
4142
# "no-sandbox" => nil,

lib/ferrum/context.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ def dispose
7272
@contexts.dispose(@id)
7373
end
7474

75+
def has_target?(target_id)
76+
@targets.keys.include?(target_id)
77+
end
78+
7579
def inspect
7680
%(#<#{self.class} @id=#{@id.inspect} @targets=#{@targets.inspect} @default_target=#{@default_target.inspect}>)
7781
end

lib/ferrum/contexts.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Contexts
77
attr_reader :contexts
88

99
def initialize(browser)
10-
@contexts = Concurrent::Hash.new
10+
@contexts = Concurrent::Map.new
1111
@browser = browser
1212
subscribe
1313
discover
@@ -18,7 +18,9 @@ def default_context
1818
end
1919

2020
def find_by(target_id:)
21-
@contexts.find { |_, c| c.targets.keys.include?(target_id) }&.last
21+
context = nil
22+
@contexts.each_value { |c| context = c if c.has_target?(target_id) }
23+
context
2224
end
2325

2426
def create

lib/ferrum/frame/runtime.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,27 +118,26 @@ def add_style_tag(url: nil, path: nil, content: nil)
118118
private
119119

120120
def call(expression:, arguments: [], on: nil, wait: 0, handle: true, **options)
121-
params = options.dup
122121
errors = [NodeNotFoundError, NoExecutionContextError]
123122
attempts, sleep = INTERMITTENT_ATTEMPTS, INTERMITTENT_SLEEP
124123

125124
Ferrum.with_attempts(errors: errors, max: attempts, wait: sleep) do
125+
params = options.dup
126+
126127
if on
127128
response = @page.command("DOM.resolveNode", nodeId: on.node_id)
128129
object_id = response.dig("object", "objectId")
129-
params.merge!(objectId: object_id)
130-
elsif params[:executionContextId].nil?
131-
params.merge!(executionContextId: execution_id)
132-
else
133-
# executionContextId is passed, nop
130+
params = params.merge(objectId: object_id)
134131
end
135132

136-
params.merge!(functionDeclaration: expression,
137-
arguments: prepare_args(arguments))
133+
if params[:executionContextId].nil? && params[:objectId].nil?
134+
params = params.merge(executionContextId: execution_id)
135+
end
138136

139137
response = @page.command("Runtime.callFunctionOn",
140138
wait: wait, slowmoable: true,
141-
**params)
139+
**params.merge(functionDeclaration: expression,
140+
arguments: prepare_args(arguments)))
142141
handle_error(response)
143142
response = response["result"]
144143

lib/ferrum/node.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ def focus
3030
tap { page.command("DOM.focus", slowmoable: true, nodeId: node_id) }
3131
end
3232

33+
def focusable?
34+
focus
35+
true
36+
rescue BrowserError => e
37+
e.message == "Element is not focusable" ? false : raise
38+
end
39+
3340
def blur
3441
tap { evaluate("this.blur()") }
3542
end

0 commit comments

Comments
 (0)