When doing web testing using Watin, it is not trivial to be able to do a POST request to the server. However, with the help this article on microsoft.com, I was able to figure out how. I ended up with writing this class:
public class Navigator { private IE _ie; public Navigator(IE ie) { _ie = ie; } public void Post(Uri baseUri, params KeyValuePair<string, object>[] postData) { object flags = null; object targetFrame = null; object headers = "Content-Type: application/x-www-form-urlencoded" + Convert.ToChar(10) + Convert.ToChar(13); object postDataBytes = MakeByteStreamOf(postData); object resourceLocator = baseUri.ToString(); IWebBrowser2 browser = (IWebBrowser2)_ie.InternetExplorer; browser.Navigate2(ref resourceLocator, ref flags, ref targetFrame, ref postDataBytes, ref headers); _ie.WaitForComplete(); } private static byte[] MakeByteStreamOf(KeyValuePair<string, object>[] postData) { StringBuilder sb = new StringBuilder(); if (postData.Length > 0) { foreach (KeyValuePair<string, object> postDataEntry in postData) { sb.Append(postDataEntry.Key).Append('=').Append(postDataEntry.Value).Append('&'); } sb.Remove(sb.Length - 1, 1); } return ASCIIEncoding.ASCII.GetBytes(sb.ToString()); } }
For example, I can use it like so:
using (IE ie = new IE())
{
Navigator navigator = new Navigator(ie);
navigator.Post(new Uri("http://www.foo.com/"), new KeyValuePair<string, object>("p", 1));
Assert.AreEqual("OK", ie.Text);
}