Skip to content

Commit

Permalink
WaitFor support for functions and xPath, close #100
Browse files Browse the repository at this point in the history
  • Loading branch information
andres committed Mar 31, 2018
1 parent 071d92c commit 92dcf3f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
==================

* Added optional selector to findByTextContaining
* WaitFor support for functions and xPath
* Back, forward and refresh methods
* Settings updated if changed between browser creation
* Fixed bug with error messages expect and actual values
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,17 @@ Returns the current url of the page
**wait(ms=250)**
Waits for the given milliseconds.

**waitFor(selector, timeout=500)**
**waitFor(selector, timeout=500, ...args?)**
Waits for given selector to exists and be visible, with the given timeout in milliseconds.

```js
await browser.waitFor(".popup");
```

If a function is passed instead of a selector, it will wait for that function to resolve in the browser context to true, the optional arguments are passed to the function.

> Css and Xpath selectors supported
**waitUntilNotVisible(selector, timeout=500)**
Waits until the given selector is no longer visible or doesn't exists, with the given timeout in milliseconds.

Expand Down
8 changes: 4 additions & 4 deletions lib/mixins/browser_wait.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ module.exports = function BrowserWaitMixin(s) {
});
}

waitFor(selector, timeout = 500) {
waitFor(selector, timeout = 500, ...args) {
this._failIfNotLoaded();
return this.page.waitForSelector(selector, {timeout: timeout, visible: true}).catch(() => {
return this.page.waitFor(selector, {timeout: timeout, visible: true}, ...args).catch(() => {
return Promise.reject(new Error(`Waiting for element "${selector}" failed, timeout of ${timeout}ms exceeded`));
});
}

waitUntilNotVisible(selector, timeout = 500) {
this._failIfNotLoaded();
return this.page.waitForFunction((selector) => {
return this.waitFor((selector) => {
const element = WendigoUtils.queryElement(selector);
return !WendigoUtils.isVisible(element);
}, {timeout: timeout}, selector).catch(() => {
}, timeout, selector).catch(() => {
return Promise.reject(new Error(`Waiting for element "${selector}" not to be visible, timeout of ${timeout}ms exceeded`));
});
}
Expand Down
23 changes: 23 additions & 0 deletions tests/browser/wait_for.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ describe("Wait For", function() {
await browser.assert.not.exists("#switch.on");
});

it("Wait For XPath", async () => {
await browser.open(configUrls.click);
await browser.click(".btn2");
await browser.waitFor("//*[@id='switch' and @class='off']", 600);
await browser.assert.exists("#switch.off");
await browser.assert.text("#switch", "Off");
await browser.assert.not.exists("#switch.on");
});

it("Wait For Timeout", async () => {
await browser.open(configUrls.click);
await browser.assert.exists("#switch.on");
Expand Down Expand Up @@ -65,4 +74,18 @@ describe("Wait For", function() {
await browser.waitFor(".hidden-text2", 10);
});
});

it("Wait For With Function", async () => {
await browser.open(configUrls.click);
await browser.assert.not.exists("#switch.off");
await browser.assert.exists("#switch.on");
await browser.click(".btn2");
await browser.waitFor((s) => {
const docs = document.querySelectorAll(s);
return docs.length > 0;
}, 600, "#switch.off");
await browser.assert.exists("#switch.off");
await browser.assert.text("#switch", "Off");
await browser.assert.not.exists("#switch.on");
});
});

0 comments on commit 92dcf3f

Please sign in to comment.