Path: blob/trunk/javascript/selenium-webdriver/test/bidi/generated/log_test.js
11822 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 { suite, ignore, Pages } = require('../../../lib/test')21const { Browser } = require('selenium-webdriver')2223const { Log } = require('selenium-webdriver/bidi/generated/log')2425suite(26function (env) {27let driver28let log2930beforeEach(async function () {31driver = await env.builder().build()32log = await Log.create(driver)33})3435afterEach(function () {36return driver.quit()37})3839describe('onConsoleEntry', function () {40it('can listen to console.log', async function () {41let entry = null4243await log.onConsoleEntry((params) => {44if (params.text === 'Hello, world!') {45entry = params46}47})4849await driver.get(Pages.logEntryAdded)50await driver.findElement({ id: 'consoleLog' }).click()51await driver.wait(() => entry !== null, 5000)5253assert.ok(entry, 'console log entry should have been captured')54assert.strictEqual(entry.type, 'console')55assert.strictEqual(entry.level, 'info')56assert.strictEqual(entry.method, 'log')57assert.strictEqual(entry.text, 'Hello, world!')58assert.ok(entry.source)59// Raw BiDi event params use source.realm (not source.realmId which is60// the hand-coded Source wrapper's getter)61assert.ok(entry.source.realm)62})6364it('can listen to console.error', async function () {65let entry = null6667await log.onConsoleEntry((params) => {68if (params.level === 'error' && params.text) {69entry = params70}71})7273await driver.get(Pages.logEntryAdded)74await driver.findElement({ id: 'consoleError' }).click()75await driver.wait(() => entry !== null, 5000)7677assert.ok(entry, 'console error entry should have been captured')78assert.strictEqual(entry.type, 'console')79assert.strictEqual(entry.level, 'error')80})8182it('can listen to console.warn', async function () {83let entry = null8485await log.onConsoleEntry((params) => {86if (params.level === 'warn' && params.text) {87entry = params88}89})9091await driver.get(Pages.logEntryAdded)92// logEntryAdded.html has no warn button; trigger via script instead93await driver.executeScript('console.warn("test warn message")')94await driver.wait(() => entry !== null, 5000)9596assert.ok(entry, 'console warn entry should have been captured')97assert.strictEqual(entry.level, 'warn')98})99100it('captures args in console log entries', async function () {101let entry = null102103await log.onConsoleEntry((params) => {104if (params.text === 'Hello, world!') {105entry = params106}107})108109await driver.get(Pages.logEntryAdded)110await driver.findElement({ id: 'consoleLog' }).click()111await driver.wait(() => entry !== null, 5000)112113assert.ok(entry)114assert.ok(Array.isArray(entry.args))115assert.ok(entry.args.length > 0)116})117})118119describe('onJavascriptException', function () {120it('can listen to javascript exceptions', async function () {121let entry = null122123await log.onJavascriptException((params) => {124entry = params125})126127await driver.get(Pages.logEntryAdded)128await driver.findElement({ id: 'jsException' }).click()129await driver.wait(() => entry !== null, 5000)130131assert.ok(entry, 'javascript exception entry should have been captured')132assert.strictEqual(entry.type, 'javascript')133assert.strictEqual(entry.level, 'error')134assert.ok(entry.text)135})136})137138describe('onEntryAdded', function () {139it('receives all log entries including console', async function () {140const entries = []141142await log.onEntryAdded((params) => {143entries.push(params)144})145146await driver.get(Pages.logEntryAdded)147await driver.findElement({ id: 'consoleLog' }).click()148await driver.wait(() => entries.length > 0, 5000)149150assert.ok(entries.length > 0, 'should have received at least one log entry')151})152153it('can register multiple listeners', async function () {154let count1 = 0155let count2 = 0156157await log.onConsoleEntry(() => {158count1++159})160await log.onConsoleEntry(() => {161count2++162})163164await driver.get(Pages.logEntryAdded)165await driver.findElement({ id: 'consoleLog' }).click()166await driver.wait(() => count1 >= 1 && count2 >= 1, 5000)167168assert.ok(count1 >= 1)169assert.ok(count2 >= 1)170})171})172173describe('onJavascriptLog', function () {174it('can listen to javascript log entries', async function () {175const entries = []176177await log.onJavascriptLog((params) => {178entries.push(params)179})180181await driver.get(Pages.logEntryAdded)182183// Trigger a JS error to produce a javascript log entry184await driver.findElement({ id: 'jsException' }).click()185await driver.wait(() => entries.length > 0, 5000)186187assert.ok(entries.length > 0, 'onJavascriptLog should have received at least one entry')188assert.strictEqual(entries[0].type, 'javascript')189})190})191},192{ browsers: [Browser.CHROME, Browser.FIREFOX] },193)194195196