Skip to content

Commit 83288fb

Browse files
committed
fix element enabled check
1 parent 26e97fe commit 83288fb

File tree

5 files changed

+49
-37
lines changed

5 files changed

+49
-37
lines changed

__tests__/e2e.spec.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ describe('Plugin Test', () => {
2222
});
2323

2424
it('Vertical swipe test', async () => {
25-
await driver.$('~username').isEnabled();
2625
await driver.$('~username').click();
2726
await driver.$('~username').clearValue();
2827
await driver.$('~username').setValue('admin');
@@ -31,6 +30,6 @@ describe('Plugin Test', () => {
3130
});
3231

3332
afterEach(async () => {
34-
await driver.deleteSession();
33+
// await driver.deleteSession();
3534
});
3635
});

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "appium-wait-plugin",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "An appium 2.0 plugin that waits for element to be found",
55
"main": "./lib/index.js",
66
"scripts": {

src/element.js

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import fetch from 'node-fetch';
22
import log from './logger';
33
import { waitUntil, TimeoutError } from 'async-wait-until';
44

5-
const defaultConfig = {
6-
timeout: '10000',
7-
intervalBetweenAttempts: '500',
8-
};
5+
let map = new Map();
96

10-
export async function find(driver, args, cliArgs) {
11-
_getTimeout(cliArgs);
7+
export async function find(driver, args) {
8+
const elementWaitProps = JSON.parse(JSON.stringify(driver.opts.plugin['element-wait']));
9+
const session = driver.sessionId;
10+
_setTimeout(elementWaitProps, session);
11+
let timeoutProp = _getTimeout(session);
1212
const locatorArgs = JSON.parse(JSON.stringify(args));
1313
const strategy = locatorArgs[0];
1414
const selector = locatorArgs[1];
@@ -22,10 +22,7 @@ export async function find(driver, args, cliArgs) {
2222
}
2323
};
2424
try {
25-
const element = await waitUntil(predicate, {
26-
timeout: defaultConfig.timeout,
27-
intervalBetweenAttempts: defaultConfig.intervalBetweenAttempts,
28-
});
25+
const element = await waitUntil(predicate, timeoutProp);
2926
if (element.value.ELEMENT) {
3027
log.info(`Element with ${strategy} strategy for ${selector} selector found.`);
3128
let elementViewState = await elementIsDisplayed(driver, element.value.ELEMENT);
@@ -38,18 +35,19 @@ export async function find(driver, args, cliArgs) {
3835
} catch (e) {
3936
if (e instanceof TimeoutError) {
4037
throw new Error(
41-
`Time out after waiting for element ${selector} for ${defaultConfig.timeout}`
38+
`Time out after waiting for element ${selector} for ${timeoutProp.timeout} ms`
4239
);
4340
} else {
4441
console.error(e);
4542
}
4643
}
4744
}
4845

49-
export async function elementEnabled(driver) {
46+
export async function elementEnabled(driver, el) {
47+
let timeoutProp = _getTimeout(driver.sessionId);
5048
const predicate = async () => {
51-
const element = await driver.elementEnabled();
52-
if (element.value === 'true') {
49+
const element = await driver.elementEnabled(el);
50+
if (element) {
5351
return element;
5452
} else {
5553
log.info('Waiting to find element to be enabled');
@@ -58,14 +56,11 @@ export async function elementEnabled(driver) {
5856
};
5957

6058
try {
61-
await waitUntil(predicate, {
62-
timeout: defaultConfig.timeout,
63-
intervalBetweenAttempts: defaultConfig.intervalBetweenAttempts,
64-
});
59+
await waitUntil(predicate, timeoutProp);
6560
} catch (e) {
6661
if (e instanceof TimeoutError) {
6762
throw new Error(
68-
`Time out after waiting for element to be enabled for ${defaultConfig.timeout}`
63+
`Time out after waiting for element to be enabled for ${timeoutProp.timeout}`
6964
);
7065
} else {
7166
console.error(e);
@@ -119,16 +114,34 @@ function sessionInfo(driver, strategy, selector) {
119114
}
120115
}
121116

122-
function _getTimeout(cliArgs) {
123-
if (cliArgs.timeout != undefined && cliArgs.timeout !== defaultConfig.timeout) {
124-
defaultConfig.timeout = cliArgs.timeout;
125-
log.info(`Timeout is set to ${defaultConfig.timeout}`);
126-
}
127-
if (
128-
cliArgs.intervalBetweenAttempts != undefined &&
129-
cliArgs.intervalBetweenAttempts !== defaultConfig.intervalBetweenAttempts
130-
) {
131-
defaultConfig.intervalBetweenAttempts = cliArgs.intervalBetweenAttempts;
132-
log.info(`Internal between attempts is set to ${defaultConfig.intervalBetweenAttempts}`);
117+
function _setTimeout(elementWaitProps, session) {
118+
if (!map.has(session)) {
119+
log.info(`Timeout properties not set for session ${session}, trying to set one`);
120+
let defaultTimeoutProp = {
121+
timeout: 10000,
122+
intervalBetweenAttempts: 500,
123+
};
124+
if (
125+
elementWaitProps.timeout != undefined &&
126+
elementWaitProps.timeout !== defaultTimeoutProp.timeout
127+
) {
128+
defaultTimeoutProp.timeout = elementWaitProps.timeout;
129+
log.info(`Timeout is set to ${defaultTimeoutProp.timeout}`);
130+
}
131+
if (
132+
elementWaitProps.intervalBetweenAttempts != undefined &&
133+
elementWaitProps.intervalBetweenAttempts !== defaultTimeoutProp.intervalBetweenAttempts
134+
) {
135+
defaultTimeoutProp.intervalBetweenAttempts = elementWaitProps.intervalBetweenAttempts;
136+
log.info(`Internal between attempts is set to ${defaultTimeoutProp.intervalBetweenAttempts}`);
137+
}
138+
map.set(session, defaultTimeoutProp);
133139
}
140+
log.info(
141+
`Timeout properties set for session ${session} is ${JSON.stringify(_getTimeout(session))} ms`
142+
);
143+
}
144+
145+
function _getTimeout(session) {
146+
return map.get(session);
134147
}

src/plugin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default class WaitCommandPlugin extends BasePlugin {
1212
const includeCommands = ['click', 'setValue', 'clear'];
1313
if (includeCommands.includes(cmdName)) {
1414
log.info('Wait for element to be clickable');
15-
await elementEnabled(driver);
15+
await elementEnabled(driver, args[0]);
1616
log.info('Element is enabled');
1717
}
1818
return await next();
@@ -26,4 +26,4 @@ export default class WaitCommandPlugin extends BasePlugin {
2626
static async updateServer(expressApp, httpServer) {
2727
expressApp.all('/fake', WaitCommandPlugin.fakeRoute);
2828
}
29-
}
29+
}

0 commit comments

Comments
 (0)