<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Vidar's Musings &#187; web test</title>
	<atom:link href="http://www.kongsli.net/nblog/tag/web-test/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kongsli.net/nblog</link>
	<description>Deep thoughts on shallow topics</description>
	<lastBuildDate>Fri, 27 Jan 2012 07:36:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Internet Explorer automation/Watin: catching navigation error codes</title>
		<link>http://www.kongsli.net/nblog/2008/07/08/internet-explorer-automationwatin-catching-navigation-error-codes/</link>
		<comments>http://www.kongsli.net/nblog/2008/07/08/internet-explorer-automationwatin-catching-navigation-error-codes/#comments</comments>
		<pubDate>Tue, 08 Jul 2008 12:20:02 +0000</pubDate>
		<dc:creator>vidarkongsli</dc:creator>
				<category><![CDATA[Software development]]></category>
		<category><![CDATA[Software testing]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[automation]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[internet explorer]]></category>
		<category><![CDATA[internetexplorer]]></category>
		<category><![CDATA[statuscode]]></category>
		<category><![CDATA[watin]]></category>
		<category><![CDATA[web test]]></category>

		<guid isPermaLink="false">http://www.kongsli.net/nblog/?p=64</guid>
		<description><![CDATA[One question have troubled me for some time when automating Internet Explorer (actually, I am doing web testing with Watin): how to test for HTTP status codes. Finally, I figured out how to do this. The lies in an event that the InternetExplorer object raises when navigation is unsuccessful. I ended up with writing a [...]]]></description>
			<content:encoded><![CDATA[<p>One question have troubled me for some time when automating Internet Explorer (actually, I am doing web testing with Watin): how to test for HTTP status codes. Finally, I figured out how to do this. The lies in an event that the InternetExplorer object raises when navigation is unsuccessful.</p>

<p>I ended up with writing a C# helper class:
<pre>using System.Net;</pre>
<pre>using WatiN.Core;</pre>
<pre>using SHDocVw;</pre>
<pre>using Microsoft.VisualStudio.TestTools.UnitTesting;</pre>
<pre>using System.Globalization;</pre>
<pre>namespace Test</pre>
<pre>{</pre>
<pre>    public class NavigationObserver</pre>
<pre>    {</pre>
<pre>        private HttpStatusCode _statusCode;</pre>
<pre>        public NavigationObserver(IE ie)</pre>
<pre>        {</pre>
<pre>            InternetExplorer internetExplorer = (InternetExplorer)ie.InternetExplorer;</pre>
<pre>            internetExplorer.NavigateError += new DWebBrowserEvents2_NavigateErrorEventHandler(IeNavigateError);</pre>
<pre>        }</pre>
<pre>        public void ShouldHave(HttpStatusCode expectedStatusCode)</pre>
<pre>        {</pre>
<pre>            if (!_statusCode.Equals(expectedStatusCode))</pre>
<pre>            {</pre>
<pre>                Assert.Fail(string.Format(CultureInfo.InvariantCulture, "Wrong status code. Expected {0}, but was {1}",</pre>
<pre>                    expectedStatusCode, _statusCode));</pre>
<pre>            }</pre>
<pre>        }</pre>
<pre>        private void IeNavigateError(object pDisp, ref object URL, ref object Frame, ref object StatusCode, ref bool Cancel)</pre>
<pre>        {</pre>
<pre>            _statusCode = (HttpStatusCode)StatusCode;</pre>
<pre>        }</pre>
<pre>    }</pre>
<pre>}</pre>
Note that I use Visual Studio test runner to run my web tests. Then, I can use this in my test:
<pre>using (IE ie = new IE())</pre>
<pre>{</pre>
<pre>    NavigationObserver observer = new NavigationObserver(ie);</pre>
<pre>    ie.GoTo("http://some.where.com");</pre>
<pre>    observer.ShouldHave(HttpStatusCode.NotFound);</pre>
<pre>}</pre></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.kongsli.net%2Fnblog%2F2008%2F07%2F08%2Finternet-explorer-automationwatin-catching-navigation-error-codes%2F&amp;title=Internet%20Explorer%20automation%2FWatin%3A%20catching%20navigation%20error%20codes" id="wpa2a_2"><img src="http://www.kongsli.net/nblog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.kongsli.net/nblog/2008/07/08/internet-explorer-automationwatin-catching-navigation-error-codes/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Sending POST requests with Watin</title>
		<link>http://www.kongsli.net/nblog/2008/07/07/sending-post-requests-with-watin/</link>
		<comments>http://www.kongsli.net/nblog/2008/07/07/sending-post-requests-with-watin/#comments</comments>
		<pubDate>Mon, 07 Jul 2008 17:25:47 +0000</pubDate>
		<dc:creator>vidarkongsli</dc:creator>
				<category><![CDATA[Software development]]></category>
		<category><![CDATA[Software testing]]></category>
		<category><![CDATA[automation]]></category>
		<category><![CDATA[com]]></category>
		<category><![CDATA[internet explorer]]></category>
		<category><![CDATA[watin]]></category>
		<category><![CDATA[web test]]></category>

		<guid isPermaLink="false">http://www.kongsli.net/nblog/?p=63</guid>
		<description><![CDATA[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) [...]]]></description>
			<content:encoded><![CDATA[<p>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 <a href="http://support.microsoft.com/kb/313068/">this</a> article on microsoft.com, I was able to figure out how. I ended up with writing this class:
<pre style="padding-left: 30px;">public class Navigator
{
    private IE _ie;</pre></p>

<p>    public Navigator(IE ie) { _ie = ie; }</p>

<p>    public void Post(Uri baseUri, params KeyValuePair&lt;string, object&gt;[] 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();
    }</p>

<p>    private static byte[] MakeByteStreamOf(KeyValuePair&lt;string, object&gt;[] postData)
    {
        StringBuilder sb = new StringBuilder();
        if (postData.Length &gt; 0)
        {
            foreach (KeyValuePair&lt;string, object&gt; postDataEntry in postData)
            {
                sb.Append(postDataEntry.Key).Append('=').Append(postDataEntry.Value).Append('&amp;');
            }
            sb.Remove(sb.Length - 1, 1);
        }
        return ASCIIEncoding.ASCII.GetBytes(sb.ToString());
    }
}
For example, I can use it like so:
<pre>using (IE ie = new IE())</pre>
<pre>{</pre>
<pre>    Navigator navigator = new Navigator(ie);</pre>
<pre>    navigator.Post(new Uri("http://www.foo.com/"), new KeyValuePair&lt;string, object&gt;("p", 1));</pre>
<pre>    Assert.AreEqual("OK", ie.Text);</pre>
<pre>}</pre></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.kongsli.net%2Fnblog%2F2008%2F07%2F07%2Fsending-post-requests-with-watin%2F&amp;title=Sending%20POST%20requests%20with%20Watin" id="wpa2a_4"><img src="http://www.kongsli.net/nblog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.kongsli.net/nblog/2008/07/07/sending-post-requests-with-watin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

