Path: blob/trunk/javascript/selenium-webdriver/test/bidi/input_test.js
2885 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 fileServer = require('../../lib/test/fileserver')21const { ignore, Pages, suite } = require('../../lib/test')22const { Key, Origin } = require('selenium-webdriver/lib/input')23const { Browser, By, until } = require('selenium-webdriver')24const Input = require('selenium-webdriver/bidi/input')2526suite(27function (env) {28describe('BiDi Input', function () {29let driver3031beforeEach(async function () {32driver = await env.builder().build()33})3435afterEach(function () {36return driver.quit()37})3839it('can click element', async function () {40const browsingContextId = await driver.getWindowHandle()41const input = await Input(driver)42await driver.get(fileServer.whereIs('/data/actions/click.html'))4344let box = await driver.findElement(By.id('box'))45assert.strictEqual(await box.getAttribute('class'), '')4647const actions = await driver.actions().click(box).getSequences()4849await input.perform(browsingContextId, actions)5051await driver.wait(async () => {52assert.strictEqual(await box.getAttribute('class'), 'green')53return true54}, 10000)55})5657it('can click in center of element', async function () {58const browsingContextId = await driver.getWindowHandle()59const input = await Input(driver)60await driver.get(fileServer.whereIs('/data/actions/record_click.html'))6162const div = await driver.findElement(By.css('div'))63const rect = await div.getRect()64assert.deepStrictEqual(rect, { width: 500, height: 500, x: 0, y: 0 })6566const actions = await driver.actions().click(div).getSequences()6768await input.perform(browsingContextId, actions)6970await driver.wait(71async () => {72const clicks = await driver.executeScript('return clicks')73return clicks.length > 074},7510000,76'No clicks returned',77)78const clicks = await driver.executeScript('return clicks')79assert.deepStrictEqual(clicks, [[250, 250]])80})8182it('can move relative to element center', async function () {83const browsingContextId = await driver.getWindowHandle()84const input = await Input(driver)85await driver.get(fileServer.whereIs('/data/actions/record_click.html'))8687const div = await driver.findElement(By.css('div'))88const rect = await div.getRect()89assert.deepStrictEqual(rect, { width: 500, height: 500, x: 0, y: 0 })9091const actions = await driver.actions().move({ x: 10, y: 10, origin: div }).click().getSequences()9293await input.perform(browsingContextId, actions)9495await driver.wait(96async () => {97const clicks = await driver.executeScript('return clicks')98return clicks.length > 099},10010000,101'No clicks returned',102)103const clicks = await driver.executeScript('return clicks')104assert.deepStrictEqual(clicks, [[260, 260]])105})106107ignore(env.browsers(Browser.SAFARI)).it('doubleClick(element)', async function () {108const browsingContextId = await driver.getWindowHandle()109const input = await Input(driver)110await driver.get(fileServer.whereIs('/data/actions/click.html'))111112let box = await driver.findElement(By.id('box'))113assert.strictEqual(await box.getAttribute('class'), '')114115const actions = await driver.actions().doubleClick(box).getSequences()116117await input.perform(browsingContextId, actions)118119await driver.wait(async () => (await box.getAttribute('class')) === 'blue', 10000)120assert.strictEqual(await box.getAttribute('class'), 'blue')121})122123it('dragAndDrop()', async function () {124const browsingContextId = await driver.getWindowHandle()125const input = await Input(driver)126await driver.get(fileServer.whereIs('/data/actions/drag.html'))127128let slide = await driver.findElement(By.id('slide'))129assert.strictEqual(await slide.getCssValue('left'), '0px')130assert.strictEqual(await slide.getCssValue('top'), '0px')131132let br = await driver.findElement(By.id('BR'))133let actions = await driver.actions().dragAndDrop(slide, br).getSequences()134await input.perform(browsingContextId, actions)135assert.strictEqual(await slide.getCssValue('left'), '206px')136assert.strictEqual(await slide.getCssValue('top'), '206px')137138let tr = await driver.findElement(By.id('TR'))139actions = await driver.actions().dragAndDrop(slide, tr).getSequences()140await input.perform(browsingContextId, actions)141assert.strictEqual(await slide.getCssValue('left'), '206px')142assert.strictEqual(await slide.getCssValue('top'), '1px')143})144145it('move()', async function () {146const browsingContextId = await driver.getWindowHandle()147const input = await Input(driver)148await driver.get(fileServer.whereIs('/data/actions/drag.html'))149150let slide = await driver.findElement(By.id('slide'))151assert.strictEqual(await slide.getCssValue('left'), '0px')152assert.strictEqual(await slide.getCssValue('top'), '0px')153154const actions = await driver155.actions()156.move({ origin: slide })157.press()158.move({ x: 100, y: 100, origin: Origin.POINTER })159.release()160.getSequences()161162input.perform(browsingContextId, actions)163164await driver.wait(async () => (await slide.getCssValue('left')) === '101px', 10000)165assert.strictEqual(await slide.getCssValue('left'), '101px')166assert.strictEqual(await slide.getCssValue('left'), '101px')167})168169xit('can move to and click element in an iframe', async function () {170const browsingContextId = await driver.getWindowHandle()171const input = await Input(driver)172await driver.get(fileServer.whereIs('click_tests/click_in_iframe.html'))173174await driver.wait(until.elementLocated(By.id('ifr')), 5000).then((frame) => driver.switchTo().frame(frame))175176let link = await driver.findElement(By.id('link'))177178const actions = await driver.actions().click(link).getSequences()179input.perform(browsingContextId, actions)180await driver.switchTo().defaultContent()181return driver.wait(until.titleIs('Submitted Successfully!'), 10000)182})183184it('can send keys to focused element', async function () {185const browsingContextId = await driver.getWindowHandle()186const input = await Input(driver)187await driver.get(Pages.formPage)188189let el = await driver.findElement(By.id('email'))190assert.strictEqual(await el.getAttribute('value'), '')191192await driver.executeScript('arguments[0].focus()', el)193194const actions = await driver.actions().sendKeys('foobar').getSequences()195196input.perform(browsingContextId, actions)197198await driver.wait(async () => (await el.getAttribute('value')) === 'foobar', 10000)199assert.strictEqual(await el.getAttribute('value'), 'foobar')200})201202it('can get the property of element', async function () {203const browsingContextId = await driver.getWindowHandle()204const input = await Input(driver)205await driver.get(Pages.formPage)206207let el = await driver.findElement(By.id('email'))208assert.strictEqual(await el.getProperty('value'), '')209210await driver.executeScript('arguments[0].focus()', el)211212const actions = await driver.actions().sendKeys('foobar').getSequences()213214await input.perform(browsingContextId, actions)215await driver.wait(async () => (await el.getProperty('value')) === 'foobar', 10000)216assert.strictEqual(await el.getProperty('value'), 'foobar')217})218219it('can send keys to focused element (with modifiers)', async function () {220const browsingContextId = await driver.getWindowHandle()221const input = await Input(driver)222await driver.get(Pages.formPage)223224let el = await driver.findElement(By.id('email'))225assert.strictEqual(await el.getAttribute('value'), '')226227await driver.executeScript('arguments[0].focus()', el)228229const actions = await driver230.actions()231.sendKeys('fo')232.keyDown(Key.SHIFT)233.sendKeys('OB')234.keyUp(Key.SHIFT)235.sendKeys('ar')236.getSequences()237238await input.perform(browsingContextId, actions)239240await driver.wait(async () => (await el.getAttribute('value')) === 'foOBar', 10000)241assert.strictEqual(await el.getAttribute('value'), 'foOBar')242})243244it('can interact with simple form elements', async function () {245const browsingContextId = await driver.getWindowHandle()246const input = await Input(driver)247await driver.get(Pages.formPage)248249let el = await driver.findElement(By.id('email'))250assert.strictEqual(await el.getAttribute('value'), '')251252const actions = await driver.actions().click(el).sendKeys('foobar').getSequences()253254await input.perform(browsingContextId, actions)255256await driver.wait(async () => (await el.getAttribute('value')) === 'foobar', 10000)257assert.strictEqual(await el.getAttribute('value'), 'foobar')258})259260it('can send keys to designated element', async function () {261const browsingContextId = await driver.getWindowHandle()262const input = await Input(driver)263await driver.get(Pages.formPage)264265let el = await driver.findElement(By.id('email'))266assert.strictEqual(await el.getAttribute('value'), '')267268const actions = await driver.actions().sendKeys(el, 'foobar').getSequences()269270await input.perform(browsingContextId, actions)271272await driver.wait(async () => (await el.getAttribute('value')) === 'foobar', 10000)273assert.strictEqual(await el.getAttribute('value'), 'foobar')274})275276it('can scroll with the wheel input', async function () {277const browsingContextId = await driver.getWindowHandle()278const input = await Input(driver)279await driver.get(Pages.scrollingPage)280let scrollable = await driver.findElement(By.id('scrollable'))281282const actions = await driver.actions().scroll(0, 0, 5, 10, scrollable).getSequences()283input.perform(browsingContextId, actions)284let events = await _getEvents(driver)285assert.strictEqual(events[0].type, 'wheel')286assert.ok(events[0].deltaX >= 5)287assert.ok(events[0].deltaY >= 10)288assert.strictEqual(events[0].deltaZ, 0)289assert.strictEqual(events[0].target, 'scrollContent')290})291292it('can execute release in browsing context', async function () {293const browsingContextId = await driver.getWindowHandle()294const input = await Input(driver)295await driver.get(Pages.releaseAction)296297let inputTextBox = await driver.findElement(By.id('keys'))298299await driver.executeScript('arguments[0].focus()', inputTextBox)300301const actions = await driver.actions().keyDown('a').keyDown('b').getSequences()302303await input.perform(browsingContextId, actions)304305await driver.executeScript('resetEvents()')306307await input.release(browsingContextId)308309const events = await driver.executeScript('return allEvents.events')310311assert.strictEqual(events[0].code, 'KeyB')312assert.strictEqual(events[1].code, 'KeyA')313})314315async function _getEvents(driver) {316await driver.wait(async () => {317const events = await driver.executeScript('return allEvents.events;')318return events.length > 0319}, 5000)320return (await driver.executeScript('return allEvents.events;')) || []321}322})323},324{ browsers: [Browser.FIREFOX, Browser.CHROME, Browser.EDGE] },325)326327328