示例#1
0
 /// <summary>
 /// Releases the browser described by webRequestContext.
 /// </summary>
 /// <param name="webRequestContext">
 /// The web request context.
 /// </param>
 public static void ReleaseBrowser(WebRequestContext webRequestContext)
 {
     if (webRequestContext.Browser != null)
     {
         ReleaseBrowser(webRequestContext.Browser);
         webRequestContext.Browser = null;
     }
 }
示例#2
0
        /// <inheritdoc />
        public WebRequestContext SendRequest(
            ContextSendRequestParameter contextSendRequestParameter)
        {
            // create web request context to be returned
            var webRequestContext = new WebRequestContext();

            // create uri
            Uri uri = CreateUri(contextSendRequestParameter.Url);

            // execute a web request
            BrowserNavigateToPage(contextSendRequestParameter, uri, webRequestContext);

            return(webRequestContext);
        }
示例#3
0
        /// <summary>
        /// Browser navigate to page.
        /// </summary>
        /// <param name="contextSendRequestParameter">
        /// The context send request parameter.
        /// </param>
        /// <param name="uri">
        /// The uri.
        /// </param>
        /// <param name="webRequestContext">
        /// The web request context.
        /// </param>
        private void BrowserNavigateToPage(
            ContextSendRequestParameter contextSendRequestParameter,
            Uri uri,
            WebRequestContext webRequestContext)
        {
            webRequestContext.ResponseHolder = new HttpWebResponseHolder();
            BrowserAbstract browser = null;

            try
            {
                var browserManager = ObjectResolver.Resolve <IBrowserManager>();

                // there is no browser pool created for this browser type
                if (browserManager.AvailableBrowserTypes.All(t => t != contextSendRequestParameter.BrowserType))
                {
                    return;
                }

                // try to acquire a browser if we don't have one already
                browser = webRequestContext.Browser ??
                          browserManager.AcquireBrowser(contextSendRequestParameter.BrowserType);

                if (browser == null)
                {
                    Library.Logger.Logger.WriteWarning(
                        "Browser pool failed allocation (measured by BrowserPoolCounters.FailedAllocationsPerMinute).");

                    return;
                }

                // in case we acquired then set this in the request context
                webRequestContext.Browser = browser;

                // Add request cookies.
                if (this.CurrentCookies.ContainsKey(uri.Host))
                {
                    foreach (Cookie cookie in this.CurrentCookies[uri.Host])
                    {
                        browser.AddCookie(cookie.Name, cookie.Value, cookie.Domain, cookie.Path, cookie.Expires);
                    }
                }

                var stopWatch = new Stopwatch();
                stopWatch.Start();

                browser.NavigateTo(uri.OriginalString);

                stopWatch.Stop();

                // FireFox does not block on the presence of an alert dialog, so we manually check here.
                string alertText;
                browser.DismissedIfAlertDisplayed(out alertText);

                browser.WaitForPageLoad(Constants.BrowserWaitForPageLoadInMilliseconds);

                // check for messages again following page load - do this before querying other browser properties
                browser.DismissedIfAlertDisplayed(out alertText);

                var fiddlerResponseSessionKey = Library.Constants.FiddlerResponseSessionKey.FormatIc(
                    browser.ProcessId,
                    browser.Url);

                // get fiddler response
                var fiddlerResponse = FiddlerProxy.ResponseSession.ContainsKey(fiddlerResponseSessionKey)
                    ? FiddlerProxy.ResponseSession[fiddlerResponseSessionKey].oResponse
                    : null;

                var fiddlerRequest = FiddlerProxy.ResponseSession.ContainsKey(fiddlerResponseSessionKey)
                    ? FiddlerProxy.ResponseSession[fiddlerResponseSessionKey].oRequest
                    : null;

                // populate response holder
                webRequestContext.ResponseHolder.StatusCode = fiddlerResponse != null
                    ? (HttpStatusCode)fiddlerResponse.headers.HTTPResponseCode
                    : HttpStatusCode.OK;

                var source = browser.PageSource;
                webRequestContext.ResponseHolder.ResponseContent = source;

                webRequestContext.ResponseHolder.RequestAbsolutUri = uri.AbsoluteUri;
                webRequestContext.ResponseHolder.RequestHost       = uri.Host;

                Uri resultUri;
                if (Uri.TryCreate(browser.Url, UriKind.Absolute, out resultUri))
                {
                    webRequestContext.ResponseHolder.ResponseUri = resultUri;
                }

                webRequestContext.ResponseHolder.ResponseDateTime = DateTime.UtcNow;
                webRequestContext.ResponseHolder.RequestUserAgent =
                    contextSendRequestParameter.BrowserType.ToString();
                webRequestContext.ResponseHolder.BrowserPageTitle    = browser.PageTitle;
                webRequestContext.ResponseHolder.ResponseContentType = "text/html";
                webRequestContext.ResponseHolder.Latency             = new TimeSpan(0, 0, 0, 0, (int)stopWatch.Elapsed.TotalMilliseconds);

                // set headers from fiddler for this request
                if (fiddlerRequest != null)
                {
                    webRequestContext.ResponseHolder.RequestHeaders =
                        Regex.Replace(fiddlerRequest.headers.ToString().Trim(), @"[\u0000-\u001F]", string.Empty);
                }

                // set headers from fiddler for this response
                if (fiddlerResponse != null)
                {
                    webRequestContext.ResponseHolder.SetResponseFiddlerHeaders(fiddlerResponse.headers);
                }

                // set back response cookies
                webRequestContext.ResponseHolder.Cookies = browser.AllCookies;
            }
            catch (Exception exception)
            {
                webRequestContext.ResponseHolder.HttpError = exception.Message;
                CheckForUnexpectedDialog(uri, browser, exception);
            }
        }