Path: blob/trunk/javascript/selenium-webdriver/test/bidi/network_commands_test.js
3991 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617'use strict'1819const assert = require('node:assert')20const { Browser, By } = require('selenium-webdriver')21const { Pages, suite } = require('../../lib/test')22const { Network } = require('selenium-webdriver/bidi/network')23const { AddInterceptParameters } = require('selenium-webdriver/bidi/addInterceptParameters')24const { InterceptPhase } = require('selenium-webdriver/bidi/interceptPhase')25const { until } = require('selenium-webdriver/index')26const { ContinueRequestParameters } = require('selenium-webdriver/bidi/continueRequestParameters')27const { ContinueResponseParameters } = require('selenium-webdriver/bidi/continueResponseParameters')28const { ProvideResponseParameters } = require('selenium-webdriver/bidi/provideResponseParameters')29const { BytesValue, Header } = require('selenium-webdriver/bidi/networkTypes')3031suite(32function (env) {33let driver34let network3536beforeEach(async function () {37driver = await env.builder().build()3839network = await Network(driver)40})4142afterEach(async function () {43await network.close()44await driver.quit()45})4647describe('Network commands', function () {48it('can add intercept', async function () {49const intercept = await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))50assert.notEqual(intercept, null)51})5253it('can remove intercept', async function () {54const network = await Network(driver)55const intercept = await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))56assert.notEqual(intercept, null)5758await network.removeIntercept(intercept)59})6061it('can continue with auth credentials ', async function () {62await network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))6364await network.authRequired(async (event) => {65await network.continueWithAuth(event.request.request, 'genie', 'bottle')66})67await driver.get(Pages.basicAuth)6869await driver.wait(until.elementLocated(By.css('pre')))70let source = await driver.getPageSource()71assert.equal(source.includes('Access granted'), true)72})7374it('can continue without auth credentials ', async function () {75await network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))7677await network.authRequired(async (event) => {78await network.continueWithAuthNoCredentials(event.request.request)79})8081await driver.get(Pages.basicAuth)82const alert = await driver.wait(until.alertIsPresent())83await alert.dismiss()8485await driver.wait(until.elementLocated(By.css('pre')))86let source = await driver.getPageSource()87assert.equal(source.includes('Access denied'), true)88})8990it('can cancel auth ', async function () {91await network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))9293await network.authRequired(async (event) => {94await network.cancelAuth(event.request.request)95})96try {97await driver.wait(until.alertIsPresent(), 3000)98assert.fail('Alert should not be present')99} catch (e) {100assert.strictEqual(e.name, 'TimeoutError')101}102})103104it('can fail request', async function () {105await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))106107await network.beforeRequestSent(async (event) => {108await network.failRequest(event.request.request)109})110111await driver.manage().setTimeouts({ pageLoad: 5000 })112113try {114await driver.get(Pages.basicAuth)115assert.fail('Page should not be loaded')116} catch (e) {117assert.strictEqual(e.name, 'TimeoutError')118}119})120121it('can continue request', async function () {122await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))123124let counter = 0125126await network.beforeRequestSent(async (event) => {127await network.continueRequest(new ContinueRequestParameters(event.request.request))128counter = counter + 1129})130131await driver.get(Pages.logEntryAdded)132133assert.strictEqual(counter >= 1, true)134})135136it('can continue response', async function () {137await network.addIntercept(new AddInterceptParameters(InterceptPhase.RESPONSE_STARTED))138139let counter = 0140141await network.responseStarted(async (event) => {142await network.continueResponse(new ContinueResponseParameters(event.request.request))143counter = counter + 1144})145146await driver.get(Pages.logEntryAdded)147148assert.strictEqual(counter >= 1, true)149})150151it('can provide response', async function () {152await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))153154let counter = 0155156await network.beforeRequestSent(async (event) => {157await network.provideResponse(new ProvideResponseParameters(event.request.request))158counter = counter + 1159})160161await driver.get(Pages.logEntryAdded)162163assert.strictEqual(counter >= 1, true)164})165166it('can provide response with headers', async function () {167await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))168169let counter = 0170171await network.beforeRequestSent(async (event) => {172const headers = [173new Header('content-type', new BytesValue(BytesValue.Type.STRING, 'application/json')),174new Header('x-custom-header', new BytesValue(BytesValue.Type.STRING, 'test-value')),175]176177const body = new BytesValue(178BytesValue.Type.BASE64,179Buffer.from(JSON.stringify({ status: 'ok' })).toString('base64'),180)181182const params = new ProvideResponseParameters(event.request.request)183.statusCode(200)184.body(body)185.headers(headers)186.reasonPhrase('OK')187188await network.provideResponse(params)189counter = counter + 1190})191192await driver.get(Pages.logEntryAdded)193194assert.strictEqual(counter >= 1, true)195})196})197},198{ browsers: [Browser.FIREFOX] },199)200201202