diff --git a/lib/appium-server.d.ts b/lib/appium-server.d.ts index 1ab5ad9..40c9363 100644 --- a/lib/appium-server.d.ts +++ b/lib/appium-server.d.ts @@ -1,3 +1,4 @@ +import { ChildProcess } from "child_process"; import { INsCapabilities } from "./interfaces/ns-capabilities"; import { IDeviceManager } from "./interfaces/device-manager"; export declare class AppiumServer { @@ -10,7 +11,7 @@ export declare class AppiumServer { constructor(_args: INsCapabilities); port: number; runType: string; - readonly server: any; + readonly server: ChildProcess; hasStarted: boolean; start(port: any, deviceManager?: IDeviceManager): Promise; private startAppiumServer; diff --git a/lib/appium-server.ts b/lib/appium-server.ts index b43f900..aaa5792 100644 --- a/lib/appium-server.ts +++ b/lib/appium-server.ts @@ -1,4 +1,4 @@ -import * as child_process from "child_process"; +import { ChildProcess, spawn } from "child_process"; import { log, resolvePath, @@ -18,7 +18,7 @@ import { existsSync } from "fs"; import { killAllProcessAndRelatedCommand } from "mobile-devices-controller"; export class AppiumServer { - private _server: any; + private _server: ChildProcess; private _appium; private _port: number; private _runType: string; @@ -113,7 +113,7 @@ export class AppiumServer { logInfo(`Server args: `, startingServerArgs); - this._server = child_process.spawn(this._appium, startingServerArgs); + this._server = spawn(this._appium, startingServerArgs); } public async stop() { diff --git a/lib/ui-element.d.ts b/lib/ui-element.d.ts index 895d35a..7468ba0 100644 --- a/lib/ui-element.d.ts +++ b/lib/ui-element.d.ts @@ -38,13 +38,17 @@ export declare class UIElement { */ text(): Promise; /** - * Returns if an element is selected + * Returns if an element is selected + */ + isSelected(): Promise; + /** + * Selected an element */ select(retries?: number): Promise; /** - * Returns if an element is selected + * Returns if an element is checked */ - isSelected(): Promise; + isChecked(): Promise; /** * Get web driver element */ diff --git a/lib/ui-element.ts b/lib/ui-element.ts index c7a91ee..8dbadbd 100644 --- a/lib/ui-element.ts +++ b/lib/ui-element.ts @@ -77,42 +77,74 @@ export class UIElement { } /** - * Returns if an element is selected + * Returns if an element is selected + */ + public async isSelected() { + const el = (await this.element()); + if (!el) return false; + if (this._args.isAndroid) { + try { + await el.getAttribute("selected"); + } catch (error) { + console.error("Check if this is the correct element!"); + } + } + + try { + return await el.isSelected(); + } catch (ex) { + console.warn("'selected' attr is not reachable on this element!"); + } + + console.warn("Trying use 'value' attr!"); + try { + const attrValue = await el.getAttribute("value"); + return attrValue === "1" || attrValue === "true" || attrValue === true; + } catch (error) { + return false; + } + } + + /** + * Selected an element */ public async select(retries: number = 3) { (await (await this.element())).click(); let el = (await this.element()); - if(!el) return el; - - const hasSelectedAttr = await (await this.element()).getAttribute("selected"); - if (hasSelectedAttr) { - let isSelected = await el.isSelected(); - while (retries >= 0 && !isSelected) { - (await (await this.element())).click(); - isSelected = await el.isSelected(); - retries--; - await this._driver.sleep(200); - } - } else { - console.log(`This element doesn't contains selected attribute!`); + if (!el) return el; + + let isSelected = await this.isSelected(); + while (retries >= 0 && !isSelected) { + (await (await this.element())).click(); + isSelected = await this.isSelected(); + retries--; + await this._driver.sleep(200); } return el; } /** - * Returns if an element is selected + * Returns if an element is checked */ - public async isSelected() { + public async isChecked() { const el = (await this.element()); - if(!el) return false; + if (!el) return false; + if (this._args.isAndroid) { + try { + const isChecked = await el.getAttribute("checked"); + return isChecked === "true" || isChecked === true; + } catch (error) { + console.error("Check if this is the correct element!"); + } + } - const hasSelectedAttr = await (await this.element()).getAttribute("selected"); - if (!hasSelectedAttr) { - console.log(`This element doesn't contains selected attribute! Skip check!`); - return true; - } else { - return await (await this.element()).isSelected(); + console.warn("Trying use 'value' attr!"); + try { + const attrValue = await el.getAttribute("value"); + return attrValue === "1" || attrValue === "true" || attrValue === true; + } catch (error) { + return false; } }