Thursday, July 31, 2014

Check if web Page has horizontal and vertical Scrollbars

The following code will be able to tell if the web page has a horizontal or vertical scrollbars. And function GetScrollbarState accepts two arguments:
1. The  document (i.e., Browser(“MyBrowser”).Page(“MyPage”).Object)
2. The direction of the scrollbar – “h” for horizontal or “v”” for vertical. 
This function will returns true or false if there is a scrollbar in the given direction

Function GetScrollBarState(ByRef document, ByVal scrollbarDir)
    Dim result
    Dim root    
 
    On Error Resume Next
    Select Case LCase(document.compatMode)
        Case "backcompat"
            Set root = document.body
        Case Else
            Set root = document.documentElement
    End Select
    
    Select Case LCase(scrollbarDir)
        Case "h"
            scrollbarDir = "horizontal"
            result = (root.scrollWidth > root.clientWidth)
        Case "v"
            scrollbarDir = "vertical"
            result = (root.scrollHeight > root.clientHeight)
    End Select    
    
    If Err.Number <> 0 Then
        Reporter.ReportEvent micWarning, "GetScrollBarState", "Could not get state for " & scrollbarDir & " scrollbar"
        result = "Undefined"
        Err.Clear
    End If
    GetScrollBarState = result
End Function
 
 
The following two functions will be used as Page properties using QTP/UFT RegisterUserFunc:
 
Function HasHorizontalScrollbar(ByRef oPage)
    HasHorizontalScrollbar = GetScrollBarState(oPage.Object, "h")
End Function
 
Function HasVerticalScrollbar(ByRef oPage)    
    HasVerticalScrollbar = GetScrollBarState(oPage.Object, "v")
End Function
 
RegisterUserFunc "Page", "HasHorizontalScrollbar", "HasHorizontalScrollbar"
RegisterUserFunc "Page", "HasVerticalScrollbar", "HasVerticalScrollbar"
 
'Usage with QTP/VBScript Code
Set oPage = Browser("Browser").Page("Page") 
Print oPage.HasHorizontalScrollbar
Print oPage.HasVerticalScrollbar

**** Special thanks to Meir Bar-Tal for such a great function which made our work easy *****