Skip to content

Commit

Permalink
Custom message for waitFor API (#1997)
Browse files Browse the repository at this point in the history
* #1996 add support to custom message to waitFor

Signed-off-by: saikrishna321 <saikrishna321@yahoo.com>

* #1996 add docs

Signed-off-by: saikrishna321 <saikrishna321@yahoo.com>

* bump version

Signed-off-by: saikrishna321 <saikrishna321@yahoo.com>
  • Loading branch information
saikrishna321 committed Apr 19, 2021
1 parent 1c7724e commit 973559e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
6 changes: 4 additions & 2 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ const sleep = (milliseconds) => {
}
};

const waitUntil = async (condition, retryInterval, retryTimeout) => {
const waitUntil = async (condition, retryInterval, retryTimeout, message) => {
if (!retryTimeout) {
return;
}
Expand All @@ -134,7 +134,9 @@ const waitUntil = async (condition, retryInterval, retryTimeout) => {
}
if (new Date().getTime() - start > retryTimeout) {
if (!actualError) {
actualError = new Error(`waiting failed: retryTimeout ${retryTimeout}ms exceeded`);
actualError = new Error(
message || `waiting failed: retryTimeout ${retryTimeout}ms exceeded`,
);
}
throw actualError;
}
Expand Down
15 changes: 12 additions & 3 deletions lib/taiko.js
Original file line number Diff line number Diff line change
Expand Up @@ -2323,12 +2323,21 @@ module.exports.into = (value) => value;
*
* @param {string} element - Element/condition to wait for
* @param {number|time} time - Time to wait. default to 10s
* @param { options.message } - Custom message
* @return {promise}
*/

const waitFor = async (element, time) => {
const waitFor = async (element, time, options = {}) => {
validate();
let timeout = time || defaultConfig.retryTimeout;
let timeout;
let message;
if (isObject(time)) {
timeout = defaultConfig.retryTimeout;
message = time.message;
} else {
timeout = time || defaultConfig.retryTimeout;
message = options.message;
}
if (!element || isFinite(element)) {
time = element;
return wait(time);
Expand All @@ -2347,7 +2356,7 @@ const waitFor = async (element, time) => {
throw new Error(`Waiting Failed: Element '${element}' not found within ${timeout} ms`);
}
} else {
await waitUntil(element, defaultConfig.retryInterval, timeout);
await waitUntil(element, defaultConfig.retryInterval, timeout, message);
}
};

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "http://json.schemastore.org/package",
"name": "taiko",
"version": "1.2.3",
"version": "1.2.4",
"description": "Taiko is a Node.js library for automating Chromium based browsers",
"main": "bin/taiko.js",
"bin": {
Expand Down
10 changes: 10 additions & 0 deletions test/unit-tests/waitFor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ describe(test_name, function () {
).to.eventually.be.rejectedWith(expectedMessage);
});

it('should reject with custom message if element is not present', async () => {
await click('Click me');
const expectedMessage = 'Text Wait is eOver is not found';
await expect(
waitFor(async () => await $('//*[text()="Wait is eOver..!"]').exists(0, 0), 2000, {
message: 'Text Wait is eOver is not found',
}),
).to.eventually.be.rejectedWith(expectedMessage);
});

it('should wait for given condition', async () => {
await click('Click me');
await expect(waitFor(async () => await $('//*[text()="Wait is Over..!"]').exists(0.0))).not.to
Expand Down

0 comments on commit 973559e

Please sign in to comment.