From 7334bfc47303a6ae2fb05df9a44d487ece372f46 Mon Sep 17 00:00:00 2001 From: Puja Jagani Date: Thu, 11 Jul 2024 13:31:48 +0530 Subject: [PATCH] [bidi][js] Add high-level script pinning APIs (#14250) --- .../node/selenium-webdriver/lib/script.js | 10 ++++++ .../test/lib/webdriver_script_test.js | 32 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/javascript/node/selenium-webdriver/lib/script.js b/javascript/node/selenium-webdriver/lib/script.js index 0b918c5f81fa0..cd1aed675e979 100644 --- a/javascript/node/selenium-webdriver/lib/script.js +++ b/javascript/node/selenium-webdriver/lib/script.js @@ -112,6 +112,16 @@ class Script { await this.#script.removeCallback(id) } + + async pin(script) { + await this.#initScript() + return await this.#script.addPreloadScript(script) + } + + async unpin(id) { + await this.#initScript() + await this.#script.removePreloadScript(id) + } } module.exports = Script diff --git a/javascript/node/selenium-webdriver/test/lib/webdriver_script_test.js b/javascript/node/selenium-webdriver/test/lib/webdriver_script_test.js index f3803190cd92c..6cb0f18664bf2 100644 --- a/javascript/node/selenium-webdriver/test/lib/webdriver_script_test.js +++ b/javascript/node/selenium-webdriver/test/lib/webdriver_script_test.js @@ -122,6 +122,38 @@ suite( assert.strictEqual(message, null) }) + + it('can pin script', async function () { + await driver.script().pin("() => { console.log('Hello!'); }") + let log + + await driver.script().addConsoleMessageHandler((logEntry) => { + log = logEntry + }) + + await driver.get(Pages.logEntryAdded) + + await delay(3000) + + assert.equal(log.text, 'Hello!') + }) + + it('can unpin script', async function () { + const id = await driver.script().pin("() => { console.log('Hello!'); }") + + let count = 0 + await driver.script().addConsoleMessageHandler((logEntry) => { + count++ + }) + + await driver.get(Pages.logEntryAdded) + + await driver.script().unpin(id) + + await driver.get(Pages.logEntryAdded) + + assert.equal(count, 1) + }) }) }, { browsers: [Browser.FIREFOX, Browser.CHROME, Browser.EDGE] },