/// <summary> /// Uses the browser to execute a GET request to the specified location, validates against the specified HTTP status /// code and creates a page using the specified factory. /// </summary> /// <typeparam name="T"> /// The type of page to return. /// </typeparam> /// <param name="browser"> /// The browser. /// </param> /// <param name="location"> /// The location to request. /// </param> /// <param name="expectedStatusCode"> /// The expected HTTP status code. /// </param> /// <param name="pageFactory"> /// The page factory. /// </param> /// <returns> /// A <typeparamref name="T" /> value. /// </returns> /// <exception cref="System.ArgumentNullException"> /// The <paramref name="browser" /> parameter is <c>null</c>. /// </exception> /// <exception cref="System.ArgumentNullException"> /// The <paramref name="location" /> parameter is <c>null</c>. /// </exception> /// <exception cref="System.ArgumentNullException"> /// The <paramref name="pageFactory" /> parameter is <c>null</c>. /// </exception> public static T GoTo <T>( this IBrowser browser, Uri location, HttpStatusCode expectedStatusCode, IPageFactory pageFactory) where T : IPage, new() { if (browser == null) { throw new ArgumentNullException("browser"); } if (location == null) { throw new ArgumentNullException("location"); } if (pageFactory == null) { throw new ArgumentNullException("pageFactory"); } using (var request = new HttpRequestMessage(HttpMethod.Get, location)) { return(browser.Execute <T>(request, expectedStatusCode, pageFactory)); } }
/// <summary> /// Uses the browser to execute a POST request with the specified parameters to the specified location, validates /// against the specified HTTP status code and creates a page using the specified factory. /// </summary> /// <typeparam name="T"> /// The type of page to return. /// </typeparam> /// <param name="browser"> /// The browser. /// </param> /// <param name="parameters"> /// The post parameters. /// </param> /// <param name="location"> /// The location to post to. /// </param> /// <param name="expectedStatusCode"> /// The expected HTTP status code. /// </param> /// <param name="pageFactory"> /// The page factory. /// </param> /// <returns> /// A <typeparamref name="T" /> value. /// </returns> /// <exception cref="System.ArgumentNullException"> /// The <paramref name="browser" /> parameter is <c>null</c>. /// </exception> /// <exception cref="System.ArgumentNullException"> /// The <paramref name="parameters" /> parameter is <c>null</c>. /// </exception> /// <exception cref="System.ArgumentNullException"> /// The <paramref name="location" /> parameter is <c>null</c>. /// </exception> public static T PostTo <T>( this IBrowser browser, IEnumerable <PostEntry> parameters, Uri location, HttpStatusCode expectedStatusCode, IPageFactory pageFactory) where T : IPage, new() { if (browser == null) { throw new ArgumentNullException("browser"); } if (parameters == null) { throw new ArgumentNullException("parameters"); } if (location == null) { throw new ArgumentNullException("location"); } if (pageFactory == null) { throw new ArgumentNullException("pageFactory"); } var parameterSet = parameters.ToList(); try { using (var request = new HttpRequestMessage(HttpMethod.Post, location)) { using (var multiPart = BuildPostContent(parameterSet)) { request.Content = multiPart; return(browser.Execute <T>(request, expectedStatusCode, pageFactory)); } } } finally { // Ensure that any stream based file post entries are disposed var streamEntries = parameterSet.OfType <PostFileStreamEntry>(); foreach (var streamEntry in streamEntries) { streamEntry.Dispose(); } } }