Path: blob/trunk/javascript/selenium-webdriver/test/window_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 test = require('../lib/test')21const { By } = require('selenium-webdriver')22const { UnknownCommandError } = require('selenium-webdriver/lib/error')2324test.suite(function (env) {25let driver2627before(async function () {28driver = await env.builder().build()29})30after(function () {31return driver.quit()32})3334beforeEach(function () {35return driver.switchTo().defaultContent()36})3738it('can set size of the current window', async function () {39await driver.get(test.Pages.echoPage)40await changeSizeBy(-20, -20)41})4243it('can set size of the current window from frame', async function () {44await driver.get(test.Pages.framesetPage)4546const frame = await driver.findElement({ css: 'frame[name="fourth"]' })47await driver.switchTo().frame(frame)48await changeSizeBy(-20, -20)49})5051it('can set size of the current window from iframe', async function () {52await driver.get(test.Pages.iframePage)5354const frame = await driver.findElement({55css: 'iframe[name="iframe1-name"]',56})57await driver.switchTo().frame(frame)58await changeSizeBy(-20, -20)59})6061it('can switch to a new window', async function () {62await driver.get(test.Pages.xhtmlTestPage)6364await driver.getWindowHandle()65let originalHandles = await driver.getAllWindowHandles()6667await driver.findElement(By.linkText('Open new window')).click()68await driver.wait(forNewWindowToBeOpened(originalHandles), 2000)69assert.strictEqual(await driver.getTitle(), 'XHTML Test Page')7071let newHandle = await getNewWindowHandle(originalHandles)7273await driver.switchTo().window(newHandle)74assert.strictEqual(await driver.getTitle(), 'We Arrive Here')75})7677it('can set the window position of the current window', async function () {78let { x, y } = await driver.manage().window().getRect()79let newX = x + 1080let newY = y + 108182await driver.manage().window().setRect({83x: newX,84y: newY,85width: 640,86height: 480,87})8889await driver.wait(forPositionToBe(newX, newY), 1000)90})9192it('can set the window position from a frame', async function () {93await driver.get(test.Pages.iframePage)9495let frame = await driver.findElement(By.name('iframe1-name'))96await driver.switchTo().frame(frame)9798let { x, y } = await driver.manage().window().getRect()99x += 10100y += 10101102await driver.manage().window().setRect({ width: 640, height: 480, x, y })103await driver.wait(forPositionToBe(x, y), 1000)104})105106it('can open a new window', async function () {107let originalHandles = await driver.getAllWindowHandles()108let originalHandle = await driver.getWindowHandle()109110let newHandle111try {112newHandle = await driver.switchTo().newWindow()113} catch (ex) {114if (ex instanceof UnknownCommandError) {115console.warn(Error(`${env.browser.name}: aborting test due to unsupported command: ${ex}`).stack)116return117}118}119120assert.strictEqual((await driver.getAllWindowHandles()).length, originalHandles.length + 1)121assert.notEqual(originalHandle, newHandle)122})123124async function changeSizeBy(dx, dy) {125let { width, height } = await driver.manage().window().getRect()126width += dx127height += dy128129let rect = await driver.manage().window().setRect({ width, height })130if (rect.width === width && rect.height === height) {131return132}133return await driver.wait(forSizeToBe(width, height), 1000)134}135136function forSizeToBe(w, h) {137return async function () {138let { width, height } = await driver.manage().window().getRect()139return width === w && height === h140}141}142143function forPositionToBe(x, y) {144return async function () {145let position = await driver.manage().window().getRect()146return (147position.x === x &&148// On OSX, the window height may be bumped down 22px for the top149// status bar.150// On Linux, Opera's window position will be off by 28px.151position.y >= y &&152position.y <= y + 28153)154}155}156157function forNewWindowToBeOpened(originalHandles) {158return function () {159return driver.getAllWindowHandles().then(function (currentHandles) {160return currentHandles.length > originalHandles.length161})162}163}164165function getNewWindowHandle(originalHandles) {166// Note: this assumes there's just one new window.167return driver.getAllWindowHandles().then(function (currentHandles) {168return currentHandles.filter(function (i) {169return originalHandles.indexOf(i) < 0170})[0]171})172}173})174175176