private void ExtractFormAndHiddenControls (Response response, string formId) { HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument (); htmlDoc.LoadHtml (response.Body); StringBuilder tempxml = new StringBuilder (); StringWriter tsw = new StringWriter (tempxml); htmlDoc.OptionOutputAsXml = true; htmlDoc.Save (tsw); XmlDocument doc = new XmlDocument (); doc.LoadXml (tempxml.ToString ()); const string HTML_NAMESPACE = "http://www.w3.org/1999/xhtml"; XmlNamespaceManager nsmgr = new XmlNamespaceManager (doc.NameTable); nsmgr.AddNamespace ("html", HTML_NAMESPACE); #if USE_CORRECT_FORMID XmlNode formNode = doc.SelectSingleNode ("//html:form[@name='" + formId + "']", nsmgr); #else XmlNode formNode = doc.SelectSingleNode ("//html:form", nsmgr); #endif if (formNode == null) throw new ArgumentException ("Form with id='" + formId + "' was not found in document: " + response.Body); string actionUrl = formNode.Attributes["action"].Value; if (actionUrl != null && actionUrl != string.Empty) base.Url = actionUrl; XmlNode method = formNode.Attributes["method"]; if (method != null && "POST" == method.Value.ToUpper(CultureInfo.InvariantCulture)) base.IsPost = true; else base.IsPost = false; #if USE_CORRECT_FORMID foreach (XmlNode inputNode in formNode.SelectNodes ("//html:input", nsmgr)) #else foreach (XmlNode inputNode in doc.SelectNodes ("//html:input[@type='hidden']", nsmgr)) #endif { BaseControl bc = new BaseControl (); bc.Name = inputNode.Attributes["name"].Value; if (bc.Name == null || bc.Name == string.Empty) continue; if (inputNode.Attributes["value"] != null) bc.Value = inputNode.Attributes["value"].Value; else bc.Value = ""; Controls[bc.Name] = bc; } }
private void CopyFrom (WebTest newTestInstance) { this._invoker = newTestInstance._invoker; this._request = newTestInstance._request; this._response = newTestInstance._response; this._userData = newTestInstance._userData; }
/// <summary> /// Create <see cref="FormRequest"/> instance from the given /// <paramref name="response">response</paramref> extracting /// form attributes and hidden controls from the form element /// with given id. /// </summary> /// <param name="response">The response to extract values from.</param> /// <param name="formId">The id of the form to use.</param> /// <remarks>Currently, the <paramref name="formId"/> is ignored, and the /// first form is used.</remarks> public FormRequest (Response response, string formId) { _controls = new BaseControlCollection (); ExtractFormAndHiddenControls (response, formId); }
string[] ExtractFormAndHiddenControls (Response response) { HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument (); htmlDoc.LoadHtml (response.Body); var tempxml = new StringBuilder (); var tsw = new StringWriter (tempxml); htmlDoc.OptionOutputAsXml = true; htmlDoc.Save (tsw); var doc = new XmlDocument (); doc.LoadXml (tempxml.ToString ()); XmlNamespaceManager nsmgr = new XmlNamespaceManager (doc.NameTable); nsmgr.AddNamespace ("html", HTML_NAMESPACE); XmlNode formNode = doc.SelectSingleNode ("//html:form", nsmgr); if (formNode == null) throw new ArgumentException ("Form was not found in document: " + response.Body); string actionUrl = formNode.Attributes ["action"].Value; XmlNode method = formNode.Attributes ["method"]; var data = new List <string> (); string name, value; foreach (XmlNode inputNode in doc.SelectNodes ("//html:input[@type='hidden']", nsmgr)) { name = inputNode.Attributes["name"].Value; if (String.IsNullOrEmpty (name)) continue; XmlAttribute attr = inputNode.Attributes["value"]; if (attr != null) value = attr.Value; else value = String.Empty; data.Add (name); data.Add (value); } return data.ToArray (); }