private async Task <Response> WaitForNavigation(NavigationOptions options = null) { var mainFrame = _frameManager.MainFrame; var timeout = options?.Timeout ?? DefaultNavigationTimeout; var watcher = new NavigatorWatcher(_frameManager, mainFrame, timeout, options); var responses = new Dictionary <string, Response>(); EventHandler <ResponseCreatedEventArgs> createResponseEventListener = (object sender, ResponseCreatedEventArgs e) => responses.Add(e.Response.Url, e.Response); _networkManager.ResponseCreated += createResponseEventListener; await watcher.NavigationTask; _networkManager.ResponseCreated -= createResponseEventListener; var exception = watcher.NavigationTask.Exception; if (exception != null) { throw new NavigationException(exception.Message, exception); } return(responses.GetValueOrDefault(_frameManager.MainFrame.Url)); }
public async Task <Response> WaitForFrameNavigationAsync(Frame frame, NavigationOptions options = null) { var timeout = options?.Timeout ?? DefaultNavigationTimeout; using (var watcher = new NavigatorWatcher(_client, this, frame, _networkManager, timeout, options)) { var raceTask = await Task.WhenAny( watcher.NewDocumentNavigationTask, watcher.SameDocumentNavigationTask, watcher.TimeoutOrTerminationTask ).ConfigureAwait(false); var exception = raceTask.Exception; if (exception == null && watcher.TimeoutOrTerminationTask.IsCompleted && watcher.TimeoutOrTerminationTask.Result.IsFaulted) { exception = watcher.TimeoutOrTerminationTask.Result.Exception; } if (exception != null) { throw new NavigationException(exception.Message, exception); } return(watcher.NavigationResponse); } }
public async Task <Response> GoToAsync(string url, NavigationOptions options = null) { var referrer = _networkManager.ExtraHTTPHeaders?.GetValueOrDefault("referer"); var requests = new Dictionary <string, Request>(); EventHandler <RequestEventArgs> createRequestEventListener = (object sender, RequestEventArgs e) => { if (!requests.ContainsKey(e.Request.Url)) { requests.Add(e.Request.Url, e.Request); } }; _networkManager.RequestCreated += createRequestEventListener; var mainFrame = _frameManager.MainFrame; var timeout = options?.Timeout ?? DefaultNavigationTimeout; var watcher = new NavigatorWatcher(_frameManager, mainFrame, timeout, options); var navigateTask = Navigate(_client, url, referrer); await Task.WhenAny( navigateTask, watcher.NavigationTask ); var exception = navigateTask.Exception; if (exception == null) { await watcher.NavigationTask; exception = watcher.NavigationTask.Exception; } watcher.Cancel(); _networkManager.RequestCreated -= createRequestEventListener; if (exception != null) { throw new NavigationException(exception.Message, exception); } Request request = null; if (requests.ContainsKey(_frameManager.MainFrame.Url)) { request = requests[_frameManager.MainFrame.Url]; } return(request?.Response); }
public async Task <Response> NavigateFrameAsync(Frame frame, string url, NavigationOptions options) { var referrer = string.IsNullOrEmpty(options.Referer) ? _networkManager.ExtraHTTPHeaders?.GetValueOrDefault(MessageKeys.Referer) : options.Referer; var requests = new Dictionary <string, Request>(); var timeout = options?.Timeout ?? DefaultNavigationTimeout; using (var watcher = new NavigatorWatcher(_client, this, frame, _networkManager, timeout, options)) { var navigateTask = NavigateAsync(_client, url, referrer, frame.Id); await Task.WhenAny( watcher.TimeoutOrTerminationTask, navigateTask).ConfigureAwait(false); AggregateException exception = null; if (navigateTask.IsFaulted) { exception = navigateTask.Exception; } else { await Task.WhenAny( watcher.TimeoutOrTerminationTask, _ensureNewDocumentNavigation?watcher.NewDocumentNavigationTask : watcher.SameDocumentNavigationTask ).ConfigureAwait(false); if (watcher.TimeoutOrTerminationTask.IsCompleted && watcher.TimeoutOrTerminationTask.Result.IsFaulted) { exception = watcher.TimeoutOrTerminationTask.Result.Exception; } } if (exception != null) { throw new NavigationException(exception.InnerException.Message, exception.InnerException); } return(watcher.NavigationResponse); } }