Skip to content

Commit 29c9ae4

Browse files
committed
Retry when element is not found!
1 parent a498c95 commit 29c9ae4

1 file changed

Lines changed: 25 additions & 9 deletions

File tree

src/plugin.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,42 @@
11
import BasePlugin from '@appium/base-plugin';
22
import fetch from 'node-fetch';
3-
const retry = require('async-retry');
43

4+
let retryCount = 0;
55
export default class WaitCommandPlugin extends BasePlugin {
66
async findElement(next, driver, ...args) {
7-
console.log(`Before findElement is run with args ${JSON.stringify(args)}`);
7+
const locatorArgs = JSON.parse(JSON.stringify(args));
88
let originalRes;
9+
try {
10+
await this._find(driver, locatorArgs);
11+
} catch (e) {
12+
originalRes = await next();
13+
}
14+
return originalRes;
15+
}
16+
17+
async _find(driver, locatorArgs) {
918
const response = await fetch(
1019
`http://localhost:8200/wd/hub/session/${driver.sessionId}/element`,
1120
{
12-
body: JSON.stringify({ using: 'accessibility id', value: 'Views' }),
21+
body: JSON.stringify({
22+
strategy: locatorArgs[0],
23+
selector: locatorArgs[1],
24+
context: '',
25+
multiple: false,
26+
}),
1327
method: 'POST',
1428
headers: { 'Content-Type': 'application/json' },
1529
}
1630
);
1731
const json = await response.json();
18-
console.log('====================================');
1932
console.log(json);
20-
console.log('====================================');
21-
originalRes = await next();
22-
console.log('After findElement is run', originalRes);
23-
console.log('driver details', driver);
24-
return originalRes;
33+
if (retryCount === 5) {
34+
throw new Error('Element not found...');
35+
}
36+
if (json.value.error) {
37+
console.log(json.value.error, 'Retying...');
38+
retryCount++;
39+
await this._find(driver, locatorArgs);
40+
}
2541
}
2642
}

0 commit comments

Comments
 (0)