This is a very useful method for documentation and reviewing test results. Its purpose is to save a screen shot of the web page in its current state as an image file for later review. The type of image file saved is based on the file extension passed in the parameter. Currently file formats supported are: BMP, GIF, JPG, PNG, TIF. WatiN depends on the image encoders built into Windows, so if a particular encoder is missing, it will save the file in another image format.
The title bar and address bar are not included in the screen capture, just the browser window. Using this method liberally throughout your tests, it is easy to spot when things start to go wrong in a test with more than a few lines. Some developers like to capture a screen shot before every assert to assist in determining the failure when one occurs. This may be overkill, but when running more than a hundred tests, it is reassuring to know that the state of the screens are all being preserved for later review in case one fails.
Example of Usage
The following test is a reuse of part of the test created for ActiveElement, with the addition of the screen capture.
''' <summary> ''' Determine if the input textbox is initially the ActiveElement ''' and capture a screen shot. ''' </summary> ''' <remarks> ''' currentURL is a Private Const = ''' "http://localhost:3587/Default.aspx" ''' MessageTextBox is a Private Const = "MessageTextBox" ''' </remarks> <Test()> _ Public Sub TestActiveElementInitialFocus() Dim aStringValue As String = "" Using ie As IE = New IE(currentURL) If Not ie.ActiveElement.Id Is Nothing Then aStringValue = ie.ActiveElement.Id.ToString End If ie.CaptureWebPageToFile("C:\Doug\Default\TestActiveElementInitialFocus.jpg") Assert.IsTrue(aStringValue.Contains(MessageTextBox), "The initial focus should have been the textbox but instead was [" & aStringValue & "].") End Using End Sub
As shown in this test, it is a good idea to capture a screen shot before the assert is called, because if the assert fails, it is too late to do so.
A standard that some developers follow is to provide a folder for each test class (in this case the Default.aspx page is being tested) and then name the pictures after the test method with sequential numbers used to capture multiple images in the same test if there are several actions being tested.
Contents: Table of Contents Previous Page: WatiN API Reference - Buttons Next Page: WatiN API Reference - CheckBox