Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/bidi/browsing_context.rb
4050 views
1
# frozen_string_literal: true
2
3
# Licensed to the Software Freedom Conservancy (SFC) under one
4
# or more contributor license agreements. See the NOTICE file
5
# distributed with this work for additional information
6
# regarding copyright ownership. The SFC licenses this file
7
# to you under the Apache License, Version 2.0 (the
8
# "License"); you may not use this file except in compliance
9
# with the License. You may obtain a copy of the License at
10
#
11
# http://www.apache.org/licenses/LICENSE-2.0
12
#
13
# Unless required by applicable law or agreed to in writing,
14
# software distributed under the License is distributed on an
15
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
# KIND, either express or implied. See the License for the
17
# specific language governing permissions and limitations
18
# under the License.
19
20
module Selenium
21
module WebDriver
22
class BiDi
23
# Implements the BrowsingContext Module of the WebDriver-BiDi specification
24
# Continue to use functionality from existing `driver.navigate` method
25
#
26
# @api private
27
#
28
class BrowsingContext
29
READINESS_STATE = {
30
'none' => 'none',
31
'eager' => 'interactive',
32
'normal' => 'complete'
33
}.freeze
34
35
# TODO: store current window handle in bridge object instead of always calling it
36
def initialize(bridge)
37
@bridge = bridge
38
@bidi = @bridge.bidi
39
page_load_strategy = bridge.capabilities[:page_load_strategy]
40
@readiness = READINESS_STATE[page_load_strategy]
41
end
42
43
# Navigates to the specified URL in the given browsing context.
44
#
45
# @param url [String] The URL to navigate to.
46
# @param context_id [String, NilClass] The ID of the browsing context to navigate in.
47
# Defaults to the window handle of the current context.
48
def navigate(url, context_id: nil)
49
context_id ||= @bridge.window_handle
50
@bidi.send_cmd('browsingContext.navigate', context: context_id, url: url, wait: @readiness)
51
end
52
53
# Traverses the browsing context history by a given delta.
54
#
55
# @param delta [Integer] The number of steps to traverse.
56
# Positive values go forwards, negative values go backwards.
57
# @param context_id [String, NilClass] The ID of the context to traverse.
58
# Defaults to the window handle of the current context.
59
def traverse_history(delta, context_id: nil)
60
context_id ||= @bridge.window_handle
61
@bidi.send_cmd('browsingContext.traverseHistory', context: context_id, delta: delta)
62
end
63
64
# Reloads the browsing context.
65
# @param [String, NilClass] context_id The ID of the context to reload.
66
# Defaults to the window handle of the current context.
67
# @param [Boolean] ignore_cache Whether to bypass the cache when reloading.
68
# Defaults to false.
69
def reload(context_id: nil, ignore_cache: false)
70
context_id ||= @bridge.window_handle
71
params = {context: context_id, ignore_cache: ignore_cache, wait: @readiness}
72
@bidi.send_cmd('browsingContext.reload', **params)
73
end
74
75
# Closes the browsing context.
76
#
77
# @param [String] context_id The ID of the context to close.
78
# Defaults to the window handle of the current context.
79
def close(context_id: nil)
80
context_id ||= @bridge.window_handle
81
@bidi.send_cmd('browsingContext.close', context: context_id)
82
end
83
84
# Create a new browsing context.
85
#
86
# @param [Symbol] type The type of browsing context to create.
87
# Valid options are :tab and :window with :window being the default
88
# @param [String] context_id The reference context for the new browsing context.
89
# Defaults to the current window handle.
90
#
91
# @return [String] The context ID of the created browsing context.
92
def create(type: nil, context_id: nil)
93
type ||= :window
94
context_id ||= @bridge.window_handle
95
result = @bidi.send_cmd('browsingContext.create', type: type.to_s, referenceContext: context_id)
96
result['context']
97
end
98
99
def set_viewport(context_id: nil, width: nil, height: nil, device_pixel_ratio: nil)
100
context_id ||= @bridge.window_handle
101
params = {context: context_id, viewport: {width:, height:}, device_pixel_ratio:}
102
@bidi.send_cmd('browsingContext.setViewport', **params)
103
end
104
105
def handle_user_prompt(context_id, accept: true, text: nil)
106
@bidi.send_cmd('browsingContext.handleUserPrompt', context: context_id, accept: accept, text: text)
107
end
108
109
def activate(context_id: nil)
110
context_id ||= @bridge.window_handle
111
@bidi.send_cmd('browsingContext.activate', context: context_id)
112
end
113
end # BrowsingContext
114
end # BiDi
115
end # WebDriver
116
end # Selenium
117
118