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 - Dispose

 

This method is used to release resources no longer needed by the current instance of the browser.  Normally, you can omit this statement since it is called internally by the IE instance when it closes itself.

 

Once Dispose is called, the IE instance is no longer valid and cannot be used.  The only valid method to call on the IE instance after Dispose is Close.

Example of Usage


    ''' <summary>
    ''' Determine dispose will affect the current instance of IE.
    ''' </summary>
    ''' <remarks>
    ''' currentURL is a Private Const =
    ''' "http://localhost:3587/Default.aspx"
    ''' OKButton is a Private Const = "OKButton"
    ''' </remarks>
    <Test()> _
    Public Sub TestDispose()
        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.Dispose()
 
            'This should produce an error after IE instance is closed.
            Assert.IsTrue(ie.Button(New Regex(OKButton)).Text = "OK", "Error was not caused when disposed.")
 
        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 dispose.")
 
        ErrorFlag = False
        Try
            Dim ie As IE = New IE(currentURL)
 
            Assert.IsTrue(ie.Button(New Regex(OKButton)).Text = "OK", "The second page did not contain expected text.")
            ie.Dispose()
            ie.Close()
 
        Catch ex As Exception
            ErrorFlag = True
            Assert.IsTrue(ex.Message.Contains("Could not find"), "Close should not produce an error.")
        End Try
 
        Assert.IsFalse(ErrorFlag, "Error was caused by close after dispose.")
 
        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 Dispose statement effectively closes 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 final stage, where the Using statement is employed.  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 - DialogWatcher Next Page: WatiN API Reference - Div

 

Published Wednesday, August 20, 2008 2:16 PM by ddodgen

Comments

No Comments