Path: blob/trunk/rb/spec/integration/selenium/webdriver/bidi/network_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 WebDriver23class BiDi24describe Network, exclusive: {bidi: true, reason: 'only executed when bidi is enabled'},25only: {browser: %i[chrome edge firefox]} do26after { |example| reset_driver!(example: example) }2728it 'adds an intercept' do29network = described_class.new(driver.bidi)30intercept = network.add_intercept(phases: [described_class::PHASES[:before_request]])31expect(intercept).not_to be_nil32end3334it 'adds an intercept with a default pattern type' do35network = described_class.new(driver.bidi)36pattern = 'http://localhost:4444/formPage.html'37intercept = network.add_intercept(phases: [described_class::PHASES[:before_request]], url_patterns: pattern)38expect(intercept).not_to be_nil39end4041it 'adds an intercept with a url pattern' do42network = described_class.new(driver.bidi)43pattern = 'http://localhost:4444/formPage.html'44intercept = network.add_intercept(phases: [described_class::PHASES[:before_request]],45url_patterns: pattern,46pattern_type: :url)47expect(intercept).not_to be_nil48end4950it 'removes an intercept' do51network = described_class.new(driver.bidi)52intercept = network.add_intercept(phases: [described_class::PHASES[:before_request]])53expect(network.remove_intercept(intercept['intercept'])).to be_empty54end5556it 'continues with auth' do57username = SpecSupport::RackServer::TestApp::BASIC_AUTH_CREDENTIALS.first58password = SpecSupport::RackServer::TestApp::BASIC_AUTH_CREDENTIALS.last59network = described_class.new(driver.bidi)60phases = [Selenium::WebDriver::BiDi::Network::PHASES[:auth_required]]61network.add_intercept(phases: phases)62network.on(:auth_required) do |event|63request_id = event['request']['request']64network.continue_with_auth(request_id, username, password)65end6667driver.navigate.to url_for('basicAuth')68expect(driver.find_element(tag_name: 'h1').text).to eq('authorized')69end7071it 'continues without auth' do72network = described_class.new(driver.bidi)73network.add_intercept(phases: [described_class::PHASES[:auth_required]])74network.on(:auth_required) do |event|75request_id = event['request']['request']76network.continue_without_auth(request_id)77end7879expect { driver.navigate.to url_for('basicAuth') }.to raise_error(Error::WebDriverError)80end8182it 'cancels auth' do83network = described_class.new(driver.bidi)84network.add_intercept(phases: [described_class::PHASES[:auth_required]])85network.on(:auth_required) do |event|86request_id = event['request']['request']87network.cancel_auth(request_id)88end8990driver.navigate.to url_for('basicAuth')91expect(driver.find_element(tag_name: 'pre').text).to eq('Login please')92end9394it 'continues request' do95network = described_class.new(driver.bidi)96network.add_intercept(phases: [described_class::PHASES[:before_request]])97network.on(:before_request) do |event|98request_id = event['request']['request']99network.continue_request(id: request_id)100end101102driver.navigate.to url_for('formPage.html')103expect(driver.find_element(name: 'login')).to be_displayed104end105106it 'fails request' do107network = described_class.new(driver.bidi)108network.add_intercept(phases: [described_class::PHASES[:before_request]])109network.on(:before_request) do |event|110request_id = event['request']['request']111network.fail_request(request_id)112end113114expect { driver.navigate.to url_for('formPage.html') }.to raise_error(Error::WebDriverError)115end116117it 'continues response' do118network = described_class.new(driver.bidi)119network.add_intercept(phases: [described_class::PHASES[:response_started]])120network.on(:response_started) do |event|121request_id = event['request']['request']122network.continue_response(id: request_id)123end124125driver.navigate.to url_for('formPage.html')126expect(driver.find_element(name: 'login')).to be_displayed127end128129it 'provides response', except: {browser: :firefox,130reason: 'https://github.com/w3c/webdriver-bidi/issues/747'} do131network = described_class.new(driver.bidi)132network.add_intercept(phases: [described_class::PHASES[:response_started]])133network.on(:response_started) do |event|134request_id = event['request']['request']135network.provide_response(136id: request_id,137status: 200,138headers: [139{140name: 'foo',141value: {142type: 'string',143value: 'bar'144}145}146],147body: {148type: 'string',149value: '<html><head><title>Hello World!</title></head><body/></html>'150}151)152end153154driver.navigate.to url_for('formPage.html')155source = driver.page_source156expect(source).not_to include('There should be a form here:')157expect(source).to include('Hello World!')158end159160it 'sets the cache to bypass' do161browsing_context = BrowsingContext.new(driver).create162network = described_class.new(driver.bidi)163network.set_cache_behavior('bypass', browsing_context)164expect(network.set_cache_behavior('bypass', browsing_context)).to be_a(Hash)165end166167it 'sets the cache to default' do168browsing_context = BrowsingContext.new(driver).create169network = described_class.new(driver.bidi)170expect(network.set_cache_behavior('default', browsing_context)).to be_a(Hash)171end172end173end174end175end176177178