public async Task <HttpResponseMessage> PostBackWithHtmlForm(string pageUri, CssSelectorForDom selector, Dictionary <string, string> postFormValue, string expectedRedirectedPage = null) { var response = await Client.GetAsync($"{RootUri}/{pageUri}"); var content = await HtmlHelper.GetDocumentAsync(response); IHtmlFormElement form = (IHtmlFormElement)content.QuerySelector(selector.Form); IHtmlButtonElement btnSubmit = (IHtmlButtonElement)content.QuerySelector(selector.SubmitButton); //Act HttpResponseMessage result = await Client.SendAsync(form, btnSubmit, postFormValue); LastResponse = result; //Assert string requestUri = result.RequestMessage.RequestUri.ToString(); if (expectedRedirectedPage != null) { string expectedUri = (RootUri + '/' + expectedRedirectedPage).ToLower(); if (expectedUri != requestUri.ToLower()) { throw new Exception($"Unexpected rediction of page: {requestUri}"); } } if (response.StatusCode != System.Net.HttpStatusCode.OK) { throw new HttpRequestException(await response.Content.ReadAsStringAsync()); } /*Success might not be determined correctly * due to Server still response Http status 200 OK * but use JavaScript or Dom UI element to indicate error instead. */ return(result); }
public static async Task <IHtmlDocument> RequestHtmlDocumentWithPostAsync(IHtmlFormElement form, IHtmlButtonElement submitBtn) { var factory = new WebApplicationFactory <Startup>(); var client = factory.CreateClient(); var response = await client.SendAsync(form, submitBtn); var document = await HtmlHelpers.GetDocumentAsync(response); return(document); }
private async Task <(HttpResponseMessage response, IHtmlDocument _document)> Post(IHtmlFormElement form, IHtmlButtonElement btn) { var response = await _client.SendAsync(form, btn); if (response.StatusCode == HttpStatusCode.BadRequest) { return(response, null); } var document = await HtmlHelpers.GetDocumentAsync(response); return(response, document); }