|
1 | 1 | import BasePlugin from '@appium/base-plugin'; |
2 | 2 | import fetch from 'node-fetch'; |
3 | | -const retry = require('async-retry'); |
4 | 3 |
|
| 4 | +let retryCount = 0; |
5 | 5 | export default class WaitCommandPlugin extends BasePlugin { |
6 | 6 | 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)); |
8 | 8 | 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) { |
9 | 18 | const response = await fetch( |
10 | 19 | `http://localhost:8200/wd/hub/session/${driver.sessionId}/element`, |
11 | 20 | { |
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 | + }), |
13 | 27 | method: 'POST', |
14 | 28 | headers: { 'Content-Type': 'application/json' }, |
15 | 29 | } |
16 | 30 | ); |
17 | 31 | const json = await response.json(); |
18 | | - console.log('===================================='); |
19 | 32 | 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 | + } |
25 | 41 | } |
26 | 42 | } |
0 commit comments