Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/spec/integration/selenium/webdriver/bidi/browsing_context_spec.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
require_relative '../spec_helper'
21
22
module Selenium
23
module WebDriver
24
class BiDi
25
describe BrowsingContext, exclusive: {bidi: true, reason: 'only executed when bidi is enabled'},
26
only: {browser: %i[chrome edge firefox]} do
27
after { |example| reset_driver!(example: example) }
28
29
let(:bridge) { driver.instance_variable_get(:@bridge) }
30
31
describe '#create' do
32
it 'without arguments' do
33
id = described_class.new(bridge).create
34
35
expect(driver.window_handles).to include(id)
36
end
37
38
it 'accepts a tab type' do
39
id = described_class.new(bridge).create(type: :tab)
40
41
expect(driver.window_handles).to include(id)
42
end
43
44
it 'accepts a window type' do
45
id = described_class.new(bridge).create(type: :window)
46
47
expect(driver.window_handles).to include(id)
48
end
49
50
it 'errors on unknown type', except: {browser: :firefox, reason: "Doesn't return the expected error"} do
51
msg = /invalid argument: Invalid enum value. Expected 'tab' | 'window', received 'unknown'/
52
expect {
53
described_class.new(bridge).create(type: :unknown)
54
}.to raise_error(Error::WebDriverError, msg)
55
end
56
57
it 'accepts a reference context' do
58
id = driver.window_handle
59
result = described_class.new(bridge).create(context_id: id)
60
61
expect(driver.window_handles).to include(id, result)
62
end
63
end
64
65
it 'closes a window' do
66
browsing_context = described_class.new(bridge)
67
window1 = browsing_context.create
68
window2 = browsing_context.create
69
70
browsing_context.close(context_id: window2)
71
72
handles = driver.window_handles
73
expect(handles).to include(window1)
74
expect(handles).not_to include(window2)
75
end
76
77
it 'sets the viewport', except: {rbe: true, reason: 'unknown, returns value of 1 instead of 2.0'} do
78
browsing_context = described_class.new(bridge)
79
browsing_context.set_viewport(width: 800, height: 600, device_pixel_ratio: 2.0)
80
expect(driver.execute_script('return [window.innerWidth, window.innerHeight]')).to eq([800, 600])
81
expect(driver.execute_script('return window.devicePixelRatio')).to eq(2.0)
82
end
83
84
it 'accepts users prompts without text',
85
except: {browser: %i[edge chrome],
86
reason: 'https://github.com/GoogleChromeLabs/chromium-bidi/issues/3281'} do
87
browsing_context = described_class.new(bridge)
88
89
driver.navigate.to url_for('alerts.html')
90
driver.find_element(id: 'alert').click
91
wait_for_alert
92
window = driver.window_handles.first
93
browsing_context.handle_user_prompt(window, accept: true)
94
wait_for_no_alert
95
96
expect(driver.title).to eq('Testing Alerts')
97
end
98
99
it 'accepts users prompts with text',
100
except: {browser: %i[edge chrome],
101
reason: 'https://github.com/GoogleChromeLabs/chromium-bidi/issues/3281'} do
102
browsing_context = described_class.new(bridge)
103
driver.navigate.to url_for('alerts.html')
104
driver.find_element(id: 'prompt').click
105
wait_for_alert
106
window = driver.window_handles.first
107
browsing_context.handle_user_prompt(window, accept: true, text: 'Hello, world!')
108
wait_for_no_alert
109
110
expect(driver.title).to eq('Testing Alerts')
111
end
112
113
it 'rejects users prompts', except: {browser: %i[edge chrome],
114
reason: 'https://github.com/GoogleChromeLabs/chromium-bidi/issues/3281'} do
115
browsing_context = described_class.new(bridge)
116
driver.navigate.to url_for('alerts.html')
117
driver.find_element(id: 'alert').click
118
wait_for_alert
119
window = driver.window_handles.first
120
121
browsing_context.handle_user_prompt(window, accept: false)
122
wait_for_no_alert
123
124
expect(driver.title).to eq('Testing Alerts')
125
end
126
127
it 'activates a browser context' do
128
browsing_context = described_class.new(bridge)
129
browsing_context.create
130
131
expect(driver.execute_script('return document.hasFocus();')).to be_falsey
132
browsing_context.activate
133
expect(driver.execute_script('return document.hasFocus();')).to be_truthy
134
end
135
end
136
end # BiDi
137
end # WebDriver
138
end # Selenium
139
140