Skip to content

Commit 7e253b5

Browse files
authored
[functional/services] update webdriver lib and types (#47381) (#47446)
* [functional/services] refactor using new types * [code/history] wrap forward navigation with browser service
1 parent 38f666c commit 7e253b5

8 files changed

Lines changed: 102 additions & 95 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@
333333
"@types/redux-actions": "^2.2.1",
334334
"@types/request": "^2.48.2",
335335
"@types/rimraf": "^2.0.2",
336-
"@types/selenium-webdriver": "^3.0.16",
336+
"@types/selenium-webdriver": "^4.0.3",
337337
"@types/semver": "^5.5.0",
338338
"@types/sinon": "^7.0.0",
339339
"@types/strip-ansi": "^3.0.0",
@@ -429,7 +429,7 @@
429429
"proxyquire": "1.8.0",
430430
"regenerate": "^1.4.0",
431431
"sass-lint": "^1.12.1",
432-
"selenium-webdriver": "^4.0.0-alpha.4",
432+
"selenium-webdriver": "^4.0.0-alpha.5",
433433
"simple-git": "1.116.0",
434434
"sinon": "^7.2.2",
435435
"strip-ansi": "^3.0.1",

test/functional/services/browser.ts

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
*/
1919

2020
import { cloneDeep } from 'lodash';
21-
import { IKey, logging } from 'selenium-webdriver';
21+
import { logging, Key, Origin } from 'selenium-webdriver';
22+
// @ts-ignore internal modules are not typed
23+
import { LegacyActionSequence } from 'selenium-webdriver/lib/actions';
2224
import { takeUntil } from 'rxjs/operators';
2325

2426
import { modifyUrl } from '../../../src/core/utils';
@@ -31,9 +33,7 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
3133
const log = getService('log');
3234
const config = getService('config');
3335
const lifecycle = getService('lifecycle');
34-
const { driver, Key, LegacyActionSequence, browserType } = await getService(
35-
'__webdriver__'
36-
).init();
36+
const { driver, browserType } = await getService('__webdriver__').init();
3737

3838
const isW3CEnabled = (driver as any).executor_.w3c === true;
3939

@@ -56,7 +56,7 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
5656
/**
5757
* Keyboard events
5858
*/
59-
public readonly keys: IKey = Key;
59+
public readonly keys = Key;
6060

6161
/**
6262
* Browser name
@@ -76,10 +76,8 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
7676
* Returns instance of Actions API based on driver w3c flag
7777
* https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/webdriver_exports_WebDriver.html#actions
7878
*/
79-
public getActions(): any {
80-
return this.isW3CEnabled
81-
? (driver as any).actions()
82-
: (driver as any).actions({ bridge: true });
79+
public getActions() {
80+
return this.isW3CEnabled ? driver.actions() : driver.actions({ bridge: true });
8381
}
8482

8583
/**
@@ -101,7 +99,10 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
10199
* @return {Promise<{height: number, width: number, x: number, y: number}>}
102100
*/
103101
public async getWindowSize(): Promise<{ height: number; width: number; x: number; y: number }> {
104-
return await (driver.manage().window() as any).getRect();
102+
return await driver
103+
.manage()
104+
.window()
105+
.getRect();
105106
}
106107

107108
/**
@@ -112,10 +113,11 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
112113
* @param {number} height
113114
* @return {Promise<void>}
114115
*/
115-
public async setWindowSize(width: number, height: number): Promise<void>;
116-
public async setWindowSize(...args: number[]): Promise<void>;
117-
public async setWindowSize(...args: unknown[]): Promise<void> {
118-
await (driver.manage().window() as any).setRect({ width: args[0], height: args[1] });
116+
public async setWindowSize(width: number, height: number) {
117+
await driver
118+
.manage()
119+
.window()
120+
.setRect({ width, height });
119121
}
120122

121123
/**
@@ -124,7 +126,7 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
124126
*
125127
* @return {Promise<string>}
126128
*/
127-
public async getCurrentUrl(): Promise<string> {
129+
public async getCurrentUrl() {
128130
// strip _t=Date query param when url is read
129131
const current = await driver.getCurrentUrl();
130132
const currentWithoutTime = modifyUrl(current, parsed => {
@@ -142,7 +144,7 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
142144
* @param {boolean} insertTimestamp Optional
143145
* @return {Promise<void>}
144146
*/
145-
public async get(url: string, insertTimestamp: boolean = true): Promise<void> {
147+
public async get(url: string, insertTimestamp: boolean = true) {
146148
if (insertTimestamp) {
147149
const urlWithTime = modifyUrl(url, parsed => {
148150
(parsed.query as any)._t = Date.now();
@@ -168,12 +170,12 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
168170
.move({ x: 0, y: 0 })
169171
.perform();
170172
await this.getActions()
171-
.move({ x: point.x, y: point.y, origin: 'pointer' })
173+
.move({ x: point.x, y: point.y, origin: Origin.POINTER })
172174
.perform();
173175
} else {
174176
await this.getActions()
175177
.pause(this.getActions().mouse)
176-
.move({ x: point.x, y: point.y, origin: 'pointer' })
178+
.move({ x: point.x, y: point.y, origin: Origin.POINTER })
177179
.perform();
178180
}
179181
}
@@ -198,7 +200,7 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
198200
}
199201
return data.location instanceof WebElementWrapper
200202
? { x: data.offset.x || 0, y: data.offset.y || 0, origin: data.location._webElement }
201-
: { x: data.location.x, y: data.location.y, origin: 'pointer' };
203+
: { x: data.location.x, y: data.location.y, origin: Origin.POINTER };
202204
};
203205

204206
const startPoint = getW3CPoint(from);
@@ -223,7 +225,7 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
223225
return await this.getActions()
224226
.move({ origin: from.location._webElement })
225227
.press()
226-
.move({ x: to.location.x, y: to.location.y, origin: 'pointer' })
228+
.move({ x: to.location.x, y: to.location.y, origin: Origin.POINTER })
227229
.release()
228230
.perform();
229231
} else {
@@ -243,7 +245,7 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
243245
*
244246
* @return {Promise<void>}
245247
*/
246-
public async refresh(): Promise<void> {
248+
public async refresh() {
247249
await driver.navigate().refresh();
248250
}
249251

@@ -253,10 +255,18 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
253255
*
254256
* @return {Promise<void>}
255257
*/
256-
public async goBack(): Promise<void> {
258+
public async goBack() {
257259
await driver.navigate().back();
258260
}
259261

262+
/**
263+
* Moves forwards in the browser history.
264+
* https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/webdriver_exports_Navigation.html#forward
265+
*/
266+
public async goForward() {
267+
await driver.navigate().forward();
268+
}
269+
260270
/**
261271
* Sends a sequance of keyboard keys. For each key, this will record a pair of keyDown and keyUp actions
262272
* https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/input_exports_Actions.html#sendKeys
@@ -282,19 +292,19 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
282292
* @param {x: number, y: number} point on browser page
283293
* @return {Promise<void>}
284294
*/
285-
public async clickMouseButton(point: { x: number; y: number }): Promise<void> {
295+
public async clickMouseButton(point: { x: number; y: number }) {
286296
if (this.isW3CEnabled) {
287297
await this.getActions()
288298
.move({ x: 0, y: 0 })
289299
.perform();
290300
await this.getActions()
291-
.move({ x: point.x, y: point.y, origin: 'pointer' })
301+
.move({ x: point.x, y: point.y, origin: Origin.POINTER })
292302
.click()
293303
.perform();
294304
} else {
295305
await this.getActions()
296306
.pause(this.getActions().mouse)
297-
.move({ x: point.x, y: point.y, origin: 'pointer' })
307+
.move({ x: point.x, y: point.y, origin: Origin.POINTER })
298308
.click()
299309
.perform();
300310
}
@@ -307,7 +317,7 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
307317
*
308318
* @return {Promise<string>}
309319
*/
310-
public async getPageSource(): Promise<string> {
320+
public async getPageSource() {
311321
return await driver.getPageSource();
312322
}
313323

@@ -317,7 +327,7 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
317327
*
318328
* @return {Promise<Buffer>}
319329
*/
320-
public async takeScreenshot(): Promise<string> {
330+
public async takeScreenshot() {
321331
return await driver.takeScreenshot();
322332
}
323333

@@ -327,7 +337,7 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
327337
* @param {WebElementWrapper} element
328338
* @return {Promise<void>}
329339
*/
330-
public async doubleClick(): Promise<void> {
340+
public async doubleClick() {
331341
await this.getActions()
332342
.doubleClick()
333343
.perform();
@@ -341,10 +351,8 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
341351
* @param {string} handle
342352
* @return {Promise<void>}
343353
*/
344-
public async switchToWindow(handle: string): Promise<void>;
345-
public async switchToWindow(...args: string[]): Promise<void>;
346-
public async switchToWindow(...args: string[]): Promise<void> {
347-
await (driver.switchTo() as any).window(...args);
354+
public async switchToWindow(nameOrHandle: string) {
355+
await driver.switchTo().window(nameOrHandle);
348356
}
349357

350358
/**
@@ -353,7 +361,7 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
353361
*
354362
* @return {Promise<string[]>}
355363
*/
356-
public async getAllWindowHandles(): Promise<string[]> {
364+
public async getAllWindowHandles() {
357365
return await driver.getAllWindowHandles();
358366
}
359367

@@ -379,7 +387,7 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
379387
*
380388
* @return {Promise<void>}
381389
*/
382-
public async closeCurrentWindow(): Promise<void> {
390+
public async closeCurrentWindow() {
383391
await driver.close();
384392
}
385393

@@ -418,18 +426,18 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
418426
);
419427
}
420428

421-
public async getScrollTop(): Promise<number> {
429+
public async getScrollTop() {
422430
const scrollSize = await driver.executeScript<string>('return document.body.scrollTop');
423431
return parseInt(scrollSize, 10);
424432
}
425433

426-
public async getScrollLeft(): Promise<number> {
434+
public async getScrollLeft() {
427435
const scrollSize = await driver.executeScript<string>('return document.body.scrollLeft');
428436
return parseInt(scrollSize, 10);
429437
}
430438

431439
// return promise with REAL scroll position
432-
public async setScrollTop(scrollSize: number | string): Promise<number> {
440+
public async setScrollTop(scrollSize: number | string) {
433441
await driver.executeScript('document.body.scrollTop = ' + scrollSize);
434442
return this.getScrollTop();
435443
}

test/functional/services/find.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export async function FindProvider({ getService }: FtrProviderContext) {
141141
elements = [];
142142
}
143143
// Force isStale checks for all the retrieved elements.
144-
await Promise.all(elements.map(async (element: any) => await element.isEnabled()));
144+
await Promise.all(elements.map(async element => await element.isEnabled()));
145145
await this._withTimeout(defaultFindTimeout);
146146
return elements;
147147
});
@@ -322,7 +322,7 @@ export async function FindProvider({ getService }: FtrProviderContext) {
322322
log.debug(`Find.clickByPartialLinkText('${linkText}') with timeout=${timeout}`);
323323
await retry.try(async () => {
324324
const element = await this.byPartialLinkText(linkText, timeout);
325-
await (element as any).moveMouseTo();
325+
await element.moveMouseTo();
326326
await element.click();
327327
});
328328
}
@@ -334,7 +334,7 @@ export async function FindProvider({ getService }: FtrProviderContext) {
334334
log.debug(`Find.clickByLinkText('${linkText}') with timeout=${timeout}`);
335335
await retry.try(async () => {
336336
const element = await this.byLinkText(linkText, timeout);
337-
await (element as any).moveMouseTo();
337+
await element.moveMouseTo();
338338
await element.click();
339339
});
340340
}
@@ -470,7 +470,7 @@ export async function FindProvider({ getService }: FtrProviderContext) {
470470
private async _withTimeout(timeout: number) {
471471
if (timeout !== this.currentWait) {
472472
this.currentWait = timeout;
473-
await (driver.manage() as any).setTimeouts({ implicit: timeout });
473+
await driver.manage().setTimeouts({ implicit: timeout });
474474
}
475475
}
476476
}

0 commit comments

Comments
 (0)