TestDriven.Me

I hope to spend some time discussing testing with VB.NET. I don't think of myself as an expert, but I have had some experience and certainly some opinions. I will try to share some of those opinions along with some information and hopefully generate some discussion so that we all can improve our skills a bit.
WatiN API Reference - Close

This method is used to close the current instance of the browser.  If you want to leave the browser open at the end of the test, you can omit this statement.  This will allow you to examine the contents of the browser, but you will have to close it yourself.  If you are running more than one test at a time, this can be somewhat aggravating, and fortunately is unnecessary just to see what the browser contains, since you can always capture a screen shot instead.

Example of Usage

 

    ''' <summary>
    ''' Determine if the current instance of IE will close properly.
    ''' </summary>
    ''' <remarks>
    ''' currentURL is a Private Const =
    ''' "http://localhost:3587/Default.aspx"
    ''' OKButton is a Private Const = "OKButton"
    ''' </remarks>
    <Test()> _
    Public Sub TestClose()
        Dim ErrorFlag As Boolean = False
 
        Try
            Dim ie As IE = New IE(currentURL)
 
            Assert.IsTrue(ie.Button(New Regex(OKButton)).Text = "OK", "The page did not contain expected text.")
            ie.Close()
 
            'This should produce an error after IE instance is closed.
            Assert.IsTrue(ie.Button(New Regex(OKButton)).Text = "OK", "Error was not caused when closed.")
 
        Catch ex As Exception
            ErrorFlag = True
            Assert.IsTrue(ex.Message.Contains("Could not find"), "Did not produce the expected error.")
        End Try
 
        Assert.IsTrue(ErrorFlag, "Error was not caused after close.")
 
        Using ie As IE = New IE(currentURL)
            Assert.IsTrue(ie.Button(New Regex(OKButton)).Text = "OK", "The page did not contain expected button with using.")
        End Using
 
    End Sub

 


 

This test shows that the Close statement disposes of the current instance of the browser, because the next statement that uses the IE instance causes an error to be thrown.  This could also have been checked for by using the attribute on the test for expected error, but I wanted to do some other processing afterwards, which would not have been possible using the attribute.

 

Note:  Deliberately causing an exception so you can catch it and examine it, is normally very bad practice.  You will notice how long this test takes to run as Visual Studio tries gamely to find the IE instance.  I only did it here to demonstrate that nothing can be accessed from the IE instance after it is closed.

 

An even more important item to note in this test is the second stage, where the Using statement is employed.  (Yes it was hard not to say “used” here.)  Using has several advantages over Dim; it ensures the proper disposal of resources, closed is not even needed, less typing, clearer syntax.  You should always utilize the Using syntax when system resources are involved, such as data objects or files, to ensure proper disposal when no longer needed.

Contents: Table of Contents Previous Page: WatiN API Reference - ClearCookies  Next Page: WatiN API Reference - ContainsText

 

Published Monday, July 14, 2008 10:29 AM by ddodgen

Comments

No Comments