private void OnExecutionContextCreated(ExecutionContextDescription contextPayload) { var auxData = contextPayload.AuxData != null ? ((JsonElement)contextPayload.AuxData).ToObject <ExecutionContextDescriptionAuxData>() : null; Frame frame = null; if (contextPayload.AuxData != null && !Page.FrameManager.Frames.TryGetValue(auxData.FrameId, out frame)) { return; } if (auxData?.Type == "isolated") { _isolatedWorlds.Add(contextPayload.Name); } var executionContextDelegate = new ChromiumExecutionContext(Client, contextPayload); var context = new FrameExecutionContext(executionContextDelegate, frame); if (auxData?.IsDefault == true) { frame.ContextCreated(ContextType.Main, context); } else if (contextPayload.Name == UtilityWorldName) { frame.ContextCreated(ContextType.Utility, context); } _contextIdToContext[contextPayload.Id.Value] = context; }
public async Task <T> EvaluateAsync <T>(FrameExecutionContext context, bool returnByValue, string script, object[] args) { string suffix = $"//# sourceURL={EvaluationScriptUrl}"; RemoteObject remoteObject = null; if (script.IsJavascriptFunction()) { var result = await _client.SendAsync(new RuntimeCallFunctionOnRequest { FunctionDeclaration = $"{script}\n{suffix}\n", ExecutionContextId = _contextId, Arguments = args.Select(a => FormatArgument(a, context)).ToArray(), ReturnByValue = returnByValue, AwaitPromise = true, UserGesture = true, }).ConfigureAwait(false); if (result.ExceptionDetails != null) { throw new PlaywrightSharpException($"Evaluation failed: {GetExceptionMessage(result.ExceptionDetails)}"); } remoteObject = result.Result; } else { string expressionWithSourceUrl = _sourceUrlRegex.IsMatch(script) ? script : script + '\n' + suffix; var result = await _client.SendAsync(new RuntimeEvaluateRequest { Expression = expressionWithSourceUrl, ContextId = _contextId, ReturnByValue = returnByValue, AwaitPromise = true, UserGesture = true, }).ConfigureAwait(false); if (result.ExceptionDetails != null) { throw new PlaywrightSharpException($"Evaluation failed: {GetExceptionMessage(result.ExceptionDetails)}"); } remoteObject = result.Result; } return((T)(returnByValue ? GetValueFromRemoteObject <T>(remoteObject) : context.CreateHandle(remoteObject))); }
private void OnExecutionContextCreated(RuntimeExecutionContextCreatedFirefoxEvent e) { var auxData = e.AuxData != null ? ((JsonElement)e.AuxData).ToObject <AuxData>() : null; if (auxData?.FrameId != null && Page.FrameManager.Frames.TryGetValue(auxData.FrameId, out var frame)) { var firefoxDelegate = new FirefoxExecutionContext(_session, e.ExecutionContextId); var context = new FrameExecutionContext(firefoxDelegate, frame); if (auxData.Name == UtilityWorldName) { frame.ContextCreated(ContextType.Utility, context); } else if (string.IsNullOrEmpty(auxData.Name)) { frame.ContextCreated(ContextType.Main, context); } ContextIdToContext[e.ExecutionContextId] = context; } }
public async Task <ElementHandle> AdoptElementHandleAsync(ElementHandle handle, FrameExecutionContext to) { var result = await _session.SendAsync(new PageAdoptNodeRequest { FrameId = handle.Context.Frame.Id, ObjectId = handle.RemoteObject.ObjectId, ExecutionContextId = ((FirefoxExecutionContext)to.Delegate).ExecutionContextId, }).ConfigureAwait(false); return(to.CreateHandle(result.RemoteObject) as ElementHandle); }
public Task <IElementHandle> AdoptElementHandleAsync(object arg, FrameExecutionContext frameExecutionContext) { throw new NotImplementedException(); }
public async Task EvaluateAsync(FrameExecutionContext context, bool returnByValue, string script, object[] args) => await EvaluateAsync <object>(context, returnByValue, script, args).ConfigureAwait(false);
private CallArgument FormatArgument(object arg, FrameExecutionContext context) { switch (arg) { case BigInteger big: return(new CallArgument { UnserializableValue = $"{big}n" }); case int integer when integer == -0: return(new CallArgument { UnserializableValue = "-0" }); case double d: if (double.IsPositiveInfinity(d)) { return(new CallArgument { UnserializableValue = "Infinity" }); } if (double.IsNegativeInfinity(d)) { return(new CallArgument { UnserializableValue = "-Infinity" }); } if (double.IsNaN(d)) { return(new CallArgument { UnserializableValue = "NaN" }); } break; case JSHandle objectHandle: if (objectHandle.Context != context) { throw new PlaywrightSharpException("JSHandles can be evaluated only in the context they were created!"); } if (objectHandle.Disposed) { throw new PlaywrightSharpException("JSHandle is disposed!"); } var remoteObject = objectHandle.RemoteObject; if (!string.IsNullOrEmpty(remoteObject.UnserializableValue)) { return(new CallArgument { UnserializableValue = remoteObject.UnserializableValue }); } if (string.IsNullOrEmpty(remoteObject.ObjectId)) { return(new CallArgument { Value = remoteObject.Value }); } return(new CallArgument { ObjectId = remoteObject.ObjectId }); } return(new CallArgument { Value = arg, }); }
private async Task <ElementHandle> AdoptBackendNodeIdAsync(int backendNodeId, FrameExecutionContext to) { DOMResolveNodeResponse result = null; try { result = await Client.SendAsync(new DOMResolveNodeRequest { BackendNodeId = backendNodeId, ExecutionContextId = ((ChromiumExecutionContext)to.Delegate).ContextId, }).ConfigureAwait(false); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); } if (result == null || result.Object.Subtype == "null") { throw new PlaywrightSharpException("Unable to adopt element handle from a different document"); } return(to.CreateHandle(result.Object) as ElementHandle); }
public async Task <ElementHandle> AdoptElementHandleAsync(ElementHandle handle, FrameExecutionContext to) { var nodeInfo = await Client.SendAsync(new DOMDescribeNodeRequest { ObjectId = handle.RemoteObject.ObjectId, }).ConfigureAwait(false); return(await AdoptBackendNodeIdAsync(nodeInfo.Node.BackendNodeId.Value, to).ConfigureAwait(false)); }