Path: blob/trunk/javascript/selenium-webdriver/test/bidi/log_inspector_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 logInspector = require('selenium-webdriver/bidi/logInspector')23const filterBy = require('selenium-webdriver/bidi/filterBy')2425suite(26function (env) {27let driver2829beforeEach(async function () {30driver = await env.builder().build()31})3233afterEach(async function () {34await driver.quit()35})3637function delay(ms) {38return new Promise((resolve) => setTimeout(resolve, ms))39}4041describe('Log Inspector', function () {42it('can listen to console log', async function () {43const inspector = await logInspector(driver)44await inspector.onConsoleEntry(function (log) {45assert.equal(log.text, 'Hello, world!')46assert.notEqual(log.source.realmId, null)47assert.notEqual(log.source.browsingContextId, null)48assert.equal(log.type, 'console')49assert.equal(log.level, 'info')50assert.equal(log.method, 'log')51assert.equal(log.args.length, 1)52})5354await driver.get(Pages.logEntryAdded)55await driver.findElement({ id: 'consoleLog' }).click()5657await inspector.close()58})5960it('can listen to console log with different consumers', async function () {61let logEntry = null62const inspector = await logInspector(driver)63await inspector.onConsoleEntry(function (log) {64logEntry = log65assert.equal(logEntry.text, 'Hello, world!')66assert.notEqual(log.source.realmId, null)67assert.notEqual(log.source.browsingContextId, null)68assert.equal(logEntry.type, 'console')69assert.equal(logEntry.level, 'info')70assert.equal(logEntry.method, 'log')71assert.equal(logEntry.args.length, 1)72})7374let logEntryText = null75await inspector.onConsoleEntry(function (log) {76logEntryText = log.text77assert.equal(logEntryText, 'Hello, world!')78})7980await driver.get(Pages.logEntryAdded)81await driver.findElement({ id: 'consoleLog' }).click()8283await inspector.close()84})8586it('can filter console info level log', async function () {87let logEntry = null88const inspector = await logInspector(driver)89await inspector.onConsoleEntry(function (log) {90logEntry = log91assert.equal(logEntry.text, 'Hello, world!')92assert.notEqual(log.source.realmId, null)93assert.notEqual(log.source.browsingContextId, null)94assert.equal(logEntry.type, 'console')95assert.equal(logEntry.level, 'info')96assert.equal(logEntry.method, 'log')97assert.equal(logEntry.args.length, 1)98}, filterBy.FilterBy.logLevel('info'))99100await driver.get(Pages.logEntryAdded)101await driver.findElement({ id: 'consoleLog' }).click()102103await inspector.close()104})105106it('can filter console log', async function () {107const inspector = await logInspector(driver)108await inspector.onConsoleEntry(function (log) {109assert.notEqual(log, null)110}, filterBy.FilterBy.logLevel('info'))111112await driver.get(Pages.logEntryAdded)113await driver.findElement({ id: 'consoleLog' }).click()114115await inspector.close()116})117118it('can call multiple callbacks for console log', async function () {119let logEntry1 = null120let logEntry2 = null121let logEntry3 = null122let logEntry4 = null123const inspector = await logInspector(driver)124await inspector.onConsoleEntry(function (log) {125logEntry1 = log126})127128await inspector.onConsoleEntry(function (log) {129logEntry2 = log130})131132await inspector.onConsoleEntry(function (log) {133logEntry3 = log134}, filterBy.FilterBy.logLevel('info'))135136await inspector.onConsoleEntry(function (log) {137logEntry4 = log138}, filterBy.FilterBy.logLevel('error'))139140await driver.get(Pages.logEntryAdded)141await driver.findElement({ id: 'consoleLog' }).click()142143await delay(3000)144145assert.notEqual(logEntry1, null)146assert.notEqual(logEntry2, null)147assert.notEqual(logEntry3, null)148assert.equal(logEntry4, null)149150await inspector.close()151})152153it('can listen to javascript log', async function () {154let logEntry = null155const inspector = await logInspector(driver)156await inspector.onJavascriptLog(function (log) {157logEntry = log158assert.equal(logEntry.text, 'Error: Not working')159assert.equal(logEntry.type, 'javascript')160assert.equal(logEntry.level, 'error')161})162163await driver.get(Pages.logEntryAdded)164await driver.findElement({ id: 'jsException' }).click()165166await inspector.close()167})168169it('can filter javascript log at error level', async function () {170let logEntry = null171const inspector = await logInspector(driver)172await inspector.onJavascriptLog(function (log) {173logEntry = log174assert.equal(logEntry.text, 'Error: Not working')175assert.equal(logEntry.type, 'javascript')176assert.equal(logEntry.level, 'error')177}, filterBy.FilterBy.logLevel('error'))178179await driver.get(Pages.logEntryAdded)180await driver.findElement({ id: 'jsException' }).click()181182await inspector.close()183})184185it('can filter javascript log', async function () {186let logEntry = null187const inspector = await logInspector(driver)188await inspector.onJavascriptLog(function (log) {189logEntry = log190assert.notEqual(logEntry, null)191}, filterBy.FilterBy.logLevel('error'))192193await driver.get(Pages.logEntryAdded)194await driver.findElement({ id: 'jsException' }).click()195196await inspector.close()197})198199it('can call multiple callbacks for javascript log', async function () {200let logEntry1 = null201let logEntry2 = null202let logEntry3 = null203let logEntry4 = null204const inspector = await logInspector(driver)205await inspector.onJavascriptLog(function (log) {206logEntry1 = log207})208209await inspector.onJavascriptLog(function (log) {210logEntry2 = log211})212213await inspector.onJavascriptLog(function (log) {214logEntry3 = log215}, filterBy.FilterBy.logLevel('error'))216217await inspector.onJavascriptLog(function (log) {218logEntry4 = log219}, filterBy.FilterBy.logLevel('info'))220221await driver.get(Pages.logEntryAdded)222await driver.findElement({ id: 'jsException' }).click()223224await delay(3000)225226assert.notEqual(logEntry1, null)227assert.notEqual(logEntry2, null)228assert.notEqual(logEntry3, null)229assert.equal(logEntry4, null)230231await inspector.close()232})233234it('can listen to javascript error log', async function () {235let logEntry = null236const inspector = await logInspector(driver)237await inspector.onJavascriptException(function (log) {238logEntry = log239assert.equal(logEntry.text, 'Error: Not working')240assert.notEqual(log.source.realmId, null)241assert.notEqual(log.source.browsingContextId, null)242assert.equal(logEntry.type, 'javascript')243assert.equal(logEntry.level, 'error')244})245246await driver.get(Pages.logEntryAdded)247await driver.findElement({ id: 'jsException' }).click()248249await inspector.close()250})251252it('can retrieve stack trace for a log', async function () {253let logEntry = null254const inspector = await logInspector(driver)255await inspector.onJavascriptException(function (log) {256logEntry = log257const stackTrace = logEntry.stackTrace258assert.notEqual(stackTrace, null)259assert.equal(stackTrace.callFrames.length > 0, true)260})261262await driver.get(Pages.logEntryAdded)263await driver.findElement({ id: 'jsException' }).click()264265await inspector.close()266})267268it('can listen to any log', async function () {269let logEntry = null270const inspector = await logInspector(driver)271await inspector.onLog(function (log) {272logEntry = log273assert.equal(logEntry.text, 'Hello, world!')274assert.notEqual(log.source.realmId, null)275assert.notEqual(log.source.browsingContextId, null)276assert.equal(logEntry.type, 'console')277assert.equal(logEntry.level, 'info')278assert.equal(logEntry.method, 'log')279assert.equal(logEntry.args.length, 1)280})281282await driver.get(Pages.logEntryAdded)283await driver.findElement({ id: 'consoleLog' }).click()284285await inspector.close()286})287288it('can filter any log', async function () {289let logEntry = null290const inspector = await logInspector(driver)291await inspector.onLog(function (log) {292logEntry = log293assert.equal(logEntry.text, 'Hello, world!')294assert.notEqual(log.source.realmId, null)295assert.notEqual(log.source.browsingContextId, null)296assert.equal(logEntry.type, 'console')297assert.equal(logEntry.level, 'info')298assert.equal(logEntry.method, 'log')299assert.equal(logEntry.args.length, 1)300}, filterBy.FilterBy.logLevel('info'))301302await driver.get(Pages.logEntryAdded)303await driver.findElement({ id: 'consoleLog' }).click()304305await inspector.close()306})307308it('can filter any log at error level', async function () {309let logEntry = null310const inspector = await logInspector(driver)311await inspector.onLog(function (log) {312logEntry = log313assert.equal(logEntry.text, 'Error: Not working')314assert.equal(logEntry.type, 'javascript')315assert.equal(logEntry.level, 'error')316}, filterBy.FilterBy.logLevel('error'))317318await driver.get(Pages.logEntryAdded)319await driver.findElement({ id: 'jsException' }).click()320321await inspector.close()322})323324it('can call multiple callbacks for any log', async function () {325let logEntry1 = null326let logEntry2 = null327let logEntry3 = null328let logEntry4 = null329const inspector = await logInspector(driver)330await inspector.onLog(function (log) {331logEntry1 = log332})333334await inspector.onLog(function (log) {335logEntry2 = log336})337338await inspector.onLog(function (log) {339logEntry3 = log340}, filterBy.FilterBy.logLevel('error'))341342await inspector.onLog(function (log) {343logEntry4 = log344}, filterBy.FilterBy.logLevel('info'))345346await driver.get(Pages.logEntryAdded)347await driver.findElement({ id: 'jsException' }).click()348349await delay(3000)350351assert.notEqual(logEntry1, null)352assert.notEqual(logEntry2, null)353assert.notEqual(logEntry3, null)354assert.equal(logEntry4, null)355356await inspector.close()357})358359it('can call multiple callbacks for any log with filter', async function () {360let logEntry1 = null361let logEntry2 = null362const inspector = await logInspector(driver)363await inspector.onLog(function (log) {364logEntry1 = log365}, filterBy.FilterBy.logLevel('error'))366367await inspector.onLog(function (log) {368logEntry2 = log369}, filterBy.FilterBy.logLevel('error'))370371await driver.get(Pages.logEntryAdded)372await driver.findElement({ id: 'jsException' }).click()373374await delay(3000)375376assert.notEqual(logEntry1, null)377assert.notEqual(logEntry2, null)378379await inspector.close()380})381})382},383{ browsers: [Browser.FIREFOX, Browser.CHROME, Browser.EDGE] },384)385386387