Skip to content

Commit 0bf4e79

Browse files
feat(ui-element): selected (#191)
1 parent 665f85c commit 0bf4e79

File tree

5 files changed

+69
-12
lines changed

5 files changed

+69
-12
lines changed

lib/device-manager.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export declare class DeviceManager implements IDeviceManager {
99
installApp(args: INsCapabilities): Promise<any>;
1010
uninstallApp(args: INsCapabilities): Promise<any>;
1111
static kill(device: IDevice): Promise<void>;
12-
static getDefaultDevice(args: INsCapabilities, deviceName?: string, token?: string, type?: DeviceType, platformVersion?: number): IDevice;
12+
static getDefaultDevice(args: INsCapabilities, deviceName?: string, token?: string, type?: DeviceType, platformVersion?: number): any;
1313
static setDontKeepActivities(args: INsCapabilities, driver: any, value: any): Promise<void>;
1414
static executeShellCommand(driver: any, commandArgs: {
1515
command: string;

lib/device-manager.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ export class DeviceManager implements IDeviceManager {
2525
const token = process.env["DEVICE_TOKEN"] || process.env.npm_config_deviceToken;
2626
device.token = token && token.replace("emulator-", "");
2727
device.name = process.env["DEVICE_NAME"] || device.name;
28+
2829
DeviceManager.cleanUnsetProperties(device);
29-
console.log("Default device: ", device);
30+
31+
if (args.ignoreDeviceController) {
32+
console.log("Default device: ", device);
33+
}
3034

3135
if (shouldUserMobileDevicesController(args)) {
3236
device = (await DeviceController.getDevices(device))[0];
@@ -111,6 +115,11 @@ export class DeviceManager implements IDeviceManager {
111115
console.error("Check appium capabilities and provide correct device options!");
112116
process.exit(1);
113117
}
118+
119+
if (!args.ignoreDeviceController) {
120+
console.log("Default device: ", device);
121+
}
122+
114123
return device;
115124
}
116125

lib/ui-element.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ export declare class UIElement {
3737
* Get text of element
3838
*/
3939
text(): Promise<any>;
40+
/**
41+
* Returns if an element is selected
42+
*/
43+
select(retries?: number): Promise<any>;
44+
/**
45+
* Returns if an element is selected
46+
*/
47+
isSelected(): Promise<any>;
4048
/**
4149
* Get web driver element
4250
*/

lib/ui-element.ts

+49-9
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,46 @@ export class UIElement {
7676
return await (await this.element()).text();
7777
}
7878

79+
/**
80+
* Returns if an element is selected
81+
*/
82+
public async select(retries: number = 3) {
83+
(await (await this.element())).click();
84+
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!`);
98+
}
99+
100+
return el;
101+
}
102+
103+
/**
104+
* Returns if an element is selected
105+
*/
106+
public async isSelected() {
107+
const el = (await this.element());
108+
if(!el) return false;
109+
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();
116+
}
117+
}
118+
79119
/**
80120
* Get web driver element
81121
*/
@@ -318,28 +358,28 @@ export class UIElement {
318358
/**
319359
* Easy to use in order to chain and search for nested elements
320360
*/
321-
public driver() {
361+
public driver(): any {
322362
return this._element.browser;
323363
}
324364

325-
/**
326-
* Swipe element left/right
327-
* @param direction
328-
*/
329-
public async swipe(direction: Direction){
365+
/**
366+
* Swipe element left/right
367+
* @param direction
368+
*/
369+
public async swipe(direction: Direction) {
330370
const rectangle = await this.getRectangle();
331371
const centerX = rectangle.x + rectangle.width / 2;
332372
const centerY = rectangle.y + rectangle.height / 2;
333373
let swipeX;
334-
if(direction == Direction.right){
374+
if (direction == Direction.right) {
335375
const windowSize = await this._driver.getWindowSize();
336376
swipeX = windowSize.width - 10;
337-
} else if (direction == Direction.left){
377+
} else if (direction == Direction.left) {
338378
swipeX = 10;
339379
} else {
340380
console.log("Provided direction must be left or right !");
341381
}
342-
382+
343383
if (this._args.isAndroid) {
344384
const action = new this._wd.TouchAction(this._driver);
345385
action.press({ x: centerX, y: centerY })

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,4 @@
5656
"watch": "tsc --watch",
5757
"test": "mocha --timeout 999999"
5858
}
59-
}
59+
}

0 commit comments

Comments
 (0)