SIGN IN SIGN UP
SeleniumHQ / selenium UNCLAIMED

A browser automation framework and ecosystem.

0 0 1280 Java
// 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.
2013-04-27 11:38:41 -07:00
2020-08-03 17:56:31 +03:00
'use strict'
2013-04-27 11:38:41 -07:00
const assert = require('node:assert')
2020-08-03 17:56:31 +03:00
const test = require('../lib/test')
const { By } = require('selenium-webdriver')
const { UnknownCommandError } = require('selenium-webdriver/lib/error')
2013-04-27 11:38:41 -07:00
2020-08-03 17:56:31 +03:00
test.suite(function (env) {
let driver
2013-04-27 11:38:41 -07:00
2020-08-03 17:56:31 +03:00
before(async function () {
driver = await env.builder().build()
})
after(function () {
return driver.quit()
})
2014-11-05 13:26:31 -08:00
2020-08-03 17:56:31 +03:00
beforeEach(function () {
return driver.switchTo().defaultContent()
})
2013-04-27 11:38:41 -07:00
2020-08-03 17:56:31 +03:00
it('can set size of the current window', async function () {
await driver.get(test.Pages.echoPage)
await changeSizeBy(-20, -20)
})
2013-04-27 11:38:41 -07:00
2020-08-03 17:56:31 +03:00
it('can set size of the current window from frame', async function () {
await driver.get(test.Pages.framesetPage)
2016-07-01 16:11:56 -07:00
2022-08-25 21:22:03 +05:30
const frame = await driver.findElement({ css: 'frame[name="fourth"]' })
2020-08-03 17:56:31 +03:00
await driver.switchTo().frame(frame)
await changeSizeBy(-20, -20)
})
2013-04-27 11:38:41 -07:00
2020-08-03 17:56:31 +03:00
it('can set size of the current window from iframe', async function () {
await driver.get(test.Pages.iframePage)
2016-07-01 16:11:56 -07:00
const frame = await driver.findElement({
css: 'iframe[name="iframe1-name"]',
})
2020-08-03 17:56:31 +03:00
await driver.switchTo().frame(frame)
await changeSizeBy(-20, -20)
})
2013-04-27 11:38:41 -07:00
2020-08-03 17:56:31 +03:00
it('can switch to a new window', async function () {
await driver.get(test.Pages.xhtmlTestPage)
2020-08-03 17:56:31 +03:00
await driver.getWindowHandle()
let originalHandles = await driver.getAllWindowHandles()
2020-08-03 17:56:31 +03:00
await driver.findElement(By.linkText('Open new window')).click()
await driver.wait(forNewWindowToBeOpened(originalHandles), 2000)
assert.strictEqual(await driver.getTitle(), 'XHTML Test Page')
2020-08-03 17:56:31 +03:00
let newHandle = await getNewWindowHandle(originalHandles)
2020-08-03 17:56:31 +03:00
await driver.switchTo().window(newHandle)
assert.strictEqual(await driver.getTitle(), 'We Arrive Here')
2020-08-03 17:56:31 +03:00
})
it('can set the window position of the current window', async function () {
2020-08-03 17:56:31 +03:00
let { x, y } = await driver.manage().window().getRect()
let newX = x + 10
let newY = y + 10
await driver.manage().window().setRect({
x: newX,
y: newY,
width: 640,
2020-08-03 17:56:31 +03:00
height: 480,
})
2022-08-25 21:22:03 +05:30
await driver.wait(forPositionToBe(newX, newY), 1000)
2020-08-03 17:56:31 +03:00
})
2013-04-27 11:38:41 -07:00
it('can set the window position from a frame', async function () {
2020-08-03 17:56:31 +03:00
await driver.get(test.Pages.iframePage)
2020-08-03 17:56:31 +03:00
let frame = await driver.findElement(By.name('iframe1-name'))
await driver.switchTo().frame(frame)
2020-08-03 17:56:31 +03:00
let { x, y } = await driver.manage().window().getRect()
x += 10
y += 10
2020-08-03 17:56:31 +03:00
await driver.manage().window().setRect({ width: 640, height: 480, x, y })
2022-08-25 21:22:03 +05:30
await driver.wait(forPositionToBe(x, y), 1000)
2020-08-03 17:56:31 +03:00
})
2013-04-27 11:38:41 -07:00
2020-08-03 17:56:31 +03:00
it('can open a new window', async function () {
let originalHandles = await driver.getAllWindowHandles()
let originalHandle = await driver.getWindowHandle()
2020-08-03 17:56:31 +03:00
let newHandle
try {
2020-08-03 17:56:31 +03:00
newHandle = await driver.switchTo().newWindow()
} catch (ex) {
if (ex instanceof UnknownCommandError) {
console.warn(Error(`${env.browser.name}: aborting test due to unsupported command: ${ex}`).stack)
2020-08-03 17:56:31 +03:00
return
}
}
assert.strictEqual((await driver.getAllWindowHandles()).length, originalHandles.length + 1)
2020-08-03 17:56:31 +03:00
assert.notEqual(originalHandle, newHandle)
})
async function changeSizeBy(dx, dy) {
2020-08-03 17:56:31 +03:00
let { width, height } = await driver.manage().window().getRect()
width += dx
height += dy
2020-08-03 17:56:31 +03:00
let rect = await driver.manage().window().setRect({ width, height })
if (rect.width === width && rect.height === height) {
2020-08-03 17:56:31 +03:00
return
}
2022-08-25 21:22:03 +05:30
return await driver.wait(forSizeToBe(width, height), 1000)
2013-04-27 11:38:41 -07:00
}
function forSizeToBe(w, h) {
2020-08-03 17:56:31 +03:00
return async function () {
let { width, height } = await driver.manage().window().getRect()
return width === w && height === h
}
2013-04-27 11:38:41 -07:00
}
function forPositionToBe(x, y) {
2020-08-03 17:56:31 +03:00
return async function () {
let position = await driver.manage().window().getRect()
return (
position.x === x &&
// On OSX, the window height may be bumped down 22px for the top
// status bar.
// On Linux, Opera's window position will be off by 28px.
position.y >= y &&
position.y <= y + 28
)
}
2013-04-27 11:38:41 -07:00
}
function forNewWindowToBeOpened(originalHandles) {
2020-08-03 17:56:31 +03:00
return function () {
return driver.getAllWindowHandles().then(function (currentHandles) {
return currentHandles.length > originalHandles.length
})
}
}
function getNewWindowHandle(originalHandles) {
// Note: this assumes there's just one new window.
2020-08-03 17:56:31 +03:00
return driver.getAllWindowHandles().then(function (currentHandles) {
return currentHandles.filter(function (i) {
return originalHandles.indexOf(i) < 0
})[0]
})
}
2020-08-03 17:56:31 +03:00
})