Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/spec/integration/selenium/webdriver/chrome/driver_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
module Chrome
25
describe Driver, exclusive: [{bidi: false, reason: 'Not yet implemented with BiDi'}, {browser: :chrome}] do
26
it 'gets and sets network conditions' do
27
driver.network_conditions = {offline: false, latency: 56, throughput: 789}
28
expect(driver.network_conditions).to eq(
29
'offline' => false,
30
'latency' => 56,
31
'download_throughput' => 789,
32
'upload_throughput' => 789
33
)
34
end
35
36
it 'sets download path' do
37
expect { driver.download_path = File.expand_path(__dir__) }.not_to raise_exception
38
end
39
40
it 'can execute CDP commands' do
41
res = driver.execute_cdp('Page.addScriptToEvaluateOnNewDocument', source: 'window.was_here="TW";')
42
expect(res).to have_key('identifier')
43
44
begin
45
driver.navigate.to url_for('formPage.html')
46
47
tw = driver.execute_script('return window.was_here')
48
expect(tw).to eq('TW')
49
ensure
50
driver.execute_cdp('Page.removeScriptToEvaluateOnNewDocument', identifier: res['identifier'])
51
end
52
end
53
54
describe 'PrintsPage' do
55
before(:all) { @headless = ENV.delete('HEADLESS') }
56
before { reset_driver!(args: ['--headless']) }
57
58
after(:all) do
59
quit_driver
60
ENV['HEADLESS'] = @headless
61
end
62
63
let(:magic_number) { 'JVBER' }
64
65
it 'returns base64 for print command' do
66
driver.navigate.to url_for('printPage.html')
67
expect(driver.print_page).to include(magic_number)
68
end
69
70
it 'prints with valid params' do
71
driver.navigate.to url_for('printPage.html')
72
expect(driver.print_page(orientation: 'landscape',
73
page_ranges: ['1-2'],
74
page: {width: 30})).to include(magic_number)
75
end
76
77
it 'saves pdf' do
78
driver.navigate.to url_for('printPage.html')
79
80
path = "#{Dir.tmpdir}/test#{SecureRandom.urlsafe_base64}.pdf"
81
82
driver.save_print_page path
83
84
expect(File.exist?(path)).to be true
85
expect(File.size(path)).to be_positive
86
ensure
87
FileUtils.rm_rf(path)
88
end
89
end
90
91
describe '#logs' do
92
before do
93
reset_driver!(logging_prefs: {browser: 'ALL',
94
driver: 'ALL',
95
performance: 'ALL'})
96
driver.navigate.to url_for('errors.html')
97
end
98
99
after(:all) { reset_driver! }
100
101
it 'can fetch available log types' do
102
expect(driver.logs.available_types).to include(:performance, :browser, :driver)
103
end
104
105
it 'can get the browser log' do
106
driver.find_element(tag_name: 'input').click
107
108
entries = driver.logs.get(:browser)
109
expect(entries).not_to be_empty
110
expect(entries.first).to be_a(LogEntry)
111
end
112
113
it 'can get the driver log' do
114
entries = driver.logs.get(:driver)
115
expect(entries).not_to be_empty
116
expect(entries.first).to be_a(LogEntry)
117
end
118
119
it 'can get the performance log' do
120
entries = driver.logs.get(:performance)
121
expect(entries).not_to be_empty
122
expect(entries.first).to be_a(LogEntry)
123
end
124
end
125
126
it 'manages network features' do
127
driver.network_conditions = {offline: false, latency: 56, download_throughput: 789, upload_throughput: 600}
128
conditions = driver.network_conditions
129
expect(conditions['offline']).to be false
130
expect(conditions['latency']).to eq 56
131
expect(conditions['download_throughput']).to eq 789
132
expect(conditions['upload_throughput']).to eq 600
133
driver.delete_network_conditions
134
135
error = /network conditions must be set before it can be retrieved/
136
expect { driver.network_conditions }.to raise_error(Error::UnknownError, error)
137
138
# Need to reset because https://bugs.chromium.org/p/chromedriver/issues/detail?id=4790
139
reset_driver!
140
end
141
142
# This requires cast sinks to run
143
it 'casts' do
144
# Does not get list correctly the first time for some reason
145
driver.cast_sinks
146
sleep 2
147
sinks = driver.cast_sinks
148
unless sinks.empty?
149
device_name = sinks.first['name']
150
driver.start_cast_tab_mirroring(device_name)
151
expect { driver.stop_casting(device_name) }.not_to raise_exception
152
end
153
end
154
155
def get_permission(name)
156
driver.execute_async_script('callback = arguments[arguments.length - 1];' \
157
'callback(navigator.permissions.query({name: arguments[0]}));', name)['state']
158
end
159
160
it 'can set single permissions' do
161
driver.navigate.to url_for('xhtmlTest.html')
162
163
expect(get_permission('clipboard-read')).to eq('prompt')
164
expect(get_permission('clipboard-write')).to eq('granted')
165
166
driver.add_permission('clipboard-read', 'denied')
167
driver.add_permission('clipboard-write', 'prompt')
168
169
expect(get_permission('clipboard-read')).to eq('denied')
170
expect(get_permission('clipboard-write')).to eq('prompt')
171
172
reset_driver!
173
end
174
175
it 'can set multiple permissions' do
176
driver.navigate.to url_for('xhtmlTest.html')
177
178
expect(get_permission('clipboard-read')).to eq('prompt')
179
expect(get_permission('clipboard-write')).to eq('granted')
180
181
driver.add_permissions('clipboard-read' => 'denied', 'clipboard-write' => 'prompt')
182
183
expect(get_permission('clipboard-read')).to eq('denied')
184
expect(get_permission('clipboard-write')).to eq('prompt')
185
end
186
end
187
end # Chrome
188
end # WebDriver
189
end # Selenium
190
191