Skip to content

Commit 4d91aab

Browse files
authored
fix: waitfortext doesnt throw error when text doesnt exist (#4195)
1 parent 8f37eb3 commit 4d91aab

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

lib/helper/Playwright.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const { v4: uuidv4 } = require('uuid');
66
const assert = require('assert');
77
const promiseRetry = require('promise-retry');
88
const Locator = require('../locator');
9-
const store = require('../store');
109
const recorder = require('../recorder');
1110
const stringIncludes = require('../assert/include').includes;
1211
const { urlEquals } = require('../assert/equal');
@@ -2679,6 +2678,7 @@ class Playwright extends Helper {
26792678
*/
26802679
async waitForText(text, sec = null, context = null) {
26812680
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
2681+
const errorMessage = `Text "${text}" was not found on page after ${waitTimeout / 1000} sec.`;
26822682
let waiter;
26832683

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

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

2717-
if (!waiter) throw new Error(`Text "${text}" was not found on page after ${waitTimeout / 1000} sec`);
2720+
if (!waiter) throw new Error(`${errorMessage}`);
27182721
}
27192722
}
27202723

test/helper/webapi.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,18 @@ module.exports.tests = function () {
986986
await I.dontSee('Dynamic text');
987987
await I.waitForText('Dynamic text', 5, '//div[@id="text"]');
988988
});
989+
990+
it('should throw error when text not found', async () => {
991+
await I.amOnPage('/dynamic');
992+
await I.dontSee('Dynamic text');
993+
let failed = false;
994+
try {
995+
await I.waitForText('Some text', 1, '//div[@id="text"]');
996+
} catch (e) {
997+
failed = true;
998+
}
999+
assert.ok(failed);
1000+
});
9891001
});
9901002

9911003
describe('#waitForElement', () => {

0 commit comments

Comments
 (0)