Skip to content

Commit fefdba1

Browse files
titusfortneraguspe
andauthored
[rb] implement navigation commands with BiDi (#14094)
* [rb] create context manager and implement navigation with BiDi * replace BrowsingContext class instead of creating new context manager * Fix formatting issues --------- Co-authored-by: Augustin Gottlieb <[email protected]> Co-authored-by: aguspe <[email protected]>
1 parent 789e8d4 commit fefdba1

File tree

8 files changed

+176
-230
lines changed

8 files changed

+176
-230
lines changed

rb/lib/selenium/webdriver/bidi/browsing_context.rb

Lines changed: 61 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -17,72 +17,84 @@
1717
# specific language governing permissions and limitations
1818
# under the License.
1919

20-
require_relative 'navigate_result'
21-
require_relative 'browsing_context_info'
22-
2320
module Selenium
2421
module WebDriver
2522
class BiDi
23+
# Implements the browsingContext Module of the WebDriver-BiDi specification
24+
#
25+
# @api private
26+
#
2627
class BrowsingContext
27-
attr_accessor :id
28-
2928
READINESS_STATE = {
30-
none: 'none',
31-
interactive: 'interactive',
32-
complete: 'complete'
29+
'none' => 'none',
30+
'eager' => 'interactive',
31+
'normal' => 'complete'
3332
}.freeze
3433

35-
def initialize(driver:, browsing_context_id: nil, type: nil, reference_context: nil)
36-
unless driver.capabilities.web_socket_url
37-
raise Error::WebDriverError,
38-
'WebDriver instance must support BiDi protocol'
39-
end
40-
41-
unless type.nil? || %i[window tab].include?(type)
42-
raise ArgumentError,
43-
"Valid types are :window & :tab. Received: #{type.inspect}"
44-
end
45-
46-
@bidi = driver.bidi
47-
@id = browsing_context_id.nil? ? create(type, reference_context)['context'] : browsing_context_id
34+
# TODO: store current window handle in bridge object instead of always calling it
35+
def initialize(bridge)
36+
@bridge = bridge
37+
@bidi = @bridge.bidi
38+
page_load_strategy = bridge.capabilities[:page_load_strategy]
39+
@readiness = READINESS_STATE[page_load_strategy]
4840
end
4941

50-
def navigate(url:, readiness_state: nil)
51-
unless readiness_state.nil? || READINESS_STATE.key?(readiness_state)
52-
raise ArgumentError,
53-
"Valid readiness states are :none, :interactive & :complete. Received: #{readiness_state.inspect}"
54-
end
55-
56-
navigate_result = @bidi.send_cmd('browsingContext.navigate', context: @id, url: url,
57-
wait: READINESS_STATE[readiness_state])
58-
59-
NavigateResult.new(
60-
url: navigate_result['url'],
61-
navigation_id: navigate_result['navigation']
62-
)
42+
# Navigates to the specified URL in the given browsing context.
43+
#
44+
# @param url [String] The URL to navigate to.
45+
# @param context_id [String, NilClass] The ID of the browsing context to navigate in.
46+
# Defaults to the window handle of the current context.
47+
def navigate(url, context_id: nil)
48+
context_id ||= @bridge.window_handle
49+
@bidi.send_cmd('browsingContext.navigate', context: context_id, url: url, wait: @readiness)
6350
end
6451

65-
def get_tree(max_depth: nil)
66-
result = @bidi.send_cmd('browsingContext.getTree', root: @id, maxDepth: max_depth).dig('contexts', 0)
67-
68-
BrowsingContextInfo.new(
69-
id: result['context'],
70-
url: result['url'],
71-
children: result['children'],
72-
parent_context: result['parent']
73-
)
52+
# Traverses the browsing context history by a given delta.
53+
#
54+
# @param delta [Integer] The number of steps to traverse.
55+
# Positive values go forwards, negative values go backwards.
56+
# @param context_id [String, NilClass] The ID of the context to traverse.
57+
# Defaults to the window handle of the current context.
58+
def traverse_history(delta, context_id: nil)
59+
context_id ||= @bridge.window_handle
60+
@bidi.send_cmd('browsingContext.traverseHistory', context: context_id, delta: delta)
7461
end
7562

76-
def close
77-
@bidi.send_cmd('browsingContext.close', context: @id)
63+
# Reloads the browsing context.
64+
# @param [String, NilClass] context_id The ID of the context to reload.
65+
# Defaults to the window handle of the current context.
66+
# @param [Boolean] ignore_cache Whether to bypass the cache when reloading.
67+
# Defaults to false.
68+
def reload(context_id: nil, ignore_cache: false)
69+
context_id ||= @bridge.window_handle
70+
params = {context: context_id, ignore_cache: ignore_cache, wait: @readiness}
71+
@bidi.send_cmd('browsingContext.reload', **params)
7872
end
7973

80-
private
74+
# Closes the browsing context.
75+
#
76+
# @param [String] context_id The ID of the context to close.
77+
# Defaults to the window handle of the current context.
78+
def close(context_id: nil)
79+
context_id ||= @bridge.window_handle
80+
@bidi.send_cmd('browsingContext.close', context: context_id)
81+
end
8182

82-
def create(type, reference_context)
83-
@bidi.send_cmd('browsingContext.create', type: type.to_s, referenceContext: reference_context)
83+
# Create a new browsing context.
84+
#
85+
# @param [Symbol] type The type of browsing context to create.
86+
# Valid options are :tab and :window with :window being the default
87+
# @param [String] context_id The reference context for the new browsing context.
88+
# Defaults to the current window handle.
89+
#
90+
# @return [String] The context ID of the created browsing context.
91+
def create(type: nil, context_id: nil)
92+
type ||= :window
93+
context_id ||= @bridge.window_handle
94+
result = @bidi.send_cmd('browsingContext.create', type: type.to_s, referenceContext: context_id)
95+
result['context']
8496
end
85-
end # BrowsingContext
97+
end
8698
end # BiDi
8799
end # WebDriver
88100
end # Selenium

rb/lib/selenium/webdriver/bidi/browsing_context_info.rb

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

rb/lib/selenium/webdriver/bidi/navigate_result.rb

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

rb/lib/selenium/webdriver/remote/bidi_bridge.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ def create_session(capabilities)
2929
@bidi = Selenium::WebDriver::BiDi.new(url: socket_url)
3030
end
3131

32+
def get(url)
33+
browsing_context.navigate(url)
34+
end
35+
36+
def go_back
37+
browsing_context.traverse_history(-1)
38+
end
39+
40+
def go_forward
41+
browsing_context.traverse_history(1)
42+
end
43+
44+
def refresh
45+
browsing_context.reload
46+
end
47+
3248
def quit
3349
super
3450
ensure
@@ -38,6 +54,12 @@ def quit
3854
def close
3955
execute(:close_window).tap { |handles| bidi.close if handles.empty? }
4056
end
57+
58+
private
59+
60+
def browsing_context
61+
@browsing_context ||= WebDriver::BiDi::BrowsingContext.new(self)
62+
end
4163
end # BiDiBridge
4264
end # Remote
4365
end # WebDriver

rb/sig/lib/selenium/webdriver/bidi/browsing_context.rbs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,21 @@ module Selenium
22
module WebDriver
33
class BiDi
44
class BrowsingContext
5-
@bidi: untyped
5+
@bidi: BiDi
66

7-
@id: untyped
7+
READINESS_STATE: Hash[String, String]
88

9-
attr_accessor id: untyped
9+
def initialize: (Remote::Bridge bridge) -> void
1010

11-
READINESS_STATE: Hash[Symbol, String]
11+
def navigate: (String url, String? context_id) -> void
1212

13-
def initialize: (driver: untyped, ?browsing_context_id: untyped?, ?type: untyped?, ?reference_context: untyped?) -> void
13+
def traverse_history: (Integer delta, String? context_id) -> void
1414

15-
def navigate: (url: untyped, ?readiness_state: untyped?) -> untyped
15+
def reload: (String? context_id, ?ignore_cache: bool) -> void
1616

17-
def get_tree: (?max_depth: untyped?) -> untyped
17+
def close: (String? context_id) -> void
1818

19-
def close: () -> untyped
20-
21-
private
22-
23-
def create: (untyped type, untyped reference_context) -> untyped
19+
def create: (?type: Symbol | String | nil, ?context_id: String | nil) -> String
2420
end
2521
end
2622
end

rb/sig/lib/selenium/webdriver/remote/bidi_bridge.rbs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
module Selenium
22
module WebDriver
33
module Remote
4-
class BiDiBridge < Bridge
5-
@bidi: untyped
4+
class BiDiBridge
5+
@browsing_context: BiDi::BrowsingContext
66

7-
attr_reader bidi: untyped
7+
attr_reader bidi: BiDi
88

9-
def create_session: (Hash[Symbol, String] capabilities) -> BiDi
9+
def create_session: (untyped capabilities) -> void
1010

11-
def quit: () -> nil
11+
def get: (String url) -> void
1212

13-
def close: () -> untyped
13+
def go_back: () -> void
14+
15+
def go_forward: () -> void
16+
17+
def refresh: () -> void
18+
19+
def quit: () -> void
20+
21+
def close: () -> void
22+
23+
private
24+
25+
def browsing_context: () -> BiDi::BrowsingContext
1426
end
1527
end
1628
end

0 commit comments

Comments
 (0)