Skip to content

feat(ui-element): selected #191

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 2 commits into from
Jan 10, 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
2 changes: 1 addition & 1 deletion lib/device-manager.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export declare class DeviceManager implements IDeviceManager {
installApp(args: INsCapabilities): Promise<any>;
uninstallApp(args: INsCapabilities): Promise<any>;
static kill(device: IDevice): Promise<void>;
static getDefaultDevice(args: INsCapabilities, deviceName?: string, token?: string, type?: DeviceType, platformVersion?: number): IDevice;
static getDefaultDevice(args: INsCapabilities, deviceName?: string, token?: string, type?: DeviceType, platformVersion?: number): any;
static setDontKeepActivities(args: INsCapabilities, driver: any, value: any): Promise<void>;
static executeShellCommand(driver: any, commandArgs: {
command: string;
Expand Down
11 changes: 10 additions & 1 deletion lib/device-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ export class DeviceManager implements IDeviceManager {
const token = process.env["DEVICE_TOKEN"] || process.env.npm_config_deviceToken;
device.token = token && token.replace("emulator-", "");
device.name = process.env["DEVICE_NAME"] || device.name;

DeviceManager.cleanUnsetProperties(device);
console.log("Default device: ", device);

if (args.ignoreDeviceController) {
console.log("Default device: ", device);
}

if (shouldUserMobileDevicesController(args)) {
device = (await DeviceController.getDevices(device))[0];
Expand Down Expand Up @@ -111,6 +115,11 @@ export class DeviceManager implements IDeviceManager {
console.error("Check appium capabilities and provide correct device options!");
process.exit(1);
}

if (!args.ignoreDeviceController) {
console.log("Default device: ", device);
}

return device;
}

Expand Down
8 changes: 8 additions & 0 deletions lib/ui-element.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ export declare class UIElement {
* Get text of element
*/
text(): Promise<any>;
/**
* Returns if an element is selected
*/
select(retries?: number): Promise<any>;
/**
* Returns if an element is selected
*/
isSelected(): Promise<any>;
/**
* Get web driver element
*/
Expand Down
58 changes: 49 additions & 9 deletions lib/ui-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,46 @@ export class UIElement {
return await (await this.element()).text();
}

/**
* Returns if an element is selected
*/
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!`);
}

return el;
}

/**
* Returns if an element is selected
*/
public async isSelected() {
const el = (await this.element());
if(!el) return false;

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();
}
}

/**
* Get web driver element
*/
Expand Down Expand Up @@ -318,28 +358,28 @@ export class UIElement {
/**
* Easy to use in order to chain and search for nested elements
*/
public driver() {
public driver(): any {
return this._element.browser;
}

/**
* Swipe element left/right
* @param direction
*/
public async swipe(direction: Direction){
/**
* Swipe element left/right
* @param direction
*/
public async swipe(direction: Direction) {
const rectangle = await this.getRectangle();
const centerX = rectangle.x + rectangle.width / 2;
const centerY = rectangle.y + rectangle.height / 2;
let swipeX;
if(direction == Direction.right){
if (direction == Direction.right) {
const windowSize = await this._driver.getWindowSize();
swipeX = windowSize.width - 10;
} else if (direction == Direction.left){
} else if (direction == Direction.left) {
swipeX = 10;
} else {
console.log("Provided direction must be left or right !");
}

if (this._args.isAndroid) {
const action = new this._wd.TouchAction(this._driver);
action.press({ x: centerX, y: centerY })
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@
"watch": "tsc --watch",
"test": "mocha --timeout 999999"
}
}
}