|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +'use strict' |
| 19 | + |
| 20 | +const assert = require('node:assert') |
| 21 | +const { Browser } = require('../../') |
| 22 | +const { Pages, suite } = require('../../lib/test') |
| 23 | +const { until } = require('../../index') |
| 24 | + |
| 25 | +suite( |
| 26 | + function (env) { |
| 27 | + let driver |
| 28 | + |
| 29 | + beforeEach(async function () { |
| 30 | + driver = await env.builder().build() |
| 31 | + }) |
| 32 | + |
| 33 | + afterEach(async function () { |
| 34 | + await driver.quit() |
| 35 | + }) |
| 36 | + |
| 37 | + function delay(ms) { |
| 38 | + return new Promise((resolve) => setTimeout(resolve, ms)) |
| 39 | + } |
| 40 | + |
| 41 | + describe('script()', function () { |
| 42 | + it('can listen to console log', async function () { |
| 43 | + let log = null |
| 44 | + const handler = await driver.script().addConsoleMessageHandler((logEntry) => { |
| 45 | + log = logEntry |
| 46 | + }) |
| 47 | + |
| 48 | + await driver.get(Pages.logEntryAdded) |
| 49 | + await driver.findElement({ id: 'consoleLog' }).click() |
| 50 | + |
| 51 | + await delay(3000) |
| 52 | + |
| 53 | + assert.equal(log.text, 'Hello, world!') |
| 54 | + assert.equal(log.realm, null) |
| 55 | + assert.equal(log.type, 'console') |
| 56 | + assert.equal(log.level, 'info') |
| 57 | + assert.equal(log.method, 'log') |
| 58 | + assert.equal(log.args.length, 1) |
| 59 | + await driver.script().removeConsoleMessageHandler(handler) |
| 60 | + }) |
| 61 | + |
| 62 | + it('can listen to javascript error', async function () { |
| 63 | + let log = null |
| 64 | + const handler = await driver.script().addJavaScriptErrorHandler((logEntry) => { |
| 65 | + log = logEntry |
| 66 | + }) |
| 67 | + |
| 68 | + await driver.get(Pages.logEntryAdded) |
| 69 | + await driver.findElement({ id: 'jsException' }).click() |
| 70 | + |
| 71 | + await delay(3000) |
| 72 | + |
| 73 | + assert.equal(log.text, 'Error: Not working') |
| 74 | + assert.equal(log.type, 'javascript') |
| 75 | + assert.equal(log.level, 'error') |
| 76 | + |
| 77 | + await driver.script().removeJavaScriptErrorHandler(handler) |
| 78 | + }) |
| 79 | + |
| 80 | + it('throws an error while removing a handler that does not exist', async function () { |
| 81 | + try { |
| 82 | + await driver.script().removeJavaScriptErrorHandler(10) |
| 83 | + assert.fail('Expected error not thrown. Non-existent handler cannot be removed') |
| 84 | + } catch (e) { |
| 85 | + assert.strictEqual(e.message, 'Callback with id 10 not found') |
| 86 | + } |
| 87 | + }) |
| 88 | + }) |
| 89 | + }, |
| 90 | + { browsers: [Browser.FIREFOX, Browser.CHROME, Browser.EDGE] }, |
| 91 | +) |
0 commit comments