Skip to content

Commit 55a9ef5

Browse files
fix(uielement): isSelected and isChecked
1 parent 68b32eb commit 55a9ef5

File tree

4 files changed

+67
-30
lines changed

4 files changed

+67
-30
lines changed

lib/appium-server.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ChildProcess } from "child_process";
12
import { INsCapabilities } from "./interfaces/ns-capabilities";
23
import { IDeviceManager } from "./interfaces/device-manager";
34
export declare class AppiumServer {
@@ -10,7 +11,7 @@ export declare class AppiumServer {
1011
constructor(_args: INsCapabilities);
1112
port: number;
1213
runType: string;
13-
readonly server: any;
14+
readonly server: ChildProcess;
1415
hasStarted: boolean;
1516
start(port: any, deviceManager?: IDeviceManager): Promise<boolean | this>;
1617
private startAppiumServer;

lib/appium-server.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as child_process from "child_process";
1+
import { ChildProcess, spawn } from "child_process";
22
import {
33
log,
44
resolvePath,
@@ -18,7 +18,7 @@ import { existsSync } from "fs";
1818
import { killAllProcessAndRelatedCommand } from "mobile-devices-controller";
1919

2020
export class AppiumServer {
21-
private _server: any;
21+
private _server: ChildProcess;
2222
private _appium;
2323
private _port: number;
2424
private _runType: string;
@@ -113,7 +113,7 @@ export class AppiumServer {
113113

114114
logInfo(`Server args: `, startingServerArgs);
115115

116-
this._server = child_process.spawn(this._appium, startingServerArgs);
116+
this._server = spawn(this._appium, startingServerArgs);
117117
}
118118

119119
public async stop() {

lib/ui-element.d.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,17 @@ export declare class UIElement {
3838
*/
3939
text(): Promise<any>;
4040
/**
41-
* Returns if an element is selected
41+
* Returns if an element is selected
42+
*/
43+
isSelected(): Promise<any>;
44+
/**
45+
* Selected an element
4246
*/
4347
select(retries?: number): Promise<any>;
4448
/**
45-
* Returns if an element is selected
49+
* Returns if an element is checked
4650
*/
47-
isSelected(): Promise<any>;
51+
isChecked(): Promise<boolean>;
4852
/**
4953
* Get web driver element
5054
*/

lib/ui-element.ts

+55-23
Original file line numberDiff line numberDiff line change
@@ -77,42 +77,74 @@ export class UIElement {
7777
}
7878

7979
/**
80-
* Returns if an element is selected
80+
* Returns if an element is selected
81+
*/
82+
public async isSelected() {
83+
const el = (await this.element());
84+
if (!el) return false;
85+
if (this._args.isAndroid) {
86+
try {
87+
await el.getAttribute("selected");
88+
} catch (error) {
89+
console.error("Check if this is the correct element!");
90+
}
91+
}
92+
93+
try {
94+
return await el.isSelected();
95+
} catch (ex) {
96+
console.warn("'selected' attr is not reachable on this element!");
97+
}
98+
99+
console.warn("Trying use 'value' attr!");
100+
try {
101+
const attrValue = await el.getAttribute("value");
102+
return attrValue === "1" || attrValue === "true" || attrValue === true;
103+
} catch (error) {
104+
return false;
105+
}
106+
}
107+
108+
/**
109+
* Selected an element
81110
*/
82111
public async select(retries: number = 3) {
83112
(await (await this.element())).click();
84113
let el = (await this.element());
85-
if(!el) return el;
86-
87-
const hasSelectedAttr = await (await this.element()).getAttribute("selected");
88-
if (hasSelectedAttr) {
89-
let isSelected = await el.isSelected();
90-
while (retries >= 0 && !isSelected) {
91-
(await (await this.element())).click();
92-
isSelected = await el.isSelected();
93-
retries--;
94-
await this._driver.sleep(200);
95-
}
96-
} else {
97-
console.log(`This element doesn't contains selected attribute!`);
114+
if (!el) return el;
115+
116+
let isSelected = await this.isSelected();
117+
while (retries >= 0 && !isSelected) {
118+
(await (await this.element())).click();
119+
isSelected = await this.isSelected();
120+
retries--;
121+
await this._driver.sleep(200);
98122
}
99123

100124
return el;
101125
}
102126

103127
/**
104-
* Returns if an element is selected
128+
* Returns if an element is checked
105129
*/
106-
public async isSelected() {
130+
public async isChecked() {
107131
const el = (await this.element());
108-
if(!el) return false;
132+
if (!el) return false;
133+
if (this._args.isAndroid) {
134+
try {
135+
const isChecked = await el.getAttribute("checked");
136+
return isChecked === "true" || isChecked === true;
137+
} catch (error) {
138+
console.error("Check if this is the correct element!");
139+
}
140+
}
109141

110-
const hasSelectedAttr = await (await this.element()).getAttribute("selected");
111-
if (!hasSelectedAttr) {
112-
console.log(`This element doesn't contains selected attribute! Skip check!`);
113-
return true;
114-
} else {
115-
return await (await this.element()).isSelected();
142+
console.warn("Trying use 'value' attr!");
143+
try {
144+
const attrValue = await el.getAttribute("value");
145+
return attrValue === "1" || attrValue === "true" || attrValue === true;
146+
} catch (error) {
147+
return false;
116148
}
117149
}
118150

0 commit comments

Comments
 (0)