Path: blob/trunk/javascript/selenium-webdriver/test/logging_test.js
2884 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 test = require('../lib/test')21const { Browser, logging } = require('selenium-webdriver')2223test.suite(function (env) {24// Logging API is not supported in IE.25// Logging API not supported in Marionette.26// Logging API not supported in Safari.27test28.ignore(env.browsers(Browser.INTERNET_EXPLORER, Browser.SAFARI, Browser.FIREFOX, Browser.CHROME))29.describe('logging', function () {30var driver3132beforeEach(function () {33driver = null34})3536afterEach(async function () {37if (driver) {38return driver.quit()39}40})4142it('can be disabled', async function () {43var prefs = new logging.Preferences()44prefs.setLevel(logging.Type.BROWSER, logging.Level.OFF)4546driver = await env.builder().setLoggingPrefs(prefs).build()4748await driver.get(49dataUrl(50'<!DOCTYPE html><script>',51'console.info("hello");',52'console.warn("this is a warning");',53'console.error("and this is an error");',54'</script>',55),56)57return driver58.manage()59.logs()60.get(logging.Type.BROWSER)61.then((entries) => assert.strictEqual(entries.length, 0))62})6364// Firefox does not capture JS error console log messages.65test.ignore(env.browsers(Browser.FIREFOX, 'legacy-firefox')).it('can be turned down', async function () {66var prefs = new logging.Preferences()67prefs.setLevel(logging.Type.BROWSER, logging.Level.SEVERE)6869driver = await env.builder().setLoggingPrefs(prefs).build()7071await driver.get(72dataUrl(73'<!DOCTYPE html><script>',74'console.info("hello");',75'console.warn("this is a warning");',76'console.error("and this is an error");',77'</script>',78),79)80return driver81.manage()82.logs()83.get(logging.Type.BROWSER)84.then(function (entries) {85assert.strictEqual(entries.length, 1)86assert.strictEqual(entries[0].level.name, 'SEVERE')87// eslint-disable-next-line no-useless-escape88assert.ok(/.*\"?and this is an error\"?/.test(entries[0].message))89})90})9192// Firefox does not capture JS error console log messages.93test.ignore(env.browsers(Browser.FIREFOX, 'legacy-firefox')).it('can be made verbose', async function () {94var prefs = new logging.Preferences()95prefs.setLevel(logging.Type.BROWSER, logging.Level.DEBUG)9697driver = await env.builder().setLoggingPrefs(prefs).build()9899await driver.get(100dataUrl(101'<!DOCTYPE html><script>',102'console.debug("hello");',103'console.warn("this is a warning");',104'console.error("and this is an error");',105'</script>',106),107)108return driver109.manage()110.logs()111.get(logging.Type.BROWSER)112.then(function (entries) {113assert.strictEqual(entries.length, 3)114assert.strictEqual(entries[0].level.name, 'DEBUG')115// eslint-disable-next-line no-useless-escape116assert.ok(/.*\"?hello\"?/.test(entries[0].message))117118assert.strictEqual(entries[1].level.name, 'WARNING')119// eslint-disable-next-line no-useless-escape120assert.ok(/.*\"?this is a warning\"?/.test(entries[1].message))121122assert.strictEqual(entries[2].level.name, 'SEVERE')123// eslint-disable-next-line no-useless-escape124assert.ok(/.*\"?and this is an error\"?/.test(entries[2].message))125})126})127128// Firefox does not capture JS error console log messages.129test130.ignore(env.browsers(Browser.FIREFOX, 'legacy-firefox'))131.it('clears records after retrieval', async function () {132var prefs = new logging.Preferences()133prefs.setLevel(logging.Type.BROWSER, logging.Level.DEBUG)134135driver = await env.builder().setLoggingPrefs(prefs).build()136137await driver.get(138dataUrl(139'<!DOCTYPE html><script>',140'console.debug("hello");',141'console.warn("this is a warning");',142'console.error("and this is an error");',143'</script>',144),145)146await driver147.manage()148.logs()149.get(logging.Type.BROWSER)150.then((entries) => assert.strictEqual(entries.length, 3))151return driver152.manage()153.logs()154.get(logging.Type.BROWSER)155.then((entries) => assert.strictEqual(entries.length, 0))156})157158it('does not mix log types', async function () {159var prefs = new logging.Preferences()160prefs.setLevel(logging.Type.BROWSER, logging.Level.DEBUG)161prefs.setLevel(logging.Type.DRIVER, logging.Level.SEVERE)162163driver = await env.builder().setLoggingPrefs(prefs).build()164165await driver.get(166dataUrl(167'<!DOCTYPE html><script>',168'console.debug("hello");',169'console.warn("this is a warning");',170'console.error("and this is an error");',171'</script>',172),173)174return driver175.manage()176.logs()177.get(logging.Type.DRIVER)178.then((entries) => assert.strictEqual(entries.length, 0))179})180})181182function dataUrl(...args) {183return 'data:text/html,' + args.join('')184}185})186187188