Path: blob/trunk/rb/spec/integration/selenium/webdriver/network_spec.rb
1864 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 WebDriver23describe Network, exclude: {version: 'beta'},24exclusive: {bidi: true, reason: 'only executed when bidi is enabled'},25only: {browser: %i[chrome edge firefox]} do26let(:username) { SpecSupport::RackServer::TestApp::BASIC_AUTH_CREDENTIALS.first }27let(:password) { SpecSupport::RackServer::TestApp::BASIC_AUTH_CREDENTIALS.last }2829it 'adds an auth handler' do30reset_driver!(web_socket_url: true) do |driver|31network = described_class.new(driver)32network.add_authentication_handler(username, password)33driver.navigate.to url_for('basicAuth')34expect(driver.find_element(tag_name: 'h1').text).to eq('authorized')35expect(network.callbacks.count).to be 136end37end3839it 'adds an auth handler with a filter' do40reset_driver!(web_socket_url: true) do |driver|41network = described_class.new(driver)42network.add_authentication_handler(username, password, url_for('basicAuth'))43driver.navigate.to url_for('basicAuth')44expect(driver.find_element(tag_name: 'h1').text).to eq('authorized')45expect(network.callbacks.count).to be 146end47end4849it 'adds an auth handler with multiple filters' do50reset_driver!(web_socket_url: true) do |driver|51network = described_class.new(driver)52network.add_authentication_handler(username, password, url_for('basicAuth'), url_for('formPage.html'))53driver.navigate.to url_for('basicAuth')54expect(driver.find_element(tag_name: 'h1').text).to eq('authorized')55expect(network.callbacks.count).to be 156end57end5859it 'adds an auth handler with a pattern type' do60reset_driver!(web_socket_url: true) do |driver|61network = described_class.new(driver)62network.add_authentication_handler(username, password, url_for('basicAuth'), pattern_type: :url)63driver.navigate.to url_for('basicAuth')64expect(driver.find_element(tag_name: 'h1').text).to eq('authorized')65expect(network.callbacks.count).to be 166end67end6869it 'removes an auth handler' do70reset_driver!(web_socket_url: true) do |driver|71network = described_class.new(driver)72id = network.add_authentication_handler(username, password)73network.remove_handler(id)74expect(network.callbacks.count).to be 075end76end7778it 'clears all auth handlers' do79reset_driver!(web_socket_url: true) do |driver|80network = described_class.new(driver)812.times { network.add_authentication_handler(username, password) }82network.clear_handlers83expect(network.callbacks.count).to be 084end85end8687it 'continues without auth' do88reset_driver!(web_socket_url: true) do |driver|89network = described_class.new(driver)90network.add_authentication_handler(&:skip)91expect { driver.navigate.to url_for('basicAuth') }.to raise_error(Error::WebDriverError)92end93end9495it 'cancels auth' do96reset_driver!(web_socket_url: true) do |driver|97network = described_class.new(driver)98network.add_authentication_handler(&:cancel)99driver.navigate.to url_for('basicAuth')100expect(driver.find_element(tag_name: 'pre').text).to eq('Login please')101end102end103104it 'adds a request handler' do105reset_driver!(web_socket_url: true) do |driver|106network = described_class.new(driver)107network.add_request_handler(&:continue)108driver.navigate.to url_for('formPage.html')109expect(driver.current_url).to eq(url_for('formPage.html'))110expect(network.callbacks.count).to be 1111end112end113114it 'adds a request handler with a filter' do115reset_driver!(web_socket_url: true) do |driver|116network = described_class.new(driver)117network.add_request_handler(url_for('formPage.html'), &:continue)118driver.navigate.to url_for('formPage.html')119expect(driver.current_url).to eq(url_for('formPage.html'))120expect(network.callbacks.count).to be 1121end122end123124it 'adds a request handler with multiple filters' do125reset_driver!(web_socket_url: true) do |driver|126network = described_class.new(driver)127network.add_request_handler(url_for('formPage.html'), url_for('basicAuth'), &:continue)128driver.navigate.to url_for('formPage.html')129expect(driver.current_url).to eq(url_for('formPage.html'))130expect(network.callbacks.count).to be 1131end132end133134it 'adds a request handler with a pattern type' do135reset_driver!(web_socket_url: true) do |driver|136network = described_class.new(driver)137network.add_request_handler(url_for('formPage.html'), pattern_type: :url, &:continue)138driver.navigate.to url_for('formPage.html')139expect(driver.current_url).to eq(url_for('formPage.html'))140expect(network.callbacks.count).to be 1141end142end143144it 'adds a request handler with attributes' do145reset_driver!(web_socket_url: true) do |driver|146network = described_class.new(driver)147network.add_request_handler do |request|148request.method = 'GET'149request.url = url_for('formPage.html')150request.headers['foo'] = 'bar'151request.headers['baz'] = 'qux'152request.cookies({153name: 'test',154value: 'value4',155domain: 'example.com',156path: '/path',157size: 1234,158httpOnly: true,159secure: true,160sameSite: 'Strict',161expiry: 1234162})163request.body = ({test: 'example'})164request.continue165end166driver.navigate.to url_for('formPage.html')167expect(driver.current_url).to eq(url_for('formPage.html'))168expect(network.callbacks.count).to be 1169end170end171172it 'fails a request' do173reset_driver!(web_socket_url: true) do |driver|174network = described_class.new(driver)175network.add_request_handler(&:fail)176expect(network.callbacks.count).to be 1177expect { driver.navigate.to url_for('formPage.html') }.to raise_error(Error::WebDriverError)178end179end180181it 'removes a request handler' do182reset_driver!(web_socket_url: true) do |driver|183network = described_class.new(driver)184id = network.add_request_handler(&:continue)185network.remove_handler(id)186expect(network.callbacks.count).to be 0187end188end189190it 'clears all request handlers' do191reset_driver!(web_socket_url: true) do |driver|192network = described_class.new(driver)1932.times { network.add_request_handler(&:continue) }194network.clear_handlers195expect(network.callbacks.count).to be 0196end197end198199it 'adds a response handler' do200reset_driver!(web_socket_url: true) do |driver|201network = described_class.new(driver)202network.add_response_handler(&:continue)203driver.navigate.to url_for('formPage.html')204expect(driver.current_url).to eq(url_for('formPage.html'))205expect(network.callbacks.count).to be 1206end207end208209it 'adds a response handler with a filter' do210reset_driver!(web_socket_url: true) do |driver|211network = described_class.new(driver)212network.add_response_handler(url_for('formPage.html'), &:continue)213driver.navigate.to url_for('formPage.html')214expect(driver.find_element(name: 'login')).to be_displayed215expect(network.callbacks.count).to be 1216end217end218219it 'adds a response handler with multiple filters' do220reset_driver!(web_socket_url: true) do |driver|221network = described_class.new(driver)222network.add_response_handler(url_for('formPage.html'), url_for('basicAuth'), &:continue)223driver.navigate.to url_for('formPage.html')224expect(driver.find_element(name: 'login')).to be_displayed225expect(network.callbacks.count).to be 1226end227end228229it 'adds a response handler with a pattern type' do230reset_driver!(web_socket_url: true) do |driver|231network = described_class.new(driver)232network.add_response_handler(url_for('formPage.html'), pattern_type: :url, &:continue)233driver.navigate.to url_for('formPage.html')234expect(driver.current_url).to eq(url_for('formPage.html'))235expect(network.callbacks.count).to be 1236end237end238239it 'adds a response handler with attributes' do240reset_driver!(web_socket_url: true) do |driver|241network = described_class.new(driver)242network.add_response_handler do |response|243response.reason = 'OK'244response.headers['foo'] = 'bar'245response.credentials.username = 'foo'246response.credentials.password = 'bar'247response.cookies({248name: 'foo',249domain: 'localhost',250httpOnly: true,251expiry: '1_000_000',252maxAge: 1_000,253path: '/',254sameSite: 'none',255secure: false256})257response.continue258end259driver.navigate.to url_for('formPage.html')260expect(driver.current_url).to eq(url_for('formPage.html'))261expect(network.callbacks.count).to be 1262end263end264265it 'adds a response handler that provides a response',266except: {browser: :firefox,267reason: 'https://github.com/w3c/webdriver-bidi/issues/747'} do268reset_driver!(web_socket_url: true) do |driver|269network = described_class.new(driver)270network.add_response_handler do |response|271response.status = 200272response.headers['foo'] = 'bar'273response.body = '<html><head><title>Hello World!</title></head><body/></html>'274response.provide_response275end276driver.navigate.to url_for('formPage.html')277source = driver.page_source278expect(source).not_to include('There should be a form here:')279expect(source).to include('Hello World!')280end281end282283it 'removes a response handler' do284reset_driver!(web_socket_url: true) do |driver|285network = described_class.new(driver)286id = network.add_response_handler(&:continue)287network.remove_handler(id)288network.clear_handlers289expect(network.callbacks.count).to be 0290end291end292293it 'clears all response handlers' do294reset_driver!(web_socket_url: true) do |driver|295network = described_class.new(driver)2962.times { network.add_response_handler(&:continue) }297network.clear_handlers298expect(network.callbacks.count).to be 0299end300end301end302end303end304305306