Really easy one tonight, but something ever tester working with APIs should be aware of. Even if you know better, someone else might not.

Here’s a little example I threw together in the past 10 minutes to illustrate tonight’s point.

Public Declare Function SetFocus Lib "user32" Alias "SetFocus" (ByVal HWnd As IntPtr) As IntPtr

Public Sub SetWindowFocus(ByVal hwnd As IntPtr)

‘ call the Windows SetFocus function

Dim retval As IntPtr = SetFocus(hwnd)

‘ retval is NULL if API failed to set focus on requested window

If retval.Equals(0) Then

Throw New Exception("Failed to set focus")

End If

End Sub

‘ the actual test case code

Public Sub Run()

‘————————————————–

InitScenario("Verify user can set focus on window")

‘————————————————–

‘ get the hwnd for the editor

Dim hwnd As IntPtr = GetSomeWindowsHwnd()

Dim ex As Exception

‘ try to set focus to window

Try

SetWindowFocus(hwnd)

Catch ex

End Try

‘ if we catch an exception, log a failure

If ex Is Nothing Then

LogPass()

Else

LogFailure()

End If

‘ continue with the rest of test case

End Sub

By popular demand, I will post the answer on a second entry. At least this way, if you find a bug in this code that I wasn’t expecting, I can play it off that I planned it that way. =)