/// <summary>
        /// Opens a new document loaded from a virtual response that can be
        /// filled via the provided callback.
        /// </summary>
        /// <param name="context">The browsing context to use.</param>
        /// <param name="request">Callback with the response to setup.</param>
        /// <param name="cancel">The cancellation token.</param>
        /// <returns>The task that creates the document.</returns>
        public static Task <IDocument> OpenAsync(this IBrowsingContext context, Action <VirtualResponse> request, CancellationToken cancel)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            if (context == null)
            {
                context = BrowsingContext.New();
            }

            using (var response = new VirtualResponse())
            {
                request(response);
                var contentType = response.GetContentType(MimeTypes.Html);
                var source      = response.CreateSourceFor(context.Configuration);
                return(context.LoadDocumentAsync(response, contentType, source, cancel));
            }
        }
Exemple #2
0
        /// <summary>
        /// Opens a new document loaded from a virtual response that can be
        /// filled via the provided callback.
        /// </summary>
        /// <param name="context">The browsing context to use.</param>
        /// <param name="request">Callback with the response to setup.</param>
        /// <param name="cancel">The cancellation token.</param>
        /// <returns>The task that creates the document.</returns>
        public static async Task <IDocument> OpenAsync(this IBrowsingContext context, Action <VirtualResponse> request, CancellationToken cancel)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            if (context == null)
            {
                context = BrowsingContext.New();
            }

            using (var response = new VirtualResponse())
            {
                request(response);
                var source   = response.CreateSourceFor(context.Configuration);
                var document = await context.LoadDocumentAsync(response, source, cancel).ConfigureAwait(false);

                context.NavigateTo(document);
                return(document);
            }
        }
 /// <summary>
 /// Opens a new document loaded from the specified request
 /// asynchronously in the given context.
 /// </summary>
 /// <param name="context">The browsing context to use.</param>
 /// <param name="request">The request to issue.</param>
 /// <param name="cancel">The cancellation token.</param>
 /// <returns>The task that creates the document.</returns>
 public static Task <IDocument> OpenAsync(this IBrowsingContext context, DocumentRequest request, CancellationToken cancel = default)
 {
     request = request ?? throw new ArgumentNullException(nameof(request));
     context = context ?? BrowsingContext.New();
     return(context.NavigateToAsync(request));
 }