August 2008 - Posts
This method is used to make visible elements contained within a DIV tag on a web page. It contains a host of properties to help manipulate the information therein.
Example of Usage
This example utilizes the main.html page that is distributed with WatiN to highlight some of the methods available with the DIV class.
''' <summary>
''' Test behavior of DIV
''' </summary>
''' <remarks>
''' currentURL is a Private Const =
''' "http://localhost:3587/main.html"
''' </remarks>
<Test()> _
Public Sub TestDiv()
Dim aDIV As Div
Dim aBoolean As Boolean = False
Using ie As IE = New IE(currentURL)
aDIV = ie.Div(Find.ById("divid"))
Assert.IsTrue(aDIV.Text.Contains("Contains text in DIV"), "Div did not contain expected text.")
Assert.IsTrue(aDIV.Tables.Length > 0, "Div did not contain a table.")
Assert.IsTrue(aDIV.Tables(0).TableRows.Length > 0, "Div table had no rows.")
Assert.IsTrue(aDIV.Tables(0).TableRows.Length = 2, "Div table did not have 2 rows.")
aString = aDIV.Tables(0).TableRows(0).TableCells(0).Text
Assert.IsTrue(aString = "Contains text in DIV", "Cell did not contain expected text.")
Assert.IsTrue(aDIV.Tables(0).TableRows(1).Text.Contains("Test label before:"), "Text not found in table row.")
Assert.IsTrue(aDIV.Tables(0).TableRows(1).TableCells(0).Text.Contains("Test label before:"), "Text not found in table cell.")
aBoolean = aDIV.Tables(0).TableRows(1).TableCells(0).CheckBoxes(0).Checked
aDIV.Tables(0).TableRows(1).TableCells(0).CheckBoxes(0).Checked = Not aBoolean
Assert.IsFalse(aBoolean = aDIV.Tables(0).TableRows(1).TableCells(0).CheckBoxes(0).Checked, "Checkbox not changed.")
End Using
End Sub
This test is pretty self-explanatory and requires little additional comment. Once the DIV is located and placed within a local variable (not required, but makes the syntax shorter on subsequent usage), the various elements within the DIV are proven to exist. The value of the checkbox is changed at the end of the test to demonstrate that this is possible.
The primary use of the DIV is to work with elements that cannot be located via the normal means available to the individual controls. For example: The text contained in the first row is not within an ASP.NET control, so it can only be examined by digging down into the table.
Contents: Table of Contents Previous Page: WatiN API Reference - Dispose Next Page: WatiN API Reference - Divs
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
The dialog watcher class is used to handle javascript and other dialogs. The IE instance can have a dialog watcher which can have dialog handlers assigned to it to assist in processing pop up dialogs.
Example of Usage
This is a rework of the TestAddDialogHander2 test that was introduced earlier:
''' <summary> ''' Ensure that an alert dialog will be handled and ''' closed properly. ''' </summary> ''' <remarks> ''' currentURL is a Private Const = ''' "http://localhost:3587/popup.html" ''' HelloButton is a Private Const = "hello" ''' </remarks> <Test()> _ Public Sub TestAddDialogHandler2Plus() Using ie As IE = New IE(currentURL) Dim myHandler As New AlertAndConfirmDialogHandler Dim myWatcher As New DialogWatcher(ie.ProcessID) myWatcher.Add(myHandler) ie.Button(HelloButton).ClickNoWait() Assert.IsTrue(myWatcher.Count > 0, "There should be a hello dialog open at this point.") ie.WaitForComplete() End Using End Sub
The dialog watcher is created from the IE instance and is therefore already assigned to it. A handler is added to process the javascript popup and the button is clicked to display the dialog.
Another way of using the dialog watcher is presented next:
''' <summary> ''' Ensure that an alert dialog will be handled and ''' closed properly. ''' </summary> ''' <remarks> ''' currentURL is a Private Const = ''' "http://localhost:3587/popup.html" ''' HelloButton is a Private Const = "hello" ''' </remarks> <Test()> _ Public Sub TestAddDialogHandler2Plus2() Using ie As IE = New IE(currentURL) Dim myHandler As New AlertAndConfirmDialogHandler Dim myWatcher As DialogWatcher myWatcher = ie.DialogWatcher myWatcher.Add(myHandler) ie.Button(HelloButton).ClickNoWait() Assert.IsTrue(myWatcher.Count > 0, "There should be a hello dialog open at this point.") ie.WaitForComplete() End Using End Sub
Here the dialog watcher is not created from the IE instance, so it must be specifically assigned.
Contents: Table of Contents Previous Page: WatiN API Reference - ContainsText Next Page: WatiN API Reference - Dispose