Skip to content

Commit

Permalink
[functional/services] update webdriver lib and types (elastic#47381)
Browse files Browse the repository at this point in the history
* [functional/services] refactor using new types

* [code/history] wrap forward navigation with browser service
  • Loading branch information
dmlemeshko committed Oct 7, 2019
1 parent 8b6dd79 commit 36d4bcd
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 95 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@
"@types/redux-actions": "^2.2.1",
"@types/request": "^2.48.2",
"@types/rimraf": "^2.0.2",
"@types/selenium-webdriver": "^3.0.16",
"@types/selenium-webdriver": "^4.0.3",
"@types/semver": "^5.5.0",
"@types/sinon": "^7.0.0",
"@types/strip-ansi": "^3.0.0",
Expand Down Expand Up @@ -429,7 +429,7 @@
"proxyquire": "1.8.0",
"regenerate": "^1.4.0",
"sass-lint": "^1.12.1",
"selenium-webdriver": "^4.0.0-alpha.4",
"selenium-webdriver": "^4.0.0-alpha.5",
"simple-git": "1.116.0",
"sinon": "^7.2.2",
"strip-ansi": "^3.0.1",
Expand Down
82 changes: 45 additions & 37 deletions test/functional/services/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
*/

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

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

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

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

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

/**
Expand All @@ -101,7 +99,10 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
* @return {Promise<{height: number, width: number, x: number, y: number}>}
*/
public async getWindowSize(): Promise<{ height: number; width: number; x: number; y: number }> {
return await (driver.manage().window() as any).getRect();
return await driver
.manage()
.window()
.getRect();
}

/**
Expand All @@ -112,10 +113,11 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
* @param {number} height
* @return {Promise<void>}
*/
public async setWindowSize(width: number, height: number): Promise<void>;
public async setWindowSize(...args: number[]): Promise<void>;
public async setWindowSize(...args: unknown[]): Promise<void> {
await (driver.manage().window() as any).setRect({ width: args[0], height: args[1] });
public async setWindowSize(width: number, height: number) {
await driver
.manage()
.window()
.setRect({ width, height });
}

/**
Expand All @@ -124,7 +126,7 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
*
* @return {Promise<string>}
*/
public async getCurrentUrl(): Promise<string> {
public async getCurrentUrl() {
// strip _t=Date query param when url is read
const current = await driver.getCurrentUrl();
const currentWithoutTime = modifyUrl(current, parsed => {
Expand All @@ -142,7 +144,7 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
* @param {boolean} insertTimestamp Optional
* @return {Promise<void>}
*/
public async get(url: string, insertTimestamp: boolean = true): Promise<void> {
public async get(url: string, insertTimestamp: boolean = true) {
if (insertTimestamp) {
const urlWithTime = modifyUrl(url, parsed => {
(parsed.query as any)._t = Date.now();
Expand All @@ -168,12 +170,12 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
.move({ x: 0, y: 0 })
.perform();
await this.getActions()
.move({ x: point.x, y: point.y, origin: 'pointer' })
.move({ x: point.x, y: point.y, origin: Origin.POINTER })
.perform();
} else {
await this.getActions()
.pause(this.getActions().mouse)
.move({ x: point.x, y: point.y, origin: 'pointer' })
.move({ x: point.x, y: point.y, origin: Origin.POINTER })
.perform();
}
}
Expand All @@ -198,7 +200,7 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
}
return data.location instanceof WebElementWrapper
? { x: data.offset.x || 0, y: data.offset.y || 0, origin: data.location._webElement }
: { x: data.location.x, y: data.location.y, origin: 'pointer' };
: { x: data.location.x, y: data.location.y, origin: Origin.POINTER };
};

const startPoint = getW3CPoint(from);
Expand All @@ -223,7 +225,7 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
return await this.getActions()
.move({ origin: from.location._webElement })
.press()
.move({ x: to.location.x, y: to.location.y, origin: 'pointer' })
.move({ x: to.location.x, y: to.location.y, origin: Origin.POINTER })
.release()
.perform();
} else {
Expand All @@ -243,7 +245,7 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
*
* @return {Promise<void>}
*/
public async refresh(): Promise<void> {
public async refresh() {
await driver.navigate().refresh();
}

Expand All @@ -253,10 +255,18 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
*
* @return {Promise<void>}
*/
public async goBack(): Promise<void> {
public async goBack() {
await driver.navigate().back();
}

/**
* Moves forwards in the browser history.
* https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/webdriver_exports_Navigation.html#forward
*/
public async goForward() {
await driver.navigate().forward();
}

/**
* Sends a sequance of keyboard keys. For each key, this will record a pair of keyDown and keyUp actions
* https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/input_exports_Actions.html#sendKeys
Expand All @@ -282,19 +292,19 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
* @param {x: number, y: number} point on browser page
* @return {Promise<void>}
*/
public async clickMouseButton(point: { x: number; y: number }): Promise<void> {
public async clickMouseButton(point: { x: number; y: number }) {
if (this.isW3CEnabled) {
await this.getActions()
.move({ x: 0, y: 0 })
.perform();
await this.getActions()
.move({ x: point.x, y: point.y, origin: 'pointer' })
.move({ x: point.x, y: point.y, origin: Origin.POINTER })
.click()
.perform();
} else {
await this.getActions()
.pause(this.getActions().mouse)
.move({ x: point.x, y: point.y, origin: 'pointer' })
.move({ x: point.x, y: point.y, origin: Origin.POINTER })
.click()
.perform();
}
Expand All @@ -307,7 +317,7 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
*
* @return {Promise<string>}
*/
public async getPageSource(): Promise<string> {
public async getPageSource() {
return await driver.getPageSource();
}

Expand All @@ -317,7 +327,7 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
*
* @return {Promise<Buffer>}
*/
public async takeScreenshot(): Promise<string> {
public async takeScreenshot() {
return await driver.takeScreenshot();
}

Expand All @@ -327,7 +337,7 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
* @param {WebElementWrapper} element
* @return {Promise<void>}
*/
public async doubleClick(): Promise<void> {
public async doubleClick() {
await this.getActions()
.doubleClick()
.perform();
Expand All @@ -341,10 +351,8 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
* @param {string} handle
* @return {Promise<void>}
*/
public async switchToWindow(handle: string): Promise<void>;
public async switchToWindow(...args: string[]): Promise<void>;
public async switchToWindow(...args: string[]): Promise<void> {
await (driver.switchTo() as any).window(...args);
public async switchToWindow(nameOrHandle: string) {
await driver.switchTo().window(nameOrHandle);
}

/**
Expand All @@ -353,7 +361,7 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
*
* @return {Promise<string[]>}
*/
public async getAllWindowHandles(): Promise<string[]> {
public async getAllWindowHandles() {
return await driver.getAllWindowHandles();
}

Expand All @@ -379,7 +387,7 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
*
* @return {Promise<void>}
*/
public async closeCurrentWindow(): Promise<void> {
public async closeCurrentWindow() {
await driver.close();
}

Expand Down Expand Up @@ -418,18 +426,18 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
);
}

public async getScrollTop(): Promise<number> {
public async getScrollTop() {
const scrollSize = await driver.executeScript<string>('return document.body.scrollTop');
return parseInt(scrollSize, 10);
}

public async getScrollLeft(): Promise<number> {
public async getScrollLeft() {
const scrollSize = await driver.executeScript<string>('return document.body.scrollLeft');
return parseInt(scrollSize, 10);
}

// return promise with REAL scroll position
public async setScrollTop(scrollSize: number | string): Promise<number> {
public async setScrollTop(scrollSize: number | string) {
await driver.executeScript('document.body.scrollTop = ' + scrollSize);
return this.getScrollTop();
}
Expand Down
8 changes: 4 additions & 4 deletions test/functional/services/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export async function FindProvider({ getService }: FtrProviderContext) {
elements = [];
}
// Force isStale checks for all the retrieved elements.
await Promise.all(elements.map(async (element: any) => await element.isEnabled()));
await Promise.all(elements.map(async element => await element.isEnabled()));
await this._withTimeout(defaultFindTimeout);
return elements;
});
Expand Down Expand Up @@ -322,7 +322,7 @@ export async function FindProvider({ getService }: FtrProviderContext) {
log.debug(`Find.clickByPartialLinkText('${linkText}') with timeout=${timeout}`);
await retry.try(async () => {
const element = await this.byPartialLinkText(linkText, timeout);
await (element as any).moveMouseTo();
await element.moveMouseTo();
await element.click();
});
}
Expand All @@ -334,7 +334,7 @@ export async function FindProvider({ getService }: FtrProviderContext) {
log.debug(`Find.clickByLinkText('${linkText}') with timeout=${timeout}`);
await retry.try(async () => {
const element = await this.byLinkText(linkText, timeout);
await (element as any).moveMouseTo();
await element.moveMouseTo();
await element.click();
});
}
Expand Down Expand Up @@ -470,7 +470,7 @@ export async function FindProvider({ getService }: FtrProviderContext) {
private async _withTimeout(timeout: number) {
if (timeout !== this.currentWait) {
this.currentWait = timeout;
await (driver.manage() as any).setTimeouts({ implicit: timeout });
await driver.manage().setTimeouts({ implicit: timeout });
}
}
}
Expand Down
Loading

0 comments on commit 36d4bcd

Please sign in to comment.