Skip to content

Commit

Permalink
fix: waitfortext doesnt throw error when text doesnt exist (#4195)
Browse files Browse the repository at this point in the history
  • Loading branch information
kobenguyent authored Feb 15, 2024
1 parent 8f37eb3 commit 4d91aab
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
23 changes: 13 additions & 10 deletions lib/helper/Playwright.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const { v4: uuidv4 } = require('uuid');
const assert = require('assert');
const promiseRetry = require('promise-retry');
const Locator = require('../locator');
const store = require('../store');
const recorder = require('../recorder');
const stringIncludes = require('../assert/include').includes;
const { urlEquals } = require('../assert/equal');
Expand Down Expand Up @@ -2679,6 +2678,7 @@ class Playwright extends Helper {
*/
async waitForText(text, sec = null, context = null) {
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
const errorMessage = `Text "${text}" was not found on page after ${waitTimeout / 1000} sec.`;
let waiter;

const contextObject = await this._getContext();
Expand All @@ -2689,18 +2689,21 @@ class Playwright extends Helper {
try {
await contextObject.locator(`${locator.isCustom() ? `${locator.type}=${locator.value}` : locator.simplify()} >> text=${text}`).first().waitFor({ timeout: waitTimeout, state: 'visible' });
} catch (e) {
console.log(e);
throw new Error(`Text "${text}" was not found on page after ${waitTimeout / 1000} sec\n${e.message}`);
throw new Error(`${errorMessage}\n${e.message}`);
}
}

if (locator.isXPath()) {
waiter = contextObject.waitForFunction(([locator, text, $XPath]) => {
eval($XPath); // eslint-disable-line no-eval
const el = $XPath(null, locator);
if (!el.length) return false;
return el[0].innerText.indexOf(text) > -1;
}, [locator.value, text, $XPath.toString()], { timeout: waitTimeout });
try {
await contextObject.waitForFunction(([locator, text, $XPath]) => {
eval($XPath); // eslint-disable-line no-eval
const el = $XPath(null, locator);
if (!el.length) return false;
return el[0].innerText.indexOf(text) > -1;
}, [locator.value, text, $XPath.toString()], { timeout: waitTimeout });
} catch (e) {
throw new Error(`${errorMessage}\n${e.message}`);
}
}
} else {
// we have this as https://github.com/microsoft/playwright/issues/26829 is not yet implemented
Expand All @@ -2714,7 +2717,7 @@ class Playwright extends Helper {
count += 1000;
} while (count <= waitTimeout);

if (!waiter) throw new Error(`Text "${text}" was not found on page after ${waitTimeout / 1000} sec`);
if (!waiter) throw new Error(`${errorMessage}`);
}
}

Expand Down
12 changes: 12 additions & 0 deletions test/helper/webapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,18 @@ module.exports.tests = function () {
await I.dontSee('Dynamic text');
await I.waitForText('Dynamic text', 5, '//div[@id="text"]');
});

it('should throw error when text not found', async () => {
await I.amOnPage('/dynamic');
await I.dontSee('Dynamic text');
let failed = false;
try {
await I.waitForText('Some text', 1, '//div[@id="text"]');
} catch (e) {
failed = true;
}
assert.ok(failed);
});
});

describe('#waitForElement', () => {
Expand Down

0 comments on commit 4d91aab

Please sign in to comment.