Skip to content

Commit bee6a6c

Browse files
GREENpointDavertMik
authored andcommitted
Add arguments pass for waitForFunction (codeceptjs#1149)
1 parent 7e9f5d6 commit bee6a6c

13 files changed

+90
-19
lines changed

docs/helpers/Nightmare.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ First parameter can be set to `maximize`
532532
## saveScreenshot
533533

534534
Saves a screenshot to ouput folder (set in codecept.json).
535-
Filename is relative to output folder.
535+
Filename is relative to output folder.
536536
Optionally resize the window to the full available page `scrollHeight` and `scrollWidth` to capture the entire page by passing `true` in as the second argument.
537537

538538
```js
@@ -835,9 +835,14 @@ I.waitForElement('.btn.continue', 5); // wait for 5 secs
835835
Waits for a function to return true (waits for 1 sec by default).
836836
Running in browser context.
837837

838+
```js
839+
I.waitForFunction(fn[, [args[, timeout]])
840+
```
841+
838842
```js
839843
I.waitForFunction(() => window.requests == 0);
840844
I.waitForFunction(() => window.requests == 0, 5); // waits for 5 sec
845+
I.waitForFunction((count) => window.requests == count, [3], 5) // pass args and wait for 5 sec
841846
```
842847

843848
**Parameters**

docs/helpers/Protractor.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ Performs right click on an element matched by CSS or XPath.
775775
## saveScreenshot
776776

777777
Saves a screenshot to ouput folder (set in codecept.json).
778-
Filename is relative to output folder.
778+
Filename is relative to output folder.
779779
Optionally resize the window to the full available page `scrollHeight` and `scrollWidth` to capture the entire page by passing `true` in as the second argument.
780780

781781
```js
@@ -1180,9 +1180,14 @@ Element can be located by CSS or XPath.
11801180
Waits for a function to return true (waits for 1 sec by default).
11811181
Running in browser context.
11821182

1183+
```js
1184+
I.waitForFunction(fn[, [args[, timeout]])
1185+
```
1186+
11831187
```js
11841188
I.waitForFunction(() => window.requests == 0);
11851189
I.waitForFunction(() => window.requests == 0, 5); // waits for 5 sec
1190+
I.waitForFunction((count) => window.requests == count, [3], 5) // pass args and wait for 5 sec
11861191
```
11871192

11881193
**Parameters**

docs/helpers/Puppeteer.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ Performs right click on an element matched by CSS or XPath.
771771
## saveScreenshot
772772

773773
Saves a screenshot to ouput folder (set in codecept.json).
774-
Filename is relative to output folder.
774+
Filename is relative to output folder.
775775
Optionally resize the window to the full available page `scrollHeight` and `scrollWidth` to capture the entire page by passing `true` in as the second argument.
776776

777777
```js
@@ -1163,9 +1163,14 @@ Element can be located by CSS or XPath.
11631163
Waits for a function to return true (waits for 1 sec by default).
11641164
Running in browser context.
11651165

1166+
```js
1167+
I.waitForFunction(fn[, [args[, timeout]])
1168+
```
1169+
11661170
```js
11671171
I.waitForFunction(() => window.requests == 0);
11681172
I.waitForFunction(() => window.requests == 0, 5); // waits for 5 sec
1173+
I.waitForFunction((count) => window.requests == count, [3], 5) // pass args and wait for 5 sec
11691174
```
11701175

11711176
**Parameters**
@@ -1194,7 +1199,7 @@ See [Pupeteer's reference](https://github.com/GoogleChrome/puppeteer/blob/master
11941199

11951200
**Parameters**
11961201

1197-
- `opts` **Any**
1202+
- `opts` **Any**
11981203

11991204
## waitForText
12001205

docs/helpers/WebDriverIO.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ placeholder for ~ locator only test case write once run on both Appium and WebDr
878878
## saveScreenshot
879879
880880
Saves a screenshot to ouput folder (set in codecept.json).
881-
Filename is relative to output folder.
881+
Filename is relative to output folder.
882882
Optionally resize the window to the full available page `scrollHeight` and `scrollWidth` to capture the entire page by passing `true` in as the second argument.
883883
884884
```js
@@ -1301,9 +1301,14 @@ Element can be located by CSS or XPath.
13011301
Waits for a function to return true (waits for 1 sec by default).
13021302
Running in browser context.
13031303
1304+
```js
1305+
I.waitForFunction(fn[, [args[, timeout]])
1306+
```
1307+
13041308
```js
13051309
I.waitForFunction(() => window.requests == 0);
13061310
I.waitForFunction(() => window.requests == 0, 5); // waits for 5 sec
1311+
I.waitForFunction((count) => window.requests == count, [3], 5) // pass args and wait for 5 sec
13071312
```
13081313
13091314
**Parameters**

docs/webapi/waitForFunction.mustache

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
Waits for a function to return true (waits for 1 sec by default).
22
Running in browser context.
33

4+
```js
5+
I.waitForFunction(fn[, [args[, timeout]])
6+
```
7+
48
```js
59
I.waitForFunction(() => window.requests == 0);
610
I.waitForFunction(() => window.requests == 0, 5); // waits for 5 sec
11+
I.waitForFunction((count) => window.requests == count, [3], 5) // pass args and wait for 5 sec
712
```
813

914
@param function to be executed in browser context
10-
@param sec time seconds to wait, 1 by default
15+
@param args arguments for function
16+
@param sec time seconds to wait, 1 by default

lib/helper/Nightmare.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -899,9 +899,17 @@ class Nightmare extends Helper {
899899
/**
900900
* {{> ../webapi/waitForFunction }}
901901
*/
902-
async waitForFunction(fn, sec = null) {
902+
async waitForFunction(fn, argsOrSec = null, sec = null) {
903+
let args = [];
904+
if (argsOrSec) {
905+
if (Array.isArray(argsOrSec)) {
906+
args = argsOrSec;
907+
} else if (typeof argsOrSec === 'number') {
908+
sec = argsOrSec;
909+
}
910+
}
903911
this.browser.options.waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
904-
return this.browser.wait(fn);
912+
return this.browser.wait(fn, ...args);
905913
}
906914

907915
/**

lib/helper/Protractor.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,9 +1381,18 @@ class Protractor extends Helper {
13811381
/**
13821382
* {{> ../webapi/waitForFunction }}
13831383
*/
1384-
async waitForFunction(fn, sec = null, timeoutMsg = null) {
1384+
async waitForFunction(fn, argsOrSec = null, sec = null) {
1385+
let args = [];
1386+
if (argsOrSec) {
1387+
if (Array.isArray(argsOrSec)) {
1388+
args = argsOrSec;
1389+
} else if (typeof argsOrSec === 'number') {
1390+
sec = argsOrSec;
1391+
}
1392+
}
1393+
13851394
const aSec = sec || this.options.waitForTimeout;
1386-
return this.browser.wait(() => this.browser.executeScript.call(this.browser, fn), aSec * 1000, timeoutMsg);
1395+
return this.browser.wait(() => this.browser.executeScript.call(this.browser, fn, ...args), aSec * 1000);
13871396
}
13881397

13891398
/**

lib/helper/Puppeteer.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,11 +1664,18 @@ class Puppeteer extends Helper {
16641664
/**
16651665
* {{> ../webapi/waitForFunction }}
16661666
*/
1667-
async waitForFunction(fn, sec = null) {
1668-
const aSec = sec || this.options.waitForTimeout;
1669-
const waitTimeout = aSec * 1000;
1667+
async waitForFunction(fn, argsOrSec = null, sec = null) {
1668+
let args = [];
1669+
if (argsOrSec) {
1670+
if (Array.isArray(argsOrSec)) {
1671+
args = argsOrSec;
1672+
} else if (typeof argsOrSec === 'number') {
1673+
sec = argsOrSec;
1674+
}
1675+
}
1676+
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
16701677
const context = await this._getContext();
1671-
return context.waitForFunction(fn, { timeout: waitTimeout });
1678+
return context.waitForFunction(fn, { timeout: waitTimeout }, ...args);
16721679
}
16731680

16741681
/**
@@ -1688,8 +1695,7 @@ class Puppeteer extends Helper {
16881695
*/
16891696
async waitUntil(fn, sec = null) {
16901697
console.log('This method will remove in CodeceptJS 1.4; use `waitForFunction` instead!');
1691-
const aSec = sec || this.options.waitForTimeout;
1692-
const waitTimeout = aSec * 1000;
1698+
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
16931699
const context = await this._getContext();
16941700
return context.waitForFunction(fn, { timeout: waitTimeout });
16951701
}
@@ -2025,4 +2031,3 @@ function targetCreatedHandler(page) {
20252031
consoleLogStore.add(msg);
20262032
});
20272033
}
2028-

lib/helper/WebDriverIO.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,10 +1642,19 @@ class WebDriverIO extends Helper {
16421642
* {{> ../webapi/waitForFunction }}
16431643
* Appium: support
16441644
*/
1645-
async waitForFunction(fn, sec = null, timeoutMsg = null) {
1645+
async waitForFunction(fn, argsOrSec = null, sec = null) {
1646+
let args = [];
1647+
if (argsOrSec) {
1648+
if (Array.isArray(argsOrSec)) {
1649+
args = argsOrSec;
1650+
} else if (typeof argsOrSec === 'number') {
1651+
sec = argsOrSec;
1652+
}
1653+
}
1654+
16461655
const aSec = sec || this.options.waitForTimeout;
16471656
const client = this.browser;
1648-
return client.waitUntil(async () => (await client.execute(fn)).value, aSec * 1000, timeoutMsg);
1657+
return client.waitUntil(async () => (await client.execute(fn, ...args)).value, aSec * 1000);
16491658
}
16501659

16511660
/**

test/helper/Nightmare_test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ describe('Nightmare', function () {
6969
describe('#waitForFunction', () => {
7070
it('should wait for function returns true', () => I.amOnPage('/form/wait_js')
7171
.then(() => I.waitForFunction(() => window.__waitJs, 3)));
72+
73+
it('should pass arguments and wait for function returns true', () => I.amOnPage('/form/wait_js')
74+
.then(() => I.waitForFunction(varName => window[varName], ['__waitJs'], 3)));
7275
});
7376

7477
// should work for webdriverio and seleniumwebdriver

0 commit comments

Comments
 (0)