Path: blob/trunk/javascript/selenium-webdriver/test/bidi/generated/input_test.js
11822 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 { suite, ignore, Pages } = require('../../../lib/test')21const { Browser } = require('selenium-webdriver')2223const { Input } = require('selenium-webdriver/bidi/generated/input')24const { Script } = require('selenium-webdriver/bidi/generated/script')2526suite(27function (env) {28let driver29let input30let script3132beforeEach(async function () {33driver = await env.builder().build()34input = await Input.create(driver)35script = await Script.create(driver)36})3738afterEach(function () {39return driver.quit()40})4142describe('performActions keyboard', function () {43it('can type into a text field using key actions', async function () {44const contextId = await driver.getWindowHandle()45await driver.get(Pages.formPage)4647// Find a text input (id="working" is <input type="text"> on formPage)48const inputEl = await driver.findElement({ id: 'working' })49await inputEl.click()5051// Perform key actions using the generated API52await input.performActions({53context: contextId,54actions: [55{56type: 'key',57id: 'keyboard',58actions: [59{ type: 'keyDown', value: 'H' },60{ type: 'keyUp', value: 'H' },61{ type: 'keyDown', value: 'i' },62{ type: 'keyUp', value: 'i' },63],64},65],66})6768const value = await inputEl.getAttribute('value')69assert.strictEqual(value, 'Hi')70})7172it('can perform select all and delete', async function () {73const contextId = await driver.getWindowHandle()74await driver.get(Pages.formPage)7576// id="working" is <input type="text"> on formPage77const inputEl = await driver.findElement({ id: 'working' })78await inputEl.clear()79await inputEl.sendKeys('hello')8081// Select all and delete using BiDi key actions82const modifier = process.platform === 'darwin' ? '' : '' // Meta / Control83await input.performActions({84context: contextId,85actions: [86{87type: 'key',88id: 'keyboard',89actions: [90{ type: 'keyDown', value: modifier },91{ type: 'keyDown', value: 'a' },92{ type: 'keyUp', value: 'a' },93{ type: 'keyUp', value: modifier },94{ type: 'keyDown', value: '' }, // Backspace95{ type: 'keyUp', value: '' },96],97},98],99})100101const value = await inputEl.getAttribute('value')102assert.strictEqual(value, '')103})104})105106describe('performActions pointer mouse', function () {107it('can click using mouse pointer actions', async function () {108const contextId = await driver.getWindowHandle()109await driver.get(Pages.clicksPage)110111const button = await driver.findElement({ id: 'normal' })112const rect = await button.getRect()113114await input.performActions({115context: contextId,116actions: [117{118type: 'pointer',119id: 'mouse',120parameters: { pointerType: 'mouse' },121actions: [122{123type: 'pointerMove',124x: Math.round(rect.x + rect.width / 2),125y: Math.round(rect.y + rect.height / 2),126origin: 'viewport',127},128{ type: 'pointerDown', button: 0, width: 1, height: 1, pressure: 0 },129{ type: 'pointerUp', button: 0 },130],131},132],133})134135// If click went through, page should show a result136const url = await driver.getCurrentUrl()137assert.ok(url)138})139140it('can perform mouse move', async function () {141const contextId = await driver.getWindowHandle()142await driver.get(Pages.emptyPage)143144await input.performActions({145context: contextId,146actions: [147{148type: 'pointer',149id: 'mouse',150parameters: { pointerType: 'mouse' },151actions: [{ type: 'pointerMove', x: 100, y: 100, origin: 'viewport' }],152},153],154})155})156})157158ignore(env.browsers(Browser.CHROME)).describe('releaseActions', function () {159it('can release actions', async function () {160const contextId = await driver.getWindowHandle()161await driver.get(Pages.emptyPage)162163await input.performActions({164context: contextId,165actions: [166{167type: 'key',168id: 'keyboard',169actions: [{ type: 'keyDown', value: 'a' }],170},171],172})173174await input.releaseActions({ context: contextId })175})176})177178ignore(env.browsers(Browser.FIREFOX)).describe('onFileDialogOpened', function () {179it('receives event when a file input is clicked', async function () {180let dialogEvent = null181await input.onFileDialogOpened((params) => {182dialogEvent = params183assert.ok(params.context, 'event should have a context')184})185186const contextId = await driver.getWindowHandle()187await driver.get(Pages.uploadPage)188189// userActivation: true is required — Chrome blocks file dialog without a user gesture190await script.evaluate({191expression: "document.getElementById('upload').click()",192target: { context: contextId },193awaitPromise: false,194userActivation: true,195})196// if code reaches here means the event did not happen so fail the test197await driver.wait(() => dialogEvent !== null, 5000, 'fileDialogOpened event should have fired')198})199})200},201{ browsers: [Browser.CHROME, Browser.FIREFOX] },202)203204205