Path: blob/trunk/javascript/selenium-webdriver/test/lib/webdriver_network_test.js
2885 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 } = require('../../lib/test')22const until = require('selenium-webdriver/lib/until')23const { By } = require('selenium-webdriver')2425suite(26function (env) {27let driver2829beforeEach(async function () {30driver = await env.builder().build()31})3233afterEach(async function () {34await driver.quit()35})3637describe('script()', function () {38it('can add authentication handler', async function () {39await driver.network().addAuthenticationHandler('genie', 'bottle')40await driver.get(Pages.basicAuth)4142await driver.wait(until.elementLocated(By.css('pre')))43let source = await driver.getPageSource()44assert.equal(source.includes('Access granted'), true)45})4647it('can add authentication handler with filter', async function () {48await driver.network().addAuthenticationHandler('genie', 'bottle', 'basicAuth')49await driver.get(Pages.basicAuth)5051await driver.wait(until.elementLocated(By.css('pre')))52let source = await driver.getPageSource()53assert.equal(source.includes('Access granted'), true)54})5556it('can add multiple authentication handlers with filter', async function () {57await driver.network().addAuthenticationHandler('genie', 'bottle', 'basicAuth')58await driver.network().addAuthenticationHandler('test', 'test', 'test')59await driver.get(Pages.basicAuth)6061await driver.wait(until.elementLocated(By.css('pre')))62let source = await driver.getPageSource()63assert.equal(source.includes('Access granted'), true)64})6566it('can add multiple authentication handlers with the same filter', async function () {67await driver.network().addAuthenticationHandler('genie', 'bottle', 'basicAuth')68await driver.network().addAuthenticationHandler('genie', 'bottle', 'basicAuth')69await driver.get(Pages.basicAuth)7071await driver.wait(until.elementLocated(By.css('pre')))72let source = await driver.getPageSource()73assert.equal(source.includes('Access granted'), true)74})7576it('can remove authentication handler', async function () {77const id = await driver.network().addAuthenticationHandler('genie', 'bottle')7879await driver.network().removeAuthenticationHandler(id)8081try {82await driver.get(Pages.basicAuth)83await driver.wait(until.elementLocated(By.css('pre')))84assert.fail('Page should not be loaded')85} catch (e) {86assert.strictEqual(e.name, 'UnexpectedAlertOpenError')87}88})8990it('throws an error when remove authentication handler that does not exist', async function () {91try {92await driver.network().removeAuthenticationHandler(10)93assert.fail('Expected error not thrown. Non-existent handler cannot be removed')94} catch (e) {95assert.strictEqual(e.message, 'Callback with id 10 not found')96}97})9899it('can clear authentication handlers', async function () {100await driver.network().addAuthenticationHandler('genie', 'bottle', 'basicAuth')101102await driver.network().addAuthenticationHandler('bottle', 'genie')103104await driver.network().clearAuthenticationHandlers()105106try {107await driver.get(Pages.basicAuth)108await driver.wait(until.elementLocated(By.css('pre')))109assert.fail('Page should not be loaded')110} catch (e) {111assert.strictEqual(e.name, 'UnexpectedAlertOpenError')112}113})114})115},116{ browsers: [Browser.FIREFOX] },117)118119120