Skip to content

fix: waitfortext doesnt throw error when text doesnt exist #4195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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