/// <summary> /// Asynchronously starts the session. /// </summary> /// <param name="protocolVersion">The version of the protocol to use in communicating with the browswer.</param> /// <returns>A task that represents the asynchronous operation.</returns> internal async Task Start(int protocolVersion) { string debuggerUrl = string.Format(CultureInfo.InvariantCulture, "http://{0}", this.debuggerEndpoint); string rawVersionInfo = string.Empty; using (HttpClient client = new HttpClient()) { client.BaseAddress = new Uri(debuggerUrl); rawVersionInfo = await client.GetStringAsync("/json/version"); } var versionInfo = JsonConvert.DeserializeObject <DevToolsVersionInfo>(rawVersionInfo); websocketAddress = versionInfo.WebSocketDebuggerUrl; if (protocolVersion == AutoDetectDevToolsProtocolVersion) { bool versionParsed = int.TryParse(versionInfo.BrowserMajorVersion, out protocolVersion); if (!versionParsed) { throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, "Unable to parse version number received from browser. Reported browser version string is '{0}'", versionInfo.Browser)); } } this.domains = DevToolsDomains.InitializeDomains(protocolVersion, this); string targetId = null; var targets = await this.domains.Target.GetTargets(); foreach (var target in targets) { if (target.Type == "page") { targetId = target.TargetId; LogTrace("Found Target ID {0}.", targetId); string sessionId = await this.domains.Target.AttachToTarget(targetId); LogTrace("Target ID {0} attached. Active session ID: {1}", targetId, sessionId); this.ActiveSessionId = sessionId; break; } } await this.domains.Target.SetAutoAttach(); LogTrace("AutoAttach is set.", targetId); try { // Wrap this in a try-catch, because it's not the end of the // world if clearing the log doesn't work. await this.domains.Log.Clear(); LogTrace("Log cleared.", targetId); } catch (WebDriverException) { } }
/// <summary> /// Initializes the supplied DevTools session's domains for the specified browser version within the specified number of versions. /// </summary> /// <param name="protocolVersion">The version of the DevTools Protocol to use.</param> /// <param name="session">The <see cref="DevToolsSession"/> for which to initialiize the domains.</param> /// <param name="versionRange">The range of versions within which to match the provided version number. Defaults to 5 versions.</param> /// <returns>The <see cref="DevToolsDomains"/> object containing the version-specific domains.</returns> public static DevToolsDomains InitializeDomains(int protocolVersion, DevToolsSession session, int versionRange) { if (versionRange < 0) { throw new ArgumentException("Version range must be positive", nameof(versionRange)); } DevToolsDomains domains = null; Type domainType = MatchDomainsVersion(protocolVersion, versionRange); ConstructorInfo constructor = domainType.GetConstructor(new Type[] { typeof(DevToolsSession) }); if (constructor != null) { domains = constructor.Invoke(new object[] { session }) as DevToolsDomains; } return(domains); }
/// <summary> /// Asynchronously starts the session. /// </summary> /// <returns>A task that represents the asynchronous operation.</returns> public async Task Start() { string debuggerUrl = string.Format(CultureInfo.InvariantCulture, "http://{0}", this.debuggerEndpoint); string rawVersionInfo = string.Empty; using (HttpClient client = new HttpClient()) { client.BaseAddress = new Uri(debuggerUrl); rawVersionInfo = await client.GetStringAsync("/json/version"); } var versionInfo = JsonConvert.DeserializeObject <DevToolsVersionInfo>(rawVersionInfo); websocketAddress = versionInfo.WebSocketDebuggerUrl; this.domains = DevToolsDomains.InitializeDomains(versionInfo, this); string targetId = null; var targets = await this.domains.Target.GetTargets(); foreach (var target in targets) { if (target.Type == "page") { targetId = target.TargetId; LogTrace("Found Target ID {0}.", targetId); string sessionId = await this.domains.Target.AttachToTarget(targetId); LogTrace("Target ID {0} attached. Active session ID: {1}", targetId, sessionId); this.ActiveSessionId = sessionId; break; } } await this.domains.Target.SetAutoAttach(); LogTrace("AutoAttach is set.", targetId); await this.domains.Log.Clear(); LogTrace("Log cleared.", targetId); }
/// <summary> /// Asynchronously starts the session. /// </summary> /// <param name="requestedProtocolVersion">The requested version of the protocol to use in communicating with the browswer.</param> /// <returns>A task that represents the asynchronous operation.</returns> internal async Task StartSession(int requestedProtocolVersion) { int protocolVersion = await InitializeProtocol(requestedProtocolVersion); this.domains = DevToolsDomains.InitializeDomains(protocolVersion, this); await this.InitializeSocketConnection(); await this.InitializeSession(); try { // Wrap this in a try-catch, because it's not the end of the // world if clearing the log doesn't work. await this.domains.Log.Clear(); LogTrace("Log cleared.", this.attachedTargetId); } catch (WebDriverException) { } }
/// <summary> /// Initializes the supplied DevTools session's domains for the specified browser version within the specified number of versions. /// </summary> /// <param name="versionInfo">The <see cref="DevToolsVersionInfo"/> object containing the browser version information.</param> /// <param name="session">The <see cref="DevToolsSession"/> for which to initialiize the domains.</param> /// <param name="versionRange">The range of versions within which to match the provided version number. Defaults to 5 versions.</param> /// <returns>The <see cref="DevToolsDomains"/> object containing the version-specific domains.</returns> public static DevToolsDomains InitializeDomains(DevToolsVersionInfo versionInfo, DevToolsSession session, int versionRange) { if (versionRange < 0) { throw new ArgumentException("Version range must be positive", "versionRange"); } DevToolsDomains domains = null; int browserMajorVersion = 0; bool versionParsed = int.TryParse(versionInfo.BrowserMajorVersion, out browserMajorVersion); if (versionParsed) { Type domainType = MatchDomainsVersion(browserMajorVersion, versionRange); ConstructorInfo constructor = domainType.GetConstructor(new Type[] { typeof(DevToolsSession) }); if (constructor != null) { domains = constructor.Invoke(new object[] { session }) as DevToolsDomains; } } return(domains); }