waitForCode(code [,options])
This is a TDK feature
This is not supported in Testim's visual editor
This method polls the page every 100 (default ms) until a passed JavaScript value is true
(well, truthy).
code
{Function} a JavaScript function to evaluate.- returns: Promise that fulfills when the expression evaluates to true.
options
{object}pollInterval
{ number } how often to poll the page in milliseconds (default 100ms)
For example:
// ✅ wait for page body loaded
await waitForCode(() => document.body); // wait for page body loaded
// ✅ wait for page body loaded, check every one second
await waitForCode(() => document.body, {
pollInterval: 10000
});
// ✅ poll a JavaScript variable
await waitForCode(() => window.isTestReady);
// ✅ poll a JavaScript variable
await waitForCode(() => window.isTestReady);
// ❌ wait for a promise, use `evaluate` instead
await waitForCode(() => fetch('/health'));
// ❌ pass a function and not a string
await waitForCode('window.body');
// ❌ because the code is sent to the browser - avoid closures
let foo = 'body';
await waitForCode(() => window[foo]));
Updated almost 3 years ago