Skip to content

Commit

Permalink
[functional/services] update webdriver lib and types (#47381) (#47444)
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 authored Oct 7, 2019
1 parent d611db4 commit 3981d48
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 97 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,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.13",
"@types/strip-ansi": "^3.0.0",
Expand Down Expand Up @@ -434,7 +434,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.4.2",
"strip-ansi": "^3.0.1",
Expand Down
90 changes: 49 additions & 41 deletions test/functional/services/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
*/

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 Jimp, { Bitmap } from 'jimp';
import Jimp from 'jimp';
import { modifyUrl } from '../../../src/core/utils';
import { WebElementWrapper } from './lib/web_element_wrapper';
import { FtrProviderContext } from '../ftr_provider_context';
Expand All @@ -32,9 +34,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 @@ -57,7 +57,7 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
/**
* Keyboard events
*/
public readonly keys: IKey = Key;
public readonly keys = Key;

/**
* Browser name
Expand All @@ -77,10 +77,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 @@ -102,7 +100,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 @@ -113,18 +114,19 @@ 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 });
}

/**
* Gets a screenshot of the focused window and returns it as a Bitmap object
*/
public async getScreenshotAsBitmap(): Promise<Bitmap> {
public async getScreenshotAsBitmap() {
const screenshot = await this.takeScreenshot();
const buffer = Buffer.from(screenshot.toString(), 'base64');
const buffer = Buffer.from(screenshot, 'base64');
const session = (await Jimp.read(buffer)).clone();
return session.bitmap;
}
Expand All @@ -136,7 +138,7 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
* @param {number} height
* @return {Promise<void>}
*/
public async setScreenshotSize(width: number, height: number): Promise<void> {
public async setScreenshotSize(width: number, height: number) {
log.debug(`======browser======== setWindowSize ${width} ${height}`);
// We really want to set the Kibana app to a specific size without regard to the browser chrome (borders)
// But that means we first need to figure out the display scaling factor.
Expand Down Expand Up @@ -179,7 +181,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 @@ -197,7 +199,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 @@ -223,12 +225,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 @@ -253,7 +255,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 @@ -278,7 +280,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 @@ -298,7 +300,7 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
*
* @return {Promise<void>}
*/
public async refresh(): Promise<void> {
public async refresh() {
await driver.navigate().refresh();
}

Expand All @@ -308,10 +310,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 @@ -337,19 +347,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 @@ -362,7 +372,7 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
*
* @return {Promise<string>}
*/
public async getPageSource(): Promise<string> {
public async getPageSource() {
return await driver.getPageSource();
}

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

Expand All @@ -382,7 +392,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 @@ -396,10 +406,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 @@ -408,7 +416,7 @@ export async function BrowserProvider({ getService }: FtrProviderContext) {
*
* @return {Promise<string[]>}
*/
public async getAllWindowHandles(): Promise<string[]> {
public async getAllWindowHandles() {
return await driver.getAllWindowHandles();
}

Expand All @@ -434,7 +442,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 @@ -473,18 +481,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 @@ -149,7 +149,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 @@ -330,7 +330,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 @@ -342,7 +342,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 @@ -478,7 +478,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 3981d48

Please sign in to comment.