Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/window_test.js
2884 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
'use strict'
19
20
const assert = require('node:assert')
21
const test = require('../lib/test')
22
const { By } = require('selenium-webdriver')
23
const { UnknownCommandError } = require('selenium-webdriver/lib/error')
24
25
test.suite(function (env) {
26
let driver
27
28
before(async function () {
29
driver = await env.builder().build()
30
})
31
after(function () {
32
return driver.quit()
33
})
34
35
beforeEach(function () {
36
return driver.switchTo().defaultContent()
37
})
38
39
it('can set size of the current window', async function () {
40
await driver.get(test.Pages.echoPage)
41
await changeSizeBy(-20, -20)
42
})
43
44
it('can set size of the current window from frame', async function () {
45
await driver.get(test.Pages.framesetPage)
46
47
const frame = await driver.findElement({ css: 'frame[name="fourth"]' })
48
await driver.switchTo().frame(frame)
49
await changeSizeBy(-20, -20)
50
})
51
52
it('can set size of the current window from iframe', async function () {
53
await driver.get(test.Pages.iframePage)
54
55
const frame = await driver.findElement({
56
css: 'iframe[name="iframe1-name"]',
57
})
58
await driver.switchTo().frame(frame)
59
await changeSizeBy(-20, -20)
60
})
61
62
it('can switch to a new window', async function () {
63
await driver.get(test.Pages.xhtmlTestPage)
64
65
await driver.getWindowHandle()
66
let originalHandles = await driver.getAllWindowHandles()
67
68
await driver.findElement(By.linkText('Open new window')).click()
69
await driver.wait(forNewWindowToBeOpened(originalHandles), 2000)
70
assert.strictEqual(await driver.getTitle(), 'XHTML Test Page')
71
72
let newHandle = await getNewWindowHandle(originalHandles)
73
74
await driver.switchTo().window(newHandle)
75
assert.strictEqual(await driver.getTitle(), 'We Arrive Here')
76
})
77
78
it('can set the window position of the current window', async function () {
79
let { x, y } = await driver.manage().window().getRect()
80
let newX = x + 10
81
let newY = y + 10
82
83
await driver.manage().window().setRect({
84
x: newX,
85
y: newY,
86
width: 640,
87
height: 480,
88
})
89
90
await driver.wait(forPositionToBe(newX, newY), 1000)
91
})
92
93
it('can set the window position from a frame', async function () {
94
await driver.get(test.Pages.iframePage)
95
96
let frame = await driver.findElement(By.name('iframe1-name'))
97
await driver.switchTo().frame(frame)
98
99
let { x, y } = await driver.manage().window().getRect()
100
x += 10
101
y += 10
102
103
await driver.manage().window().setRect({ width: 640, height: 480, x, y })
104
await driver.wait(forPositionToBe(x, y), 1000)
105
})
106
107
it('can open a new window', async function () {
108
let originalHandles = await driver.getAllWindowHandles()
109
let originalHandle = await driver.getWindowHandle()
110
111
let newHandle
112
try {
113
newHandle = await driver.switchTo().newWindow()
114
} catch (ex) {
115
if (ex instanceof UnknownCommandError) {
116
console.warn(Error(`${env.browser.name}: aborting test due to unsupported command: ${ex}`).stack)
117
return
118
}
119
}
120
121
assert.strictEqual((await driver.getAllWindowHandles()).length, originalHandles.length + 1)
122
assert.notEqual(originalHandle, newHandle)
123
})
124
125
async function changeSizeBy(dx, dy) {
126
let { width, height } = await driver.manage().window().getRect()
127
width += dx
128
height += dy
129
130
let rect = await driver.manage().window().setRect({ width, height })
131
if (rect.width === width && rect.height === height) {
132
return
133
}
134
return await driver.wait(forSizeToBe(width, height), 1000)
135
}
136
137
function forSizeToBe(w, h) {
138
return async function () {
139
let { width, height } = await driver.manage().window().getRect()
140
return width === w && height === h
141
}
142
}
143
144
function forPositionToBe(x, y) {
145
return async function () {
146
let position = await driver.manage().window().getRect()
147
return (
148
position.x === x &&
149
// On OSX, the window height may be bumped down 22px for the top
150
// status bar.
151
// On Linux, Opera's window position will be off by 28px.
152
position.y >= y &&
153
position.y <= y + 28
154
)
155
}
156
}
157
158
function forNewWindowToBeOpened(originalHandles) {
159
return function () {
160
return driver.getAllWindowHandles().then(function (currentHandles) {
161
return currentHandles.length > originalHandles.length
162
})
163
}
164
}
165
166
function getNewWindowHandle(originalHandles) {
167
// Note: this assumes there's just one new window.
168
return driver.getAllWindowHandles().then(function (currentHandles) {
169
return currentHandles.filter(function (i) {
170
return originalHandles.indexOf(i) < 0
171
})[0]
172
})
173
}
174
})
175
176