From bf5014d8023c3c9291431940d524678d54e43171 Mon Sep 17 00:00:00 2001 From: Zdravko Branzov Date: Thu, 15 Feb 2018 12:05:33 +0200 Subject: [PATCH] chore: add clickPoint functionality that clicks over the screen by provided coordinates. Add wait and search for element that might not exist. --- lib/appium-driver.d.ts | 26 ++++++++++++++++++++++- lib/appium-driver.ts | 47 ++++++++++++++++++++++++++++++++++++++++++ lib/ui-element.d.ts | 5 +++++ lib/ui-element.ts | 8 +++++++ 4 files changed, 85 insertions(+), 1 deletion(-) diff --git a/lib/appium-driver.d.ts b/lib/appium-driver.d.ts index e6d5283..0e9d17d 100644 --- a/lib/appium-driver.d.ts +++ b/lib/appium-driver.d.ts @@ -24,7 +24,7 @@ export declare class AppiumDriver { private _logPath; private _storageByDeviceName; private _storageByPlatform; - private constructor(); + private constructor(_driver, _wd, _webio, _driverConfig, _args); readonly imageHelper: ImageHelper; defaultWaitTime: number; readonly capabilities: any; @@ -120,6 +120,12 @@ export declare class AppiumDriver { * @param xOffset */ swipe(y: number, x: number, yOffset: number, inertia?: number, xOffset?: number): Promise; + /** + * Click a point by providing coordinates + * @param x + * @param y + */ + clickPoint(xCoordinate: number, yCoordinate: number): Promise; source(): Promise; sessionId(): Promise; compareElement(element: UIElement, imageName: string): Promise; @@ -138,4 +144,22 @@ export declare class AppiumDriver { private convertArrayToUIElements(array, searchM, args); private static configureLogging(driver, verbose); private getExpectedImagePath(imageName); + /** + * Wait specific amount of time before continue execution + * @param miliseconds + */ + wait(miliseconds: number): Promise; + /** + * Search for element by given xPath but does not throw error if can not find it. Instead returns 'undefined'. + * @param xPath + * @param waitForElement + */ + findElementByXPathIfExists(xPath: string, waitForElement?: number): Promise; + /** + * Search for element by given text but does not throw error if can not find it. Instead returns 'undefined'. + * @param text + * @param match + * @param waitForElement + */ + findElementByTextIfExists(text: string, match?: SearchOptions, waitForElement?: number): Promise; } diff --git a/lib/appium-driver.ts b/lib/appium-driver.ts index 32a4c79..176b9d9 100644 --- a/lib/appium-driver.ts +++ b/lib/appium-driver.ts @@ -265,6 +265,19 @@ export class AppiumDriver { await this._driver.sleep(150); } + /** + * Click a point by providing coordinates + * @param x + * @param y + */ + public async clickPoint(xCoordinate: number, yCoordinate: number) { + let action = new this._wd.TouchAction(this._driver); + action + .tap({ x: xCoordinate, y: yCoordinate }); + await action.perform(); + await this._driver.sleep(150); + } + public async source() { return await this._webio.source(); } @@ -533,4 +546,38 @@ export class AppiumDriver { return pathExpectedImage; } + + /** + * Wait specific amount of time before continue execution + * @param miliseconds + */ + public async wait(miliseconds: number) { + await this._driver.sleep(miliseconds); + } + + /** + * Search for element by given xPath but does not throw error if can not find it. Instead returns 'undefined'. + * @param xPath + * @param waitForElement + */ + public async findElementByXPathIfExists(xPath: string, waitForElement: number = this.defaultWaitTime) { + const element = await this._driver.elementByXPathIfExists(xPath, waitForElement); + if (element) { + const searchMethod = "elementByXPathIfExists"; + return await new UIElement(element, this._driver, this._wd, this._webio, this._args, searchMethod, xPath); + } else { + return undefined; + } + } + + /** + * Search for element by given text but does not throw error if can not find it. Instead returns 'undefined'. + * @param text + * @param match + * @param waitForElement + */ + public async findElementByTextIfExists(text: string, match: SearchOptions = SearchOptions.exact, waitForElement: number = this.defaultWaitTime) { + const shouldMatch = match === SearchOptions.exact ? true : false; + return await this.findElementByXPathIfExists(this._elementHelper.getXPathByText(text, shouldMatch), waitForElement); + } } \ No newline at end of file diff --git a/lib/ui-element.d.ts b/lib/ui-element.d.ts index 66e759d..95d563f 100644 --- a/lib/ui-element.d.ts +++ b/lib/ui-element.d.ts @@ -105,6 +105,11 @@ export declare class UIElement { * Click and hold over an element */ hold(): Promise; + /** + * Send keys to field or other UI component + * @param text + */ + sendKeys(text: string): Promise; log(): Promise; refetch(): Promise; } diff --git a/lib/ui-element.ts b/lib/ui-element.ts index f3966e7..575e144 100644 --- a/lib/ui-element.ts +++ b/lib/ui-element.ts @@ -243,6 +243,14 @@ export class UIElement { await this._driver.sleep(150); } + /** + * Send keys to field or other UI component + * @param text + */ + public async sendKeys(text: string) { + await this._element.sendKeys(text); + } + public async log() { console.dir(await this.element()); }