internal async Task Rerun() { var runCount = ++_runCount; JSHandle success = null; Exception exception = null; var context = await _frame.GetExecutionContextAsync().ConfigureAwait(false); try { success = await context.EvaluateFunctionHandleAsync(WaitForPredicatePageFunction, new object[] { _predicateBody, _pollingInterval ?? (object)_polling, _timeout }.Concat(_args).ToArray()).ConfigureAwait(false); } catch (Exception ex) { exception = ex; } if (_terminated || runCount != _runCount) { if (success != null) { await success.DisposeAsync().ConfigureAwait(false); } return; } if (exception == null && await _frame.EvaluateFunctionAsync <bool>("s => !s", success) .ContinueWith(task => task.IsFaulted || task.Result) .ConfigureAwait(false)) { if (success != null) { await success.DisposeAsync().ConfigureAwait(false); } return; } if (exception?.Message.Contains("Execution context was destroyed") == true) { return; } if (exception?.Message.Contains("Cannot find context with specified id") == true) { return; } if (exception != null) { _taskCompletion.SetException(exception); } else { _taskCompletion.SetResult(success); } Cleanup(); }
/// <summary> /// Runs <paramref name="pageFunction"/> within the frame and passes it the outcome of <paramref name="arrayHandle"/> as the first argument. Use only after <see cref="Page.QuerySelectorAllHandleAsync(string)"/> /// </summary> /// <typeparam name="T"></typeparam> /// <param name="arrayHandle">An <see cref="JSHandle"/> that represents an array of <see cref="ElementHandle"/> that will be used as the first argument in <paramref name="pageFunction"/></param> /// <param name="pageFunction">Function to be evaluated in browser context</param> /// <param name="args">Arguments to pass to <c>pageFunction</c></param> /// <returns>Task which resolves to the return value of <c>pageFunction</c></returns> public static async Task <T> EvaluateFunctionAsync <T>(this JSHandle arrayHandle, string pageFunction, params object[] args) { var result = await arrayHandle.EvaluateFunctionAsync <T>(pageFunction, args).ConfigureAwait(false); await arrayHandle.DisposeAsync().ConfigureAwait(false); return(result); }
/// <summary> /// Runs <paramref name="pageFunction"/> within the frame and passes it the outcome of <paramref name="arrayHandle"/> as the first argument. Use only after <see cref="Page.QuerySelectorAllHandleAsync(string)"/> /// </summary> /// <typeparam name="T"></typeparam> /// <param name="arrayHandle">An <see cref="JSHandle"/> that represents an array of <see cref="ElementHandle"/> that will be used as the first argument in <paramref name="pageFunction"/></param> /// <param name="pageFunction">Function to be evaluated in browser context</param> /// <param name="args">Arguments to pass to <c>pageFunction</c></param> /// <returns>Task which resolves to the return value of <c>pageFunction</c></returns> public static async Task<T> EvaluateFunctionAsync<T>(this JSHandle arrayHandle, string pageFunction, params object[] args) { var response = await arrayHandle.JsonValueAsync<object[]>().ConfigureAwait(false); var newArgs = new object[args.Length + 1]; newArgs[0] = arrayHandle; args.CopyTo(newArgs, 1); var result = await arrayHandle.ExecutionContext.EvaluateFunctionAsync<T>(pageFunction, newArgs).ConfigureAwait(false); await arrayHandle.DisposeAsync().ConfigureAwait(false); return result; }
/// <summary> /// Runs <paramref name="pageFunction"/> within the frame and passes it the outcome of <paramref name="arrayHandle"/> as the first argument. Use only after <see cref="Page.QuerySelectorAllHandleAsync(string)"/> /// </summary> /// <typeparam name="T">The type to deserialize the result to</typeparam> /// <param name="arrayHandle">An <see cref="JSHandle"/> that represents an array of <see cref="ElementHandle"/> that will be used as the first argument in <paramref name="pageFunction"/></param> /// <param name="pageFunction">Function to be evaluated in browser context</param> /// <param name="args">Arguments to pass to <c>pageFunction</c></param> /// <returns>Task which resolves to the return value of <c>pageFunction</c></returns> public static async Task <T> EvaluateFunctionAsync <T>(this JSHandle arrayHandle, string pageFunction, params object[] args) { if (arrayHandle == null) { throw new ArgumentNullException(nameof(arrayHandle)); } var result = await arrayHandle.EvaluateFunctionAsync <T>(pageFunction, args).ConfigureAwait(false); await arrayHandle.DisposeAsync().ConfigureAwait(false); return(result); }