Path: blob/trunk/javascript/selenium-webdriver/test/bidi/network_test.js
2887 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 } = require('selenium-webdriver')21const { Pages, suite, ignore } = require('../../lib/test')22const { Network, CacheBehavior } = require('selenium-webdriver/bidi/network')23const BrowsingContext = require('selenium-webdriver/bidi/browsingContext')24const until = require('selenium-webdriver/lib/until')2526suite(27function (env) {28let driver29let network3031beforeEach(async function () {32driver = await env.builder().build()33network = await Network(driver)34})3536afterEach(async function () {37await network.close()38await driver.quit()39})4041describe('Network network', function () {42it('can listen to event before request is sent', async function () {43let beforeRequestEvent = null44await network.beforeRequestSent(function (event) {45if (event.request.url.includes('empty')) {46beforeRequestEvent = event47}48})4950await driver.get(Pages.emptyPage)5152assert.equal(beforeRequestEvent.request.method, 'GET')53const url = beforeRequestEvent.request.url54assert.equal(url, await driver.getCurrentUrl())55})5657it('can request cookies', async function () {58let beforeRequestEvent = null59await network.beforeRequestSent(function (event) {60beforeRequestEvent = event61})6263await driver.get(Pages.emptyText)64await driver.manage().addCookie({65name: 'north',66value: 'biryani',67})68await driver.navigate().refresh()6970assert.equal(beforeRequestEvent.request.method, 'GET')71assert.equal(beforeRequestEvent.request.cookies[0].name, 'north')72assert.equal(beforeRequestEvent.request.cookies[0].value.value, 'biryani')73const url = beforeRequestEvent.request.url74assert.equal(url, await driver.getCurrentUrl())7576await driver.manage().addCookie({77name: 'south',78value: 'dosa',79})80await driver.navigate().refresh()8182assert.equal(beforeRequestEvent.request.cookies[1].name, 'south')83assert.equal(beforeRequestEvent.request.cookies[1].value.value, 'dosa')84})8586ignore(env.browsers(Browser.CHROME, Browser.EDGE)).it('can redirect http equiv', async function () {87let beforeRequestEvent = []88await network.beforeRequestSent(function (event) {89beforeRequestEvent.push(event)90})9192await driver.get(Pages.redirectedHttpEquiv)93await driver.wait(until.urlContains('redirected.html'), 1000)9495assert.equal(beforeRequestEvent[0].request.method, 'GET')96assert(beforeRequestEvent[0].request.url.includes('redirected_http_equiv.html'))97assert.equal(beforeRequestEvent[2].request.method, 'GET')98assert(beforeRequestEvent[2].request.url.includes('redirected.html'))99})100101it('can subscribe to response started', async function () {102let onResponseStarted = []103await network.responseStarted(function (event) {104onResponseStarted.push(event)105})106107await driver.get(Pages.emptyText)108109assert.equal(onResponseStarted[0].request.method, 'GET')110assert.equal(onResponseStarted[0].request.url, await driver.getCurrentUrl())111assert.equal(onResponseStarted[0].response.url, await driver.getCurrentUrl())112assert.equal(onResponseStarted[0].response.fromCache, false)113assert(onResponseStarted[0].response.mimeType.includes('text/plain'))114assert.equal(onResponseStarted[0].response.status, 200)115assert.equal(onResponseStarted[0].response.statusText, 'OK')116})117118it('test response started mime type', async function () {119let onResponseStarted = []120await network.responseStarted(function (event) {121onResponseStarted.push(event)122})123124// Checking mime type for 'html' text125await driver.get(Pages.emptyPage)126assert.equal(onResponseStarted[0].request.method, 'GET')127assert.equal(onResponseStarted[0].request.url, await driver.getCurrentUrl())128assert.equal(onResponseStarted[0].response.url, await driver.getCurrentUrl())129assert(onResponseStarted[0].response.mimeType.includes('text/html'))130131// Checking mime type for 'plain' text132onResponseStarted = []133await driver.get(Pages.emptyText)134assert.equal(onResponseStarted[0].response.url, await driver.getCurrentUrl())135assert(onResponseStarted[0].response.mimeType.includes('text/plain'))136})137138it('can subscribe to response completed', async function () {139let onResponseCompleted = []140await network.responseCompleted(function (event) {141onResponseCompleted.push(event)142})143144await driver.get(Pages.emptyPage)145146assert.equal(onResponseCompleted[0].request.method, 'GET')147assert.equal(onResponseCompleted[0].request.url, await driver.getCurrentUrl())148assert.equal(onResponseCompleted[0].response.url, await driver.getCurrentUrl())149assert.equal(onResponseCompleted[0].response.fromCache, false)150assert(onResponseCompleted[0].response.mimeType.includes('text/html'))151assert.equal(onResponseCompleted[0].response.status, 200)152assert.equal(onResponseCompleted[0].response.statusText, 'OK')153assert.equal(onResponseCompleted[0].redirectCount, 0)154})155156ignore(env.browsers(Browser.CHROME, Browser.EDGE)).it('can listen to auth required event', async function () {157let authRequiredEvent = null158await network.authRequired(function (event) {159authRequiredEvent = event160})161162await driver.get(Pages.basicAuth)163164const url = authRequiredEvent.request.url165assert.equal(authRequiredEvent.id, await driver.getWindowHandle())166assert.equal(authRequiredEvent.request.method, 'GET')167assert.equal(url.includes('basicAuth'), true)168169assert.equal(authRequiredEvent.response.status, 401)170assert.equal(authRequiredEvent.response.headers.length > 1, true)171assert.equal(authRequiredEvent.response.url.includes('basicAuth'), true)172})173174it('can listen to fetch error event', async function () {175let fetchErrorEvent = null176await network.fetchError(function (event) {177fetchErrorEvent = event178})179180try {181await driver.get('https://not_a_valid_url.test/')182/*eslint no-unused-vars: "off"*/183} catch (e) {184// ignore185}186187const url = fetchErrorEvent.request.url188assert.equal(fetchErrorEvent.id, await driver.getWindowHandle())189assert.equal(fetchErrorEvent.request.method, 'GET')190assert.equal(url.includes('valid_url'), true)191assert.equal(fetchErrorEvent.request.headers.length > 1, true)192assert.notEqual(fetchErrorEvent.errorText, null)193})194195it('test response completed mime type', async function () {196let onResponseCompleted = []197await network.responseCompleted(function (event) {198onResponseCompleted.push(event)199})200201// Checking mime type for 'html' text202await driver.get(Pages.emptyPage)203assert.equal(onResponseCompleted[0].request.method, 'GET')204assert.equal(onResponseCompleted[0].request.url, await driver.getCurrentUrl())205assert.equal(onResponseCompleted[0].response.url, await driver.getCurrentUrl())206assert(onResponseCompleted[0].response.mimeType.includes('text/html'))207208// Checking mime type for 'plain' text209onResponseCompleted = []210await driver.get(Pages.emptyText)211assert.equal(onResponseCompleted[0].response.url, await driver.getCurrentUrl())212assert(onResponseCompleted[0].response.mimeType.includes('text/plain'))213})214})215216describe('setCacheBehavior', function () {217it('can set cache behavior to bypass for a context', async function () {218await driver.get(Pages.emptyPage)219const browsingContext = await BrowsingContext(driver, {220type: 'tab',221})222const contextId = browsingContext.id223await network.setCacheBehavior(CacheBehavior.BYPASS, [contextId])224})225226it('can set cache behavior to default for a context', async function () {227await driver.get(Pages.emptyPage)228const browsingContext = await BrowsingContext(driver, {229type: 'tab',230})231const contextId = browsingContext.id232await network.setCacheBehavior(CacheBehavior.DEFAULT, [contextId])233})234235it('can set cache behavior to default/bypass with no context id', async function () {236await driver.get(Pages.emptyPage)237await network.setCacheBehavior(CacheBehavior.DEFAULT)238await network.setCacheBehavior(CacheBehavior.BYPASS)239})240241it('throws error for invalid cache behavior', async function () {242await driver.get(Pages.emptyPage)243await assert.rejects(244async () => await network.setCacheBehavior('invalid'),245/Cache behavior must be either "default" or "bypass"/,246)247})248249it('throws error for invalid context id types', async function () {250await driver.get(Pages.emptyPage)251await assert.rejects(252async () => await network.setCacheBehavior(CacheBehavior.BYPASS, ''),253/Contexts must be an array of non-empty strings/,254)255await assert.rejects(256async () => await network.setCacheBehavior(CacheBehavior.BYPASS, ['', ' ']),257/Contexts must be an array of non-empty strings/,258)259})260})261},262{ browsers: [Browser.FIREFOX, Browser.CHROME, Browser.EDGE] },263)264265266