From 2d43c2d3fe2c0c7776e5b86d92524b591a348724 Mon Sep 17 00:00:00 2001 From: Puja Jagani Date: Tue, 26 Dec 2023 12:01:18 +0530 Subject: [PATCH] [js][bidi] Add Input module command --- .../node/selenium-webdriver/bidi/input.js | 90 +++++ .../node/selenium-webdriver/lib/input.js | 12 + .../test/bidi/input_test.js | 358 ++++++++++++++++++ 3 files changed, 460 insertions(+) create mode 100644 javascript/node/selenium-webdriver/bidi/input.js create mode 100644 javascript/node/selenium-webdriver/test/bidi/input_test.js diff --git a/javascript/node/selenium-webdriver/bidi/input.js b/javascript/node/selenium-webdriver/bidi/input.js new file mode 100644 index 0000000000000..56cd16c5a4faa --- /dev/null +++ b/javascript/node/selenium-webdriver/bidi/input.js @@ -0,0 +1,90 @@ +// Licensed to the Software Freedom Conservancy (SFC) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The SFC licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +// type: module added to package.json +// import { WebElement } from '../lib/webdriver' +const { WebElement } = require('../lib/webdriver') + +class Input { + constructor(driver) { + this._driver = driver + } + + async init() { + if (!(await this._driver.getCapabilities()).get('webSocketUrl')) { + throw Error('WebDriver instance must support BiDi protocol') + } + + this.bidi = await this._driver.getBidi() + } + + async perform(browsingContextId, actions) { + const _actions = await updateActions(actions) + + const command = { + method: 'input.performActions', + params: { + context: browsingContextId, + actions: _actions, + }, + } + + let response = await this.bidi.send(command) + + return response + } +} + +async function updateActions(actions) { + const _actions = [] + for (const action of actions) { + const sequenceList = action.actions + let updatedSequenceList = [] + + if (action.type === 'pointer' || action.type === 'wheel') { + for (const sequence of sequenceList) { + if ( + (sequence.type === 'pointerMove' || sequence.type === 'scroll') && + sequence.origin instanceof WebElement + ) { + const element = sequence.origin + const elementId = await element.getId() + sequence.origin = { + type: 'element', + element: { sharedId: elementId }, + } + } + updatedSequenceList.push(sequence) + } + + const updatedAction = { ...action, actions: updatedSequenceList } + _actions.push(updatedAction) + } else { + _actions.push(action) + } + } + + return _actions +} + +async function getInputInstance(driver) { + let instance = new Input(driver) + await instance.init() + return instance +} + +module.exports = getInputInstance diff --git a/javascript/node/selenium-webdriver/lib/input.js b/javascript/node/selenium-webdriver/lib/input.js index 15e646895f46a..000302ed1a43d 100644 --- a/javascript/node/selenium-webdriver/lib/input.js +++ b/javascript/node/selenium-webdriver/lib/input.js @@ -1007,6 +1007,18 @@ class Actions { new Command(Name.ACTIONS).setParameter('actions', _actions) ) } + + getSequences() { + const _actions = [] + this.sequences_.forEach((actions, device) => { + if (!isIdle(actions)) { + actions = actions.concat() + _actions.push(Object.assign({ actions }, device.toJSON())) + } + }) + + return _actions + } } /** diff --git a/javascript/node/selenium-webdriver/test/bidi/input_test.js b/javascript/node/selenium-webdriver/test/bidi/input_test.js new file mode 100644 index 0000000000000..35bb9740ed1e5 --- /dev/null +++ b/javascript/node/selenium-webdriver/test/bidi/input_test.js @@ -0,0 +1,358 @@ +// Licensed to the Software Freedom Conservancy (SFC) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The SFC licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +'use strict' + +const assert = require('assert') +const fileServer = require('../../lib/test/fileserver') +const firefox = require('../../firefox') +const { ignore, Pages, suite } = require('../../lib/test') +const { Key, Origin } = require('../../lib/input') +const { Browser, By, until } = require('../..') +const Input = require('../../bidi/input') + +suite( + function (env) { + describe('BiDi Input', function () { + let driver + + beforeEach(async function () { + driver = await env + .builder() + .setFirefoxOptions(new firefox.Options().enableBidi()) + .build() + }) + + afterEach(function () { + return driver.quit() + }) + + it('can click element', async function () { + const browsingContextId = await driver.getWindowHandle() + const input = await Input(driver) + await driver.get(fileServer.whereIs('/data/actions/click.html')) + + let box = await driver.findElement(By.id('box')) + assert.strictEqual(await box.getAttribute('class'), '') + + const actions = await driver.actions().click(box).getSequences() + + await input.perform(browsingContextId, actions) + + await driver.wait(async () => { + assert.strictEqual(await box.getAttribute('class'), 'green') + return true + }, 10000) + }) + + it('can click in center of element', async function () { + const browsingContextId = await driver.getWindowHandle() + const input = await Input(driver) + await driver.get(fileServer.whereIs('/data/actions/record_click.html')) + + const div = await driver.findElement(By.css('div')) + const rect = await div.getRect() + assert.deepStrictEqual(rect, { width: 500, height: 500, x: 0, y: 0 }) + + const actions = await driver.actions().click(div).getSequences() + + await input.perform(browsingContextId, actions) + + await driver.wait( + async () => { + const clicks = await driver.executeScript('return clicks') + return clicks.length > 0 + }, + 10000, + 'No clicks returned', + ) + const clicks = await driver.executeScript('return clicks') + assert.deepStrictEqual(clicks, [[250, 250]]) + }) + + it('can move relative to element center', async function () { + const browsingContextId = await driver.getWindowHandle() + const input = await Input(driver) + await driver.get(fileServer.whereIs('/data/actions/record_click.html')) + + const div = await driver.findElement(By.css('div')) + const rect = await div.getRect() + assert.deepStrictEqual(rect, { width: 500, height: 500, x: 0, y: 0 }) + + const actions = await driver + .actions() + .move({ x: 10, y: 10, origin: div }) + .click() + .getSequences() + + await input.perform(browsingContextId, actions) + + await driver.wait( + async () => { + const clicks = await driver.executeScript('return clicks') + return clicks.length > 0 + }, + 10000, + 'No clicks returned', + ) + const clicks = await driver.executeScript('return clicks') + assert.deepStrictEqual(clicks, [[260, 260]]) + }) + + ignore(env.browsers(Browser.SAFARI)).it( + 'doubleClick(element)', + async function () { + const browsingContextId = await driver.getWindowHandle() + const input = await Input(driver) + await driver.get(fileServer.whereIs('/data/actions/click.html')) + + let box = await driver.findElement(By.id('box')) + assert.strictEqual(await box.getAttribute('class'), '') + + const actions = await driver.actions().doubleClick(box).getSequences() + + await input.perform(browsingContextId, actions) + + await driver.wait( + async () => (await box.getAttribute('class')) === 'blue', + 10000, + ) + assert.strictEqual(await box.getAttribute('class'), 'blue') + }, + ) + + it('dragAndDrop()', async function () { + const browsingContextId = await driver.getWindowHandle() + const input = await Input(driver) + await driver.get(fileServer.whereIs('/data/actions/drag.html')) + + let slide = await driver.findElement(By.id('slide')) + assert.strictEqual(await slide.getCssValue('left'), '0px') + assert.strictEqual(await slide.getCssValue('top'), '0px') + + let br = await driver.findElement(By.id('BR')) + let actions = await driver + .actions() + .dragAndDrop(slide, br) + .getSequences() + await input.perform(browsingContextId, actions) + assert.strictEqual(await slide.getCssValue('left'), '206px') + assert.strictEqual(await slide.getCssValue('top'), '206px') + + let tr = await driver.findElement(By.id('TR')) + actions = await driver.actions().dragAndDrop(slide, tr).getSequences() + await input.perform(browsingContextId, actions) + assert.strictEqual(await slide.getCssValue('left'), '206px') + assert.strictEqual(await slide.getCssValue('top'), '1px') + }) + + it('move()', async function () { + const browsingContextId = await driver.getWindowHandle() + const input = await Input(driver) + await driver.get(fileServer.whereIs('/data/actions/drag.html')) + + let slide = await driver.findElement(By.id('slide')) + assert.strictEqual(await slide.getCssValue('left'), '0px') + assert.strictEqual(await slide.getCssValue('top'), '0px') + + const actions = await driver + .actions() + .move({ origin: slide }) + .press() + .move({ x: 100, y: 100, origin: Origin.POINTER }) + .release() + .getSequences() + + input.perform(browsingContextId, actions) + + await driver.wait( + async () => (await slide.getCssValue('left')) === '101px', + 10000, + ) + assert.strictEqual(await slide.getCssValue('left'), '101px') + assert.strictEqual(await slide.getCssValue('left'), '101px') + }) + + ignore(env.browsers(Browser.FIREFOX)).it( + 'can move to and click element in an iframe', + async function () { + const browsingContextId = await driver.getWindowHandle() + const input = await Input(driver) + await driver.get( + fileServer.whereIs('click_tests/click_in_iframe.html'), + ) + + await driver + .wait(until.elementLocated(By.id('ifr')), 5000) + .then((frame) => driver.switchTo().frame(frame)) + + let link = await driver.findElement(By.id('link')) + + const actions = await driver.actions().click(link).getSequences() + input.perform(browsingContextId, actions) + await driver.switchTo().defaultContent() + return driver.wait(until.titleIs('Submitted Successfully!'), 10000) + }, + ) + + it('can send keys to focused element', async function () { + const browsingContextId = await driver.getWindowHandle() + const input = await Input(driver) + await driver.get(Pages.formPage) + + let el = await driver.findElement(By.id('email')) + assert.strictEqual(await el.getAttribute('value'), '') + + await driver.executeScript('arguments[0].focus()', el) + + const actions = await driver.actions().sendKeys('foobar').getSequences() + + input.perform(browsingContextId, actions) + + await driver.wait( + async () => (await el.getAttribute('value')) === 'foobar', + 10000, + ) + assert.strictEqual(await el.getAttribute('value'), 'foobar') + }) + + it('can get the property of element', async function () { + const browsingContextId = await driver.getWindowHandle() + const input = await Input(driver) + await driver.get(Pages.formPage) + + let el = await driver.findElement(By.id('email')) + assert.strictEqual(await el.getProperty('value'), '') + + await driver.executeScript('arguments[0].focus()', el) + + const actions = await driver.actions().sendKeys('foobar').getSequences() + + await input.perform(browsingContextId, actions) + await driver.wait( + async () => (await el.getProperty('value')) === 'foobar', + 10000, + ) + assert.strictEqual(await el.getProperty('value'), 'foobar') + }) + + it('can send keys to focused element (with modifiers)', async function () { + const browsingContextId = await driver.getWindowHandle() + const input = await Input(driver) + await driver.get(Pages.formPage) + + let el = await driver.findElement(By.id('email')) + assert.strictEqual(await el.getAttribute('value'), '') + + await driver.executeScript('arguments[0].focus()', el) + + const actions = await driver + .actions() + .sendKeys('fo') + .keyDown(Key.SHIFT) + .sendKeys('OB') + .keyUp(Key.SHIFT) + .sendKeys('ar') + .getSequences() + + await input.perform(browsingContextId, actions) + + await driver.wait( + async () => (await el.getAttribute('value')) === 'foOBar', + 10000, + ) + assert.strictEqual(await el.getAttribute('value'), 'foOBar') + }) + + it('can interact with simple form elements', async function () { + const browsingContextId = await driver.getWindowHandle() + const input = await Input(driver) + await driver.get(Pages.formPage) + + let el = await driver.findElement(By.id('email')) + assert.strictEqual(await el.getAttribute('value'), '') + + const actions = await driver + .actions() + .click(el) + .sendKeys('foobar') + .getSequences() + + await input.perform(browsingContextId, actions) + + await driver.wait( + async () => (await el.getAttribute('value')) === 'foobar', + 10000, + ) + assert.strictEqual(await el.getAttribute('value'), 'foobar') + }) + + it('can send keys to designated element', async function () { + const browsingContextId = await driver.getWindowHandle() + const input = await Input(driver) + await driver.get(Pages.formPage) + + let el = await driver.findElement(By.id('email')) + assert.strictEqual(await el.getAttribute('value'), '') + + const actions = await driver + .actions() + .sendKeys(el, 'foobar') + .getSequences() + + await input.perform(browsingContextId, actions) + + await driver.wait( + async () => (await el.getAttribute('value')) === 'foobar', + 10000, + ) + assert.strictEqual(await el.getAttribute('value'), 'foobar') + }) + + ignore(env.browsers(Browser.SAFARI)).it( + 'can scroll with the wheel input', + async function () { + const browsingContextId = await driver.getWindowHandle() + const input = await Input(driver) + await driver.get(Pages.scrollingPage) + let scrollable = await driver.findElement(By.id('scrollable')) + + const actions = await driver + .actions() + .scroll(0, 0, 5, 10, scrollable) + .getSequences() + input.perform(browsingContextId, actions) + let events = await _getEvents(driver) + assert.strictEqual(events[0].type, 'wheel') + assert.ok(events[0].deltaX >= 5) + assert.ok(events[0].deltaY >= 10) + assert.strictEqual(events[0].deltaZ, 0) + assert.strictEqual(events[0].target, 'scrollContent') + }, + ) + + async function _getEvents(driver) { + await driver.wait(async () => { + const events = await driver.executeScript('return allEvents.events;') + return events.length > 0 + }, 5000) + return (await driver.executeScript('return allEvents.events;')) || [] + } + }) + }, + { browsers: [Browser.FIREFOX] }, +)