Skip to content

Commit 85c38ac

Browse files
committed
check if element is enabled
1 parent 0b38144 commit 85c38ac

File tree

3 files changed

+89
-82
lines changed

3 files changed

+89
-82
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"test": "mocha --require ts-node/register -p __tests__/e2e.spec.js --timeout 60000",
88
"build": "npx tsc",
9-
"prettier": "prettier --write --single-quote",
9+
"prettier": "prettier src --write --single-quote",
1010
"uninstall": "(appium plugin uninstall element-wait || exit 0)",
1111
"plugin:install": "appium plugin install --source=local $(pwd)",
1212
"plugin:installer": "npm run uninstall && npm run build && npm run plugin:install && appium server -ka 800 --use-plugins=element-wait -pa /wd/hub"

src/element.js

Lines changed: 76 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
import fetch from 'node-fetch';
22
import log from './logger';
3-
import { waitUntil } from 'async-wait-until';
3+
import { waitUntil, TimeoutError } from 'async-wait-until';
44

55
const defaultConfig = {
66
timeout: '10000',
77
intervalBetweenAttempts: '500',
88
};
9-
class Element {
10-
constructor(driver, args) {
11-
this.driver = driver;
12-
const locatorArgs = JSON.parse(JSON.stringify(args));
13-
this.strategy = locatorArgs[0];
14-
this.selector = locatorArgs[1];
15-
this.sessionInfo = this.sessionInfo(driver);
16-
this._getTimeout();
17-
}
189

19-
async find() {
20-
const predicate = async () => {
21-
const ele = await this.elementState();
22-
if (ele.value.error == undefined) {
23-
return ele;
24-
} else {
25-
log.info(
26-
`Waiting to find element with ${this.strategy} strategy for ${this.selector} selector`
27-
);
28-
return false;
29-
}
30-
};
10+
export async function find(driver, args) {
11+
_getTimeout(driver);
12+
const locatorArgs = JSON.parse(JSON.stringify(args));
13+
const strategy = locatorArgs[0];
14+
const selector = locatorArgs[1];
15+
const predicate = async () => {
16+
const ele = await elementState(driver, strategy, selector);
17+
if (ele.value.error == undefined) {
18+
return ele;
19+
} else {
20+
log.info(
21+
`Waiting to find element with ${strategy} strategy for ${selector} selector`
22+
);
23+
return false;
24+
}
25+
};
26+
try {
3127
const element = await waitUntil(predicate, {
3228
timeout: defaultConfig.timeout,
3329
intervalBetweenAttempts: defaultConfig.intervalBetweenAttempts,
3430
});
3531
if (element.value.ELEMENT) {
36-
let elementViewState = await this.elementIsDisplayed(
32+
log.info(
33+
`Element with ${strategy} strategy for ${selector} selector found.`
34+
);
35+
let elementViewState = await elementIsDisplayed(
36+
driver,
3737
element.value.ELEMENT
3838
);
3939
if (elementViewState) log.info('Element is displayed!');
@@ -42,68 +42,66 @@ class Element {
4242
'Element was not displayed! Please make sure the element is in viewport to perform the action'
4343
);
4444
}
45+
} catch (e) {
46+
if (e instanceof TimeoutError) {
47+
throw new Error(
48+
`Time out after waiting for element ${selector} for ${defaultConfig.timeout}`
49+
);
50+
} else {
51+
console.error(e);
52+
}
4553
}
54+
}
4655

47-
async elementIsDisplayed(element) {
48-
log.info(
49-
`Element with ${this.strategy} strategy for ${this.selector} selector found.`
50-
);
51-
log.info('Check if element is displayed');
52-
return await this.driver.elementDisplayed(element);
53-
}
56+
async function elementIsDisplayed(driver, element) {
57+
log.info('Check if element is displayed');
58+
console.log(driver);
59+
return await driver.elementDisplayed(element);
60+
}
5461

55-
async elementState() {
56-
const response = await fetch(
57-
`${this.sessionInfo.baseUrl}session/${this.sessionInfo.jwProxySession}/element`,
58-
{
59-
body: JSON.stringify({
60-
strategy: this.strategy,
61-
selector: this.selector,
62-
context: '',
63-
multiple: false,
64-
}),
65-
method: 'POST',
66-
headers: { 'Content-Type': 'application/json' },
67-
}
68-
);
69-
return await response.json();
70-
}
62+
async function elementState(driver, strategy, selector) {
63+
const sessionDetails = sessionInfo(driver);
64+
const response = await fetch(
65+
`${sessionDetails.baseUrl}session/${sessionDetails.jwProxySession}/element`,
66+
{
67+
body: JSON.stringify({
68+
strategy,
69+
selector,
70+
context: '',
71+
multiple: false,
72+
}),
73+
method: 'POST',
74+
headers: { 'Content-Type': 'application/json' },
75+
}
76+
);
77+
return await response.json();
78+
}
7179

72-
_getAutomationName() {
73-
return this.driver.caps.automationName;
74-
}
80+
function _getAutomationName(driver) {
81+
return driver.caps.automationName;
82+
}
7583

76-
sessionInfo(driver) {
77-
const automationName = this._getAutomationName();
78-
if (automationName === 'XCuiTest') {
79-
return {
80-
baseUrl: `${driver.wda.wdaBaseUrl}:${driver.wda.wdaLocalPort}/`,
81-
jwProxySession: driver.wda.jwproxy.sessionId,
82-
};
83-
} else {
84-
return {
85-
baseUrl: `http://${driver.uiautomator2.host}:${driver.uiautomator2.systemPort}/`,
86-
jwProxySession: driver.uiautomator2.jwproxy.sessionId,
87-
};
88-
}
84+
function sessionInfo(driver) {
85+
const automationName = _getAutomationName(driver);
86+
if (automationName === 'XCuiTest') {
87+
return {
88+
baseUrl: `${driver.wda.wdaBaseUrl}:${driver.wda.wdaLocalPort}/`,
89+
jwProxySession: driver.wda.jwproxy.sessionId,
90+
};
91+
} else {
92+
return {
93+
baseUrl: `http://${driver.uiautomator2.host}:${driver.uiautomator2.systemPort}/`,
94+
jwProxySession: driver.uiautomator2.jwproxy.sessionId,
95+
};
8996
}
97+
}
9098

91-
_getTimeout() {
92-
if (this.driver.caps['element-wait-timeout']) {
93-
defaultConfig.timeout = this.driver.caps['element-wait-timeout'];
94-
}
95-
if (this.driver.caps['intervalBetweenAttempts']) {
96-
defaultConfig.intervalBetweenAttempts = this.driver.caps[
97-
'intervalBetweenAttempts'
98-
];
99-
}
99+
function _getTimeout(driver) {
100+
if (driver.caps['element-wait-timeout']) {
101+
defaultConfig.timeout = driver.caps['element-wait-timeout'];
100102
}
101-
102-
async handle(next, driver, cmdName, ...args) {
103-
console.log('Inside handle');
104-
console.log(cmdName, ...args);
105-
return await next();
103+
if (driver.caps['intervalBetweenAttempts']) {
104+
defaultConfig.intervalBetweenAttempts =
105+
driver.caps['intervalBetweenAttempts'];
106106
}
107107
}
108-
109-
export default Element;

src/plugin.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import BasePlugin from '@appium/base-plugin';
2-
import Element from './element';
2+
import { find } from './element';
33

44
export default class WaitCommandPlugin extends BasePlugin {
55
async findElement(next, driver, ...args) {
6-
const element = new Element(driver, args);
7-
await element.find();
6+
await find(driver, args);
7+
return await next();
8+
}
9+
10+
async handle(next, driver, cmdName, ...args) {
11+
if (cmdName === 'click' || cmdName === 'setValue') {
12+
const locatorArgs = JSON.parse(JSON.stringify(args));
13+
const elementId = locatorArgs[0];
14+
const isEnabled = await driver.elementEnabled(elementId);
15+
if (!isEnabled) throw new Error('Element is not enabled!');
16+
}
817
return await next();
918
}
1019
}

0 commit comments

Comments
 (0)