/// <summary> /// Waits for an expression to be evaluated to a truthy value /// </summary> /// <param name="script">Expression to be evaluated in browser context</param> /// <param name="options">Optional waiting parameters</param> /// <returns>A task that resolves when the <c>script</c> returns a truthy value</returns> /// <seealso cref="Page.WaitForExpressionAsync(string, WaitForFunctionOptions)"/> /// <exception cref="WaitTaskTimeoutException">If timeout occurred.</exception> public async Task <JSHandle> WaitForExpressionAsync(string script, WaitForFunctionOptions options) { using (var waitTask = new WaitTask(this, script, true, "function", options.Polling, options.PollingInterval, options.Timeout)) { return(await waitTask.Task); } }
/// <summary> /// Waits for a function to be evaluated to a truthy value /// </summary> /// <param name="script">Function to be evaluated in browser context</param> /// <param name="options">Optional waiting parameters</param> /// <param name="args">Arguments to pass to <c>script</c></param> /// <returns>A task that resolves when the <c>script</c> returns a truthy value</returns> /// <seealso cref="Page.WaitForFunctionAsync(string, WaitForFunctionOptions, object[])"/> /// <exception cref="WaitTaskTimeoutException">If timeout occurred.</exception> public async Task <JSHandle> WaitForFunctionAsync(string script, WaitForFunctionOptions options, params object[] args) { using (var waitTask = new WaitTask(this, script, false, "function", options.Polling, options.PollingInterval, options.Timeout, args)) { return(await waitTask.Task); } }
private async Task <ElementHandle> WaitForSelectorOrXPathAsync(string selectorOrXPath, bool isXPath, WaitForSelectorOptions options = null) { options = options ?? new WaitForSelectorOptions(); var timeout = options.Timeout ?? _timeoutSettings.Timeout; const string predicate = @" function predicate(selectorOrXPath, isXPath, waitForVisible, waitForHidden) { const node = isXPath ? document.evaluate(selectorOrXPath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue : document.querySelector(selectorOrXPath); if (!node) return waitForHidden; if (!waitForVisible && !waitForHidden) return node; const element = node.nodeType === Node.TEXT_NODE ? node.parentElement : node; const style = window.getComputedStyle(element); const isVisible = style && style.visibility !== 'hidden' && hasVisibleBoundingBox(); const success = (waitForVisible === isVisible || waitForHidden === !isVisible); return success ? node : null; function hasVisibleBoundingBox() { const rect = element.getBoundingClientRect(); return !!(rect.top || rect.bottom || rect.width || rect.height); } }"; var polling = options.Visible || options.Hidden ? WaitForFunctionPollingOption.Raf : WaitForFunctionPollingOption.Mutation; using var waitTask = new WaitTask( this, predicate, false, $"{(isXPath ? "XPath" : "selector")} '{selectorOrXPath}'{(options.Hidden ? " to be hidden" : string.Empty)}", polling, null, timeout, new object[] { selectorOrXPath, isXPath, options.Visible, options.Hidden }); var handle = await waitTask.Task.ConfigureAwait(false); if (!(handle is ElementHandle elementHandle)) { if (handle != null) { await handle.DisposeAsync().ConfigureAwait(false); } return(null); } return(elementHandle); }
internal async Task <JSHandle> WaitForExpressionAsync(string script, WaitForFunctionOptions options) { using var waitTask = new WaitTask( this, script, true, "function", options.Polling, options.PollingInterval, options.Timeout ?? _timeoutSettings.Timeout); return(await waitTask .Task .ConfigureAwait(false)); }