2015-04-04 09:53:59 -07:00
|
|
|
// 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
|
2014-11-12 19:33:37 +00:00
|
|
|
//
|
2015-04-04 09:53:59 -07:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-11-12 19:33:37 +00:00
|
|
|
//
|
2015-04-04 09:53:59 -07:00
|
|
|
// 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
|
|
|
|
2024-04-16 01:50:58 +05:30
|
|
|
const assert = require('node:assert')
|
2020-08-03 17:56:31 +03:00
|
|
|
const test = require('../lib/test')
|
2024-06-27 18:33:36 +01:00
|
|
|
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
|
|
|
|
2022-08-26 17:48:15 +05:30
|
|
|
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)
|
2016-09-07 18:07:26 +01:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
await driver.getWindowHandle()
|
|
|
|
|
let originalHandles = await driver.getAllWindowHandles()
|
2016-09-07 18:07:26 +01:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
await driver.findElement(By.linkText('Open new window')).click()
|
|
|
|
|
await driver.wait(forNewWindowToBeOpened(originalHandles), 2000)
|
2021-03-24 10:42:10 -07:00
|
|
|
assert.strictEqual(await driver.getTitle(), 'XHTML Test Page')
|
2016-09-07 18:07:26 +01:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
let newHandle = await getNewWindowHandle(originalHandles)
|
2016-09-07 18:07:26 +01:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
await driver.switchTo().window(newHandle)
|
2021-03-24 10:42:10 -07:00
|
|
|
assert.strictEqual(await driver.getTitle(), 'We Arrive Here')
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
2016-09-07 18:07:26 +01:00
|
|
|
|
2024-04-17 11:42:45 +05:30
|
|
|
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
|
2017-10-14 19:47:12 -07:00
|
|
|
|
|
|
|
|
await driver.manage().window().setRect({
|
|
|
|
|
x: newX,
|
|
|
|
|
y: newY,
|
|
|
|
|
width: 640,
|
2020-08-03 17:56:31 +03:00
|
|
|
height: 480,
|
|
|
|
|
})
|
2017-10-14 19:47:12 -07:00
|
|
|
|
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
|
|
|
|
2024-04-17 11:42:45 +05:30
|
|
|
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)
|
2016-10-30 11:30:58 -07:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
let frame = await driver.findElement(By.name('iframe1-name'))
|
|
|
|
|
await driver.switchTo().frame(frame)
|
2016-10-30 11:30:58 -07:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
let { x, y } = await driver.manage().window().getRect()
|
|
|
|
|
x += 10
|
|
|
|
|
y += 10
|
2016-10-30 11:30:58 -07:00
|
|
|
|
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 () {
|
2019-04-13 12:52:10 +03:00
|
|
|
let originalHandles = await driver.getAllWindowHandles()
|
|
|
|
|
let originalHandle = await driver.getWindowHandle()
|
|
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
let newHandle
|
2019-05-05 14:49:34 -07:00
|
|
|
try {
|
2020-08-03 17:56:31 +03:00
|
|
|
newHandle = await driver.switchTo().newWindow()
|
2019-05-05 14:49:34 -07:00
|
|
|
} catch (ex) {
|
|
|
|
|
if (ex instanceof UnknownCommandError) {
|
2024-02-07 16:07:24 +00:00
|
|
|
console.warn(Error(`${env.browser.name}: aborting test due to unsupported command: ${ex}`).stack)
|
2020-08-03 17:56:31 +03:00
|
|
|
return
|
2019-05-05 14:49:34 -07:00
|
|
|
}
|
|
|
|
|
}
|
2019-02-20 17:01:52 +02:00
|
|
|
|
2024-02-07 16:07:24 +00:00
|
|
|
assert.strictEqual((await driver.getAllWindowHandles()).length, originalHandles.length + 1)
|
2020-08-03 17:56:31 +03:00
|
|
|
assert.notEqual(originalHandle, newHandle)
|
2019-02-20 17:01:52 +02:00
|
|
|
})
|
|
|
|
|
|
2017-10-14 19:47:12 -07:00
|
|
|
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
|
2017-10-14 19:47:12 -07:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
let rect = await driver.manage().window().setRect({ width, height })
|
2017-10-14 19:47:12 -07:00
|
|
|
if (rect.width === width && rect.height === height) {
|
2020-08-03 17:56:31 +03:00
|
|
|
return
|
2017-10-14 19:47:12 -07:00
|
|
|
}
|
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
|
|
|
}
|
2016-09-07 18:07:26 +01: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
|
|
|
|
|
})
|
|
|
|
|
}
|
2016-09-07 18:07:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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]
|
|
|
|
|
})
|
2016-09-07 18:07:26 +01:00
|
|
|
}
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|