Skip to content

fix(uielement): isSelected and isChecked #203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/appium-server.d.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<boolean | this>;
private startAppiumServer;
Expand Down
6 changes: 3 additions & 3 deletions lib/appium-server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as child_process from "child_process";
import { ChildProcess, spawn } from "child_process";
import {
log,
resolvePath,
Expand All @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
10 changes: 7 additions & 3 deletions lib/ui-element.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ export declare class UIElement {
*/
text(): Promise<any>;
/**
* Returns if an element is selected
* Returns if an element is selected
*/
isSelected(): Promise<any>;
/**
* Selected an element
*/
select(retries?: number): Promise<any>;
/**
* Returns if an element is selected
* Returns if an element is checked
*/
isSelected(): Promise<any>;
isChecked(): Promise<boolean>;
/**
* Get web driver element
*/
Expand Down
78 changes: 55 additions & 23 deletions lib/ui-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down