Path: blob/trunk/rb/spec/integration/selenium/webdriver/chrome/driver_spec.rb
1865 views
# frozen_string_literal: true12# Licensed to the Software Freedom Conservancy (SFC) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The SFC licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.1819require_relative '../spec_helper'2021module Selenium22module WebDriver23module Chrome24describe Driver, exclusive: [{bidi: false, reason: 'Not yet implemented with BiDi'}, {browser: :chrome}] do25it 'gets and sets network conditions' do26driver.network_conditions = {offline: false, latency: 56, throughput: 789}27expect(driver.network_conditions).to eq(28'offline' => false,29'latency' => 56,30'download_throughput' => 789,31'upload_throughput' => 78932)33end3435it 'sets download path' do36expect { driver.download_path = File.expand_path(__dir__) }.not_to raise_exception37end3839it 'can execute CDP commands' do40res = driver.execute_cdp('Page.addScriptToEvaluateOnNewDocument', source: 'window.was_here="TW";')41expect(res).to have_key('identifier')4243begin44driver.navigate.to url_for('formPage.html')4546tw = driver.execute_script('return window.was_here')47expect(tw).to eq('TW')48ensure49driver.execute_cdp('Page.removeScriptToEvaluateOnNewDocument', identifier: res['identifier'])50end51end5253describe 'PrintsPage' do54before(:all) { @headless = ENV.delete('HEADLESS') }55before { reset_driver!(args: ['--headless']) }5657after(:all) do58quit_driver59ENV['HEADLESS'] = @headless60end6162let(:magic_number) { 'JVBER' }6364it 'returns base64 for print command' do65driver.navigate.to url_for('printPage.html')66expect(driver.print_page).to include(magic_number)67end6869it 'prints with valid params' do70driver.navigate.to url_for('printPage.html')71expect(driver.print_page(orientation: 'landscape',72page_ranges: ['1-2'],73page: {width: 30})).to include(magic_number)74end7576it 'saves pdf' do77driver.navigate.to url_for('printPage.html')7879path = "#{Dir.tmpdir}/test#{SecureRandom.urlsafe_base64}.pdf"8081driver.save_print_page path8283expect(File.exist?(path)).to be true84expect(File.size(path)).to be_positive85ensure86FileUtils.rm_rf(path)87end88end8990describe '#logs' do91before do92reset_driver!(logging_prefs: {browser: 'ALL',93driver: 'ALL',94performance: 'ALL'})95driver.navigate.to url_for('errors.html')96end9798after(:all) { reset_driver! }99100it 'can fetch available log types' do101expect(driver.logs.available_types).to include(:performance, :browser, :driver)102end103104it 'can get the browser log' do105driver.find_element(tag_name: 'input').click106107entries = driver.logs.get(:browser)108expect(entries).not_to be_empty109expect(entries.first).to be_a(LogEntry)110end111112it 'can get the driver log' do113entries = driver.logs.get(:driver)114expect(entries).not_to be_empty115expect(entries.first).to be_a(LogEntry)116end117118it 'can get the performance log' do119entries = driver.logs.get(:performance)120expect(entries).not_to be_empty121expect(entries.first).to be_a(LogEntry)122end123end124125it 'manages network features' do126driver.network_conditions = {offline: false, latency: 56, download_throughput: 789, upload_throughput: 600}127conditions = driver.network_conditions128expect(conditions['offline']).to be false129expect(conditions['latency']).to eq 56130expect(conditions['download_throughput']).to eq 789131expect(conditions['upload_throughput']).to eq 600132driver.delete_network_conditions133134error = /network conditions must be set before it can be retrieved/135expect { driver.network_conditions }.to raise_error(Error::UnknownError, error)136137# Need to reset because https://bugs.chromium.org/p/chromedriver/issues/detail?id=4790138reset_driver!139end140141# This requires cast sinks to run142it 'casts' do143# Does not get list correctly the first time for some reason144driver.cast_sinks145sleep 2146sinks = driver.cast_sinks147unless sinks.empty?148device_name = sinks.first['name']149driver.start_cast_tab_mirroring(device_name)150expect { driver.stop_casting(device_name) }.not_to raise_exception151end152end153154def get_permission(name)155driver.execute_async_script('callback = arguments[arguments.length - 1];' \156'callback(navigator.permissions.query({name: arguments[0]}));', name)['state']157end158159it 'can set single permissions' do160driver.navigate.to url_for('xhtmlTest.html')161162expect(get_permission('clipboard-read')).to eq('prompt')163expect(get_permission('clipboard-write')).to eq('granted')164165driver.add_permission('clipboard-read', 'denied')166driver.add_permission('clipboard-write', 'prompt')167168expect(get_permission('clipboard-read')).to eq('denied')169expect(get_permission('clipboard-write')).to eq('prompt')170171reset_driver!172end173174it 'can set multiple permissions' do175driver.navigate.to url_for('xhtmlTest.html')176177expect(get_permission('clipboard-read')).to eq('prompt')178expect(get_permission('clipboard-write')).to eq('granted')179180driver.add_permissions('clipboard-read' => 'denied', 'clipboard-write' => 'prompt')181182expect(get_permission('clipboard-read')).to eq('denied')183expect(get_permission('clipboard-write')).to eq('prompt')184end185end186end # Chrome187end # WebDriver188end # Selenium189190191