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

This method contains some gotchas.  From its method name you would think that it would clear out the system cache, at least for the web page currently being viewed, but this is a bit of a misconception.

 

The documentation on the WatiN site indicates that this method is used to clear the browser cache and gives an example of clearing the cache and then going back to a page that requires a login.  Presumably you would have to log in again after the cache was cleared.  The documentation also indicates that this may be a problem because Internet Explorer tends to keep things in memory and will ignore this method.  It is suggested that the developer employ ie.ReOpen to force the browser to close and reopen, then navigate back to their desired web page to manually clear any cache at the browser level.

 

The gotcha is really with the system cache, however.  If the web page saves information to the cache maintained on the server and then reads the value back out later, neither ClearCache nor ReOpen, nor anything else that I can find, will clear these cache values before they are set to expire by IIS.

 

The following test demonstrates this behavior.  I modified the earlier created actions for the Default.aspx page to have the click event on the OK button to save information into cache and then the page load retrieves this information, if it exists and places it into the textbox for verification of the cache activity.

Example of Usage

 

The Default.aspx page used for our tests should now appear like this:

 

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WatiNTest._Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="OKButton" runat="server" Text="OK" />
        <asp:Button ID="CancelButton" runat="server" Text="Cancel" />
        <asp:TextBox ID="MessageTextBox" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox></div>
    </form>
</body>
</html>

 

 

The Default.aspx.vb code behind should appear like this:

 

Partial Public Class _Default
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim aValue As String = ""
 
        If Not IsPostBack Then
            MessageTextBox.Text = ""
            MessageTextBox.Focus()
 
            aValue = Cache.Get("TextMessage")
 
            If Not aValue Is Nothing Then
                MessageTextBox.Text = aValue & " from cache"
                ListBox1.Items.Add(aValue & " from cache")
            Else
                MessageTextBox.Text = ""
                ListBox1.Items.Add("Nothing in cache")
            End If
        End If
    End Sub
 
    Protected Sub CancelButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CancelButton.Click
        MessageTextBox.Text = "Cancel"
    End Sub
 
    Protected Sub OKButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OKButton.Click
        MessageTextBox.Text = OKButton.Text & " clicked"
        ListBox1.Items.Add(OKButton.Text & " clicked")
        Cache.Add("TextMessage", OKButton.Text, Nothing, DateAdd(DateInterval.Hour, 1, Now), TimeSpan.Zero, CacheItemPriority.Normal, Nothing)
    End Sub
 
End Class

 


 

Now the test I created looks like this:

 

    ''' <summary>
    ''' Ensure that browser cache does not affect
    ''' the results of clicking the OK Button.
    ''' </summary>
    ''' <remarks>
    ''' currentURL is a Private Const =
    ''' "http://localhost:3587/Default.aspx"
    ''' OKButton is a Private Const = "OKButton"
    ''' MessageTextBox is a Private Const = "MessageTextBox"
    ''' </remarks>
    <Test()> _
        Public Sub TestClearCache()
        Using ie As IE = New IE(currentURL)
 
            ie.ClearCache()
 
            'Method ONE - Fully specify find criteria
            ie.Button(Find.ById(New Regex(OKButton))).Click()
            Assert.IsTrue(ie.TextField(Find.ById(New Regex(MessageTextBox))).Text.Contains("OK"), "TestButtons did not find OK in the textbox after OK button clicked.")
            ie.CaptureWebPageToFile("c:\temp\step1.jpg")
 
            ie.ClearCache()
            ie.TextField(Find.ById(New Regex(MessageTextBox))).Value = "Test"
            ie.CaptureWebPageToFile("c:\temp\step2.jpg")
            Assert.IsTrue(ie.TextField(Find.ById(New Regex(MessageTextBox))).Text.Equals("Test"), "Textfield did not reset.")
 
            ie.ClearCache()
            ie.GoTo(currentURL)
            ie.CaptureWebPageToFile("c:\temp\step3.jpg")
 
            ie.Refresh()
            ie.CaptureWebPageToFile("c:\temp\step4.jpg")
            ie.ClearCache()
            ie.GoTo(currentURL)
            ie.CaptureWebPageToFile("c:\temp\step5.jpg")
 
            ie.GoTo("http://live.com")
            ie.CaptureWebPageToFile("c:\temp\step6.jpg")
 
            ie.ClearCache()
            ie.GoTo(currentURL)
            ie.CaptureWebPageToFile("c:\temp\step7.jpg")
 
            ie.ClearCache()
            ie.Reopen()
            ie.ClearCache()
            ie.GoTo(currentURL)
            ie.CaptureWebPageToFile("c:\temp\step8.jpg")
 
 
            Assert.IsFalse(ie.TextField(Find.ById(New Regex(MessageTextBox))).Text.Contains("cache"), "Cache data remained when should have been cleared.")
 
        End Using
 
    End Sub

 


As it stands, this test will fail because the text field ends up still loading its value from the cache.  I left the test this way to highlight that the system cache is never cleared by the ClearCache method.  To make the test run nicely with its siblings, you should change the assert to IsTrue rather than IsFalse, since that is the real expected result.

 

This test tries several methods to get the cache to drop the information that the OK button has been clicked.  It leaves the page and returns, refreshes the page, even closes the browser and reopens it, navigating back to the default page again, all to no avail.  The system cache continues to retain its value and will consistently fill the textbox with “OK from cache” until the cache is set to expire (which should be one hour from when it was clicked).

 

Contents: Table of Contents Previous Page: WatiN API Reference - CheckBoxes Next Page: working on it

 

Published Tuesday, July 01, 2008 11:32 AM by ddodgen

Comments

No Comments