Path: blob/trunk/javascript/selenium-webdriver/test/actions_test.js
2884 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')2425suite(function (env) {26describe('WebDriver.actions()', function () {27let driver2829beforeEach(async function () {30driver = await env.builder().build()31})3233afterEach(function () {34return driver.quit()35})3637it('click(element)', async function () {38await driver.get(fileServer.whereIs('/data/actions/click.html'))3940let box = await driver.findElement(By.id('box'))41assert.strictEqual(await box.getAttribute('class'), '')4243await driver.actions().click(box).perform()44await driver.wait(async () => {45assert.strictEqual(await box.getAttribute('class'), 'green')46return true47}, 10000)48})4950it('click(element) clicks in center of element', async function () {51await driver.get(fileServer.whereIs('/data/actions/record_click.html'))5253const div = await driver.findElement(By.css('div'))54const rect = await div.getRect()55assert.deepStrictEqual(rect, { width: 500, height: 500, x: 0, y: 0 })5657await driver.actions().click(div).perform()5859await driver.wait(60async () => {61const clicks = await driver.executeScript('return clicks')62return clicks.length > 063},6410000,65'No clicks returned',66)67const clicks = await driver.executeScript('return clicks')68assert.deepStrictEqual(clicks, [[250, 250]])69})7071it('can move relative to element center', async function () {72await driver.get(fileServer.whereIs('/data/actions/record_click.html'))7374const div = await driver.findElement(By.css('div'))75const rect = await div.getRect()76assert.deepStrictEqual(rect, { width: 500, height: 500, x: 0, y: 0 })7778await driver.actions().move({ x: 10, y: 10, origin: div }).click().perform()7980await driver.wait(81async () => {82const clicks = await driver.executeScript('return clicks')83return clicks.length > 084},8510000,86'No clicks returned',87)88const clicks = await driver.executeScript('return clicks')89assert.deepStrictEqual(clicks, [[260, 260]])90})9192ignore(env.browsers(Browser.SAFARI)).it('doubleClick(element)', async function () {93await driver.get(fileServer.whereIs('/data/actions/click.html'))9495let box = await driver.findElement(By.id('box'))96assert.strictEqual(await box.getAttribute('class'), '')9798await driver.actions().doubleClick(box).perform()99await driver.wait(async () => (await box.getAttribute('class')) === 'blue', 10000)100assert.strictEqual(await box.getAttribute('class'), 'blue')101})102103it('dragAndDrop()', async function () {104await driver.get(fileServer.whereIs('/data/actions/drag.html'))105106let slide = await driver.findElement(By.id('slide'))107assert.strictEqual(await slide.getCssValue('left'), '0px')108assert.strictEqual(await slide.getCssValue('top'), '0px')109110let br = await driver.findElement(By.id('BR'))111await driver.actions().dragAndDrop(slide, br).perform()112assert.strictEqual(await slide.getCssValue('left'), '206px')113assert.strictEqual(await slide.getCssValue('top'), '206px')114115let tr = await driver.findElement(By.id('TR'))116await driver.actions().dragAndDrop(slide, tr).perform()117assert.strictEqual(await slide.getCssValue('left'), '206px')118assert.strictEqual(await slide.getCssValue('top'), '1px')119})120121it('move()', async function () {122await driver.get(fileServer.whereIs('/data/actions/drag.html'))123124let slide = await driver.findElement(By.id('slide'))125assert.strictEqual(await slide.getCssValue('left'), '0px')126assert.strictEqual(await slide.getCssValue('top'), '0px')127128await driver129.actions()130.move({ origin: slide })131.press()132.move({ x: 100, y: 100, origin: Origin.POINTER })133.release()134.perform()135136await driver.wait(async () => (await slide.getCssValue('left')) === '101px', 10000)137assert.strictEqual(await slide.getCssValue('left'), '101px')138assert.strictEqual(await slide.getCssValue('left'), '101px')139})140141it('can move to and click element in an iframe', async function () {142await driver.get(fileServer.whereIs('click_tests/click_in_iframe.html'))143144await driver.wait(until.elementLocated(By.id('ifr')), 5000).then((frame) => driver.switchTo().frame(frame))145146let link = await driver.findElement(By.id('link'))147148await driver.actions().click(link).perform()149await driver.switchTo().defaultContent()150return driver.wait(until.titleIs('Submitted Successfully!'), 10000)151})152153it('can send keys to focused element', async function () {154await driver.get(Pages.formPage)155156let el = await driver.findElement(By.id('email'))157assert.strictEqual(await el.getAttribute('value'), '')158159await driver.executeScript('arguments[0].focus()', el)160161await driver.actions().sendKeys('foobar').perform()162163await driver.wait(async () => (await el.getAttribute('value')) === 'foobar', 10000)164assert.strictEqual(await el.getAttribute('value'), 'foobar')165})166167it('can get the property of element', async function () {168await driver.get(Pages.formPage)169170let el = await driver.findElement(By.id('email'))171assert.strictEqual(await el.getProperty('value'), '')172173await driver.executeScript('arguments[0].focus()', el)174175await driver.actions().sendKeys('foobar').perform()176177await driver.wait(async () => (await el.getProperty('value')) === 'foobar', 10000)178assert.strictEqual(await el.getProperty('value'), 'foobar')179})180181it('can send keys to focused element (with modifiers)', async function () {182await driver.get(Pages.formPage)183184let el = await driver.findElement(By.id('email'))185assert.strictEqual(await el.getAttribute('value'), '')186187await driver.executeScript('arguments[0].focus()', el)188189await driver.actions().sendKeys('fo').keyDown(Key.SHIFT).sendKeys('OB').keyUp(Key.SHIFT).sendKeys('ar').perform()190191await driver.wait(async () => (await el.getAttribute('value')) === 'foOBar', 10000)192assert.strictEqual(await el.getAttribute('value'), 'foOBar')193})194195it('can interact with simple form elements', async function () {196await driver.get(Pages.formPage)197198let el = await driver.findElement(By.id('email'))199assert.strictEqual(await el.getAttribute('value'), '')200201await driver.actions().click(el).sendKeys('foobar').perform()202203await driver.wait(async () => (await el.getAttribute('value')) === 'foobar', 10000)204assert.strictEqual(await el.getAttribute('value'), 'foobar')205})206207it('can send keys to designated element', async function () {208await driver.get(Pages.formPage)209210let el = await driver.findElement(By.id('email'))211assert.strictEqual(await el.getAttribute('value'), '')212213await driver.actions().sendKeys(el, 'foobar').perform()214215await driver.wait(async () => (await el.getAttribute('value')) === 'foobar', 10000)216assert.strictEqual(await el.getAttribute('value'), 'foobar')217})218219ignore(env.browsers(Browser.FIREFOX, Browser.SAFARI)).it('can scroll with the wheel input', async function () {220await driver.get(Pages.scrollingPage)221let scrollable = await driver.findElement(By.id('scrollable'))222223await driver.actions().scroll(0, 0, 5, 10, scrollable).perform()224let events = await _getEvents(driver)225assert.strictEqual(events[0].type, 'wheel')226assert.ok(events[0].deltaX >= 5)227assert.ok(events[0].deltaY >= 10)228assert.strictEqual(events[0].deltaZ, 0)229assert.strictEqual(events[0].target, 'scrollContent')230})231232async function _getEvents(driver) {233await driver.wait(async () => {234const events = await driver.executeScript('return allEvents.events;')235return events.length > 0236}, 5000)237return (await driver.executeScript('return allEvents.events;')) || []238}239})240})241242243