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
1865 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
#
25
# @api private
26
#
27
class BrowsingContext
28
READINESS_STATE = {
29
'none' => 'none',
30
'eager' => 'interactive',
31
'normal' => 'complete'
32
}.freeze
33
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]
40
end
41
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)
50
end
51
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)
61
end
62
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)
72
end
73
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
82
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']
96
end
97
98
def set_viewport(context_id: nil, width: nil, height: nil, device_pixel_ratio: nil)
99
context_id ||= @bridge.window_handle
100
params = {context: context_id, viewport: {width:, height:}, device_pixel_ratio:}
101
@bidi.send_cmd('browsingContext.setViewport', **params)
102
end
103
104
def handle_user_prompt(context_id, accept: true, text: nil)
105
@bidi.send_cmd('browsingContext.handleUserPrompt', context: context_id, accept: accept, text: text)
106
end
107
108
def activate(context_id: nil)
109
context_id ||= @bridge.window_handle
110
@bidi.send_cmd('browsingContext.activate', context: context_id)
111
end
112
end
113
end # BiDi
114
end # WebDriver
115
end # Selenium
116
117