|
| 1 | + |
| 2 | +const assert = require("assert"); |
| 3 | +const firefox = require('selenium-webdriver/firefox'); |
| 4 | +const {until, Builder} = require("selenium-webdriver"); |
| 5 | + |
| 6 | +let driver |
| 7 | + |
| 8 | +beforeEach(async function () { |
| 9 | + driver = new Builder() |
| 10 | + .setFirefoxOptions(new firefox.Options().enableBidi()) |
| 11 | + .build() |
| 12 | +}) |
| 13 | + |
| 14 | +afterEach(async function () { |
| 15 | + await driver.quit() |
| 16 | +}) |
| 17 | + |
| 18 | +function delay(ms) { |
| 19 | + return new Promise((resolve) => setTimeout(resolve, ms)) |
| 20 | +} |
| 21 | + |
| 22 | +describe('BiDi Script', function () { |
| 23 | + |
| 24 | + it('can listen to dom mutations', async function () { |
| 25 | + let message = null |
| 26 | + await driver.script().addDomMutationHandler((m) => { |
| 27 | + message = m |
| 28 | + }) |
| 29 | + |
| 30 | + await driver.get('https://www.selenium.dev/selenium/web/dynamic') |
| 31 | + |
| 32 | + let element = driver.findElement({ id: 'reveal' }) |
| 33 | + await element.click() |
| 34 | + let revealed = driver.findElement({ id: 'revealed' }) |
| 35 | + await driver.wait(until.elementIsVisible(revealed), 5000) |
| 36 | + |
| 37 | + assert.strictEqual(message['attribute_name'], 'style') |
| 38 | + assert.strictEqual(message['current_value'], '') |
| 39 | + assert.strictEqual(message['old_value'], 'display:none;') |
| 40 | + }) |
| 41 | + |
| 42 | + it('can remove to dom mutation handler', async function () { |
| 43 | + let message = null |
| 44 | + let id = await driver.script().addDomMutationHandler((m) => { |
| 45 | + message = m |
| 46 | + }) |
| 47 | + |
| 48 | + await driver.script().removeDomMutationHandler(id) |
| 49 | + |
| 50 | + await driver.get('https://www.selenium.dev/selenium/web/dynamic') |
| 51 | + |
| 52 | + let element = driver.findElement({ id: 'reveal' }) |
| 53 | + await element.click() |
| 54 | + let revealed = driver.findElement({ id: 'revealed' }) |
| 55 | + await driver.wait(until.elementIsVisible(revealed), 5000) |
| 56 | + |
| 57 | + assert.strictEqual(message, null) |
| 58 | + }) |
| 59 | + |
| 60 | + it('can pin script', async function () { |
| 61 | + await driver.script().pin("() => { console.log('Hello!'); }") |
| 62 | + let log |
| 63 | + |
| 64 | + await driver.script().addConsoleMessageHandler((logEntry) => { |
| 65 | + log = logEntry |
| 66 | + }) |
| 67 | + |
| 68 | + await driver.get('https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html') |
| 69 | + |
| 70 | + await delay(3000) |
| 71 | + |
| 72 | + assert.equal(log.text, 'Hello!') |
| 73 | + }) |
| 74 | + |
| 75 | + it('can unpin script', async function () { |
| 76 | + const id = await driver.script().pin("() => { console.log('Hello!'); }") |
| 77 | + |
| 78 | + let count = 0 |
| 79 | + await driver.script().addConsoleMessageHandler((logEntry) => { |
| 80 | + count++ |
| 81 | + }) |
| 82 | + |
| 83 | + await driver.get('https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html') |
| 84 | + |
| 85 | + await driver.script().unpin(id) |
| 86 | + |
| 87 | + await driver.get('https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html') |
| 88 | + |
| 89 | + assert.equal(count, 1) |
| 90 | + }) |
| 91 | +}) |
| 92 | + |
| 93 | + |
0 commit comments