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.

July 2008 - Posts

WatiN API Reference - ContainsText

This method is used to determine if the web page has specific text being displayed.  The text to be found must be in the HTML or a label control, but cannot be located within a textbox, button or other control.

Example of Usage

 

This is the simplest usage of the method:

 

    ''' <summary>
    ''' Test behavior of ContainsText
    ''' </summary>
    ''' <remarks>
    ''' currentURL is a Private Const =
    ''' "http://localhost:3587/main.html"
    ''' </remarks>
    <Test()> _
        Public Sub TestContainsText()
 
        Using ie As IE = New IE(currentURL)
 
            Assert.IsTrue(ie.ContainsText("Contains text in DIV"), "Page did not contain expected text.")
 
        End Using
 
    End Sub

 

The page is examined for specified text.  Frequently this is used to determine the result of some other action, such as looking for a message expected to be displayed.

 

This next test demonstrates that just because it can be seen, does not mean it is “contained”, or in other words, text in some controls will not be returned in the ContainsText method.  The controls themselves must be examined.

 

    ''' <summary>
    ''' Test behavior of ContainsText
    ''' </summary>
    ''' <remarks>
    ''' currentURL is a Private Const =
    ''' "http://localhost:3587/main.html"
    ''' </remarks>
    <Test()> _
        Public Sub TestContainsText2()
 
        Using ie As IE = New IE(currentURL)
 
            ie.TextField(Find.ByName("textinput1")).Value = "TestContainsText"
            ie.TextField(Find.ById("Textarea1")).Value = "TextAreaText"
 
            Assert.IsFalse(ie.ContainsText("TestContainsText"), "Textbox found when not expected.")
            Assert.IsTrue(ie.TextField(Find.ById("name")).Value = "TestContainsText", "Text field did not contain expected text.")
 
            Assert.IsFalse(ie.ContainsText("Show allert"), "Button found when not expected.")
            Assert.IsTrue(ie.Button(Find.ById("helloid")).Text = "Show allert", "Button did not show expected text.")
 
            Assert.IsTrue(ie.ContainsText("Test label before"), "Label did not contain expected text.")
 
            Assert.IsTrue(ie.ContainsText("TextAreaText"), "Text area did not contain expected text.")
 
            Assert.IsTrue(ie.ContainsText("First Listitem"), "List item did not contain expected text.")
 
        End Using
 
    End Sub

 


This test shows how the different controls behave in relation to ContainsText calls. Controls that will return text to ContainsText include HTML controls such as TD, label controls, lists, and text areas.  Controls that will not return text to ContainsText include buttons and textboxes.

 

Contents: Table of Contents Previous Page: WatiN API Reference - Close Next Page: WatiN API Reference - DialogWatcher

 

Posted Thursday, July 31, 2008 2:45 PM by ddodgen | with no comments

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

 

Posted Monday, July 14, 2008 10:29 AM by ddodgen | with no comments

WatiN API - ClearCookies

I could not get this method to work as expected for me.

 

It is designed to clear cookies for either a specific web site or all cookies depending on whether you pass a web site URL as its parameter.  When I tried it in the below test, it failed because of no security access to clear the cookie data in IE7.  Since I did not want to modify system access because that did not seem proper for testing, I just decided that clearing cookies would probably not be that important.

 

Example of Usage

 

    ''' <summary>
    ''' Ensure that browser cookies do 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 TestClearCookies()
        Dim StringValue As String = ""
 
        Using ie As IE = New IE(currentURL)
 
 
            '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.SetCookie("http://google.com", "test=Cookie Test")
            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.")
 
            StringValue = ie.GetCookie("http://google.com", "test")
            ie.TextField(Find.ById(New Regex(MessageTextBox))).Value = StringValue
            ie.CaptureWebPageToFile("c:\temp\step3.jpg")
 
            ie.TextField(Find.ById(New Regex(MessageTextBox))).Value = "Test"
            ie.CaptureWebPageToFile("c:\temp\step4.jpg")
            ie.ClearCookies("http://google.com")
            ie.SetCookie("http://google.com", "test=")
 
            StringValue = ie.GetCookie("http://google.com", "test")
            ie.TextField(Find.ById(New Regex(MessageTextBox))).Value = StringValue
            ie.CaptureWebPageToFile("c:\temp\step5.jpg")
 
 
            Assert.IsTrue(ie.TextField(Find.ById(New Regex(MessageTextBox))).Text.Equals("test"), "Cookie data remained when should have been cleared.")
 
        End Using
 
    End Sub

 


 

This test will fail on the line with ClearCookies, because WatiN does not have access to delete cookies.  If you comment out the ClearCookies command, you can change the data manually by setting the cookie to an empty string and the test will pass.

 

Contents: Table of Contents Previous Page: WatiN API Reference - ClearCache Next Page: WatiN API Reference - Close

 

Posted Thursday, July 03, 2008 9:53 AM by ddodgen | with no comments

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

 

Posted Tuesday, July 01, 2008 11:32 AM by ddodgen | with no comments