|
17 | 17 | # specific language governing permissions and limitations |
18 | 18 | # under the License. |
19 | 19 |
|
20 | | -require_relative 'navigate_result' |
21 | | -require_relative 'browsing_context_info' |
22 | | - |
23 | 20 | module Selenium |
24 | 21 | module WebDriver |
25 | 22 | class BiDi |
| 23 | + # Implements the browsingContext Module of the WebDriver-BiDi specification |
| 24 | + # |
| 25 | + # @api private |
| 26 | + # |
26 | 27 | class BrowsingContext |
27 | | - attr_accessor :id |
28 | | - |
29 | 28 | READINESS_STATE = { |
30 | | - none: 'none', |
31 | | - interactive: 'interactive', |
32 | | - complete: 'complete' |
| 29 | + 'none' => 'none', |
| 30 | + 'eager' => 'interactive', |
| 31 | + 'normal' => 'complete' |
33 | 32 | }.freeze |
34 | 33 |
|
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] |
48 | 40 | end |
49 | 41 |
|
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) |
63 | 50 | end |
64 | 51 |
|
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) |
74 | 61 | end |
75 | 62 |
|
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) |
78 | 72 | end |
79 | 73 |
|
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 |
81 | 82 |
|
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'] |
84 | 96 | end |
85 | | - end # BrowsingContext |
| 97 | + end |
86 | 98 | end # BiDi |
87 | 99 | end # WebDriver |
88 | 100 | end # Selenium |
0 commit comments