Skip to content

Commit f86e728

Browse files
committed
configure retry to pick value from capability
1 parent acb3170 commit f86e728

File tree

2 files changed

+63
-41
lines changed

2 files changed

+63
-41
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ appium plugin install --source=npm appium-element-wait
1717

1818
## Installation - Client
1919

20-
No special action is needed to make things work on the client side. Just keep sending in your unprefixed caps!
20+
No special action is needed to make things work on the client side.
2121

2222
## Activation
2323

@@ -27,6 +27,10 @@ The plugin will not be active unless turned on when invoking the Appium server:
2727
appium --plugins=element-wait
2828
```
2929

30+
## Configuration
31+
32+
To override the default element-wait retry set `capabilities.setCapability("appium:element-wait", 20);`. Default value is 30.
33+
3034
### Example
3135

3236

src/plugin.js

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,88 +4,106 @@ import fetch from 'node-fetch';
44
let retryCount = 0;
55
export default class WaitCommandPlugin extends BasePlugin {
66
async findElement(next, driver, ...args) {
7+
this.driver = driver;
78
let originalRes;
89
const locatorArgs = JSON.parse(JSON.stringify(args));
910
this.strategy = locatorArgs[0];
1011
this.selector = locatorArgs[1];
11-
await this._find(driver, locatorArgs);
12-
await this._elementDisplayed(driver);
12+
await this._find(locatorArgs);
13+
await this._elementDisplayed();
1314
originalRes = await next();
1415
retryCount = 0;
1516
return originalRes;
1617
}
1718

18-
async _find(driver) {
19-
const baseUrl = this._constructSessionUrl(driver);
20-
const response = await fetch(
21-
`${baseUrl}wd/hub/session/${driver.sessionId}/element`,
22-
{
23-
body: JSON.stringify({
24-
strategy: this.strategy,
25-
selector: this.selector,
26-
context: '',
27-
multiple: false,
28-
}),
29-
method: 'POST',
30-
headers: { 'Content-Type': 'application/json' },
31-
}
32-
);
33-
const json = await response.json();
34-
if (json.value.error) {
35-
if (retryCount !== 25) {
19+
async _find() {
20+
const baseUrl = this._constructSessionUrl();
21+
const element = await this.elementState(baseUrl);
22+
if (element.value.error) {
23+
console.log('----', this._getTimeout());
24+
if (retryCount !== this._getTimeout()) {
3625
this.logger.info(
37-
`Retrying to find element with ${this.strategy} strategy for ${this.selector} selector`
26+
`Waiting for find element with ${this.strategy} strategy for ${this.selector} selector`
3827
);
3928
retryCount++;
40-
await this._find(driver);
29+
await this._find();
4130
}
4231
}
4332

44-
if (json.sessionId && json.value.ELEMENT) {
45-
this.element = json.value.ELEMENT;
33+
if (element.sessionId && element.value.ELEMENT) {
34+
this.element = element.value.ELEMENT;
4635
this.logger.info(
4736
`Element with ${this.strategy} strategy for ${this.selector} selector found.`
4837
);
4938
retryCount = 0;
5039
}
5140
}
5241

53-
async _elementDisplayed(driver) {
54-
this.logger.info(`Checking if ${this.selector} element is displayed`);
55-
const baseUrl = this._constructSessionUrl(driver);
42+
async elementState(baseUrl) {
5643
const response = await fetch(
57-
`${baseUrl}wd/hub/session/${driver.sessionId}/element/${this.element}/attribute/displayed`,
44+
`${baseUrl}wd/hub/session/${this.driver.sessionId}/element`,
5845
{
59-
method: 'GET',
46+
body: JSON.stringify({
47+
strategy: this.strategy,
48+
selector: this.selector,
49+
context: '',
50+
multiple: false,
51+
}),
52+
method: 'POST',
6053
headers: { 'Content-Type': 'application/json' },
6154
}
6255
);
63-
const json = await response.json();
64-
if (json.value.error) {
65-
if (retryCount !== 25) {
56+
return await response.json();
57+
}
58+
59+
async _elementDisplayed() {
60+
this.logger.info(`Checking if ${this.selector} element is displayed`);
61+
const baseUrl = this._constructSessionUrl();
62+
const response = await this.elementDisplayed(baseUrl);
63+
if (response.value.error) {
64+
if (retryCount !== this._getTimeout()) {
6665
this.logger.info(
6766
`Retrying to check whether ${this.selector} element is displayed or not`
6867
);
6968
retryCount++;
70-
await this._elementDisplayed(driver);
69+
await this._elementDisplayed();
7170
}
7271
}
73-
if (json.sessionId && json.value === 'true') {
72+
if (response.sessionId && response.value === 'true') {
7473
this.logger.info(`${this.selector} element is displayed.`);
7574
retryCount = 0;
7675
}
7776
}
7877

79-
_getAutomationName(driver) {
80-
return driver.caps.automationName;
78+
async elementDisplayed(baseUrl) {
79+
const response = await fetch(
80+
`${baseUrl}wd/hub/session/${this.driver.sessionId}/element/${this.element}/attribute/displayed`,
81+
{
82+
method: 'GET',
83+
headers: { 'Content-Type': 'application/json' },
84+
}
85+
);
86+
return await response.json();
87+
}
88+
89+
_getAutomationName() {
90+
return this.driver.caps.automationName;
8191
}
8292

83-
_constructSessionUrl(driver) {
84-
const automationName = this._getAutomationName(driver);
93+
_constructSessionUrl() {
94+
const automationName = this._getAutomationName();
8595
if (automationName === 'XCuiTest') {
86-
return `${driver.wda.wdaBaseUrl}:${driver.wda.wdaLocalPort}/`;
96+
return `${this.driver.wda.wdaBaseUrl}:${this.driver.wda.wdaLocalPort}/`;
97+
} else {
98+
return `http://${this.driver.uiautomator2.host}:${this.driver.uiautomator2.systemPort}/`;
99+
}
100+
}
101+
102+
_getTimeout() {
103+
if (this.driver.caps['element-wait']) {
104+
return (this.timeout = this.driver.caps['element-wait']);
87105
} else {
88-
return `http://${driver.uiautomator2.host}:${driver.uiautomator2.systemPort}/`;
106+
return (this.timeout = 30);
89107
}
90108
}
91109
}

0 commit comments

Comments
 (0)