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    
    OnErrorResumeNext
    SelectCase LCase(document.compatMode)
        Case"backcompat"
            Set root = document.body
        CaseElse
            Set root = document.documentElement
    EndSelect
    
    SelectCase LCase(scrollbarDir)
        Case"h"
            scrollbarDir = "horizontal"
            result = (root.scrollWidth > root.clientWidth)
        Case"v"
            scrollbarDir = "vertical"
            result = (root.scrollHeight > root.clientHeight)
    EndSelect    
    
    If Err.Number <> 0 Then
        Reporter.ReportEvent micWarning, "GetScrollBarState", "Could not get state for " & scrollbarDir & " scrollbar"
        result = "Undefined"
        Err.Clear
    EndIf
    GetScrollBarState = result
EndFunction
 
 
The following two functions will be used as Page properties using QTP/UFT RegisterUserFunc:
 
Function HasHorizontalScrollbar(ByRef oPage)
    HasHorizontalScrollbar = GetScrollBarState(oPage.Object, "h")
EndFunction
 
Function HasVerticalScrollbar(ByRef oPage)    
    HasVerticalScrollbar = GetScrollBarState(oPage.Object, "v")
EndFunction
 
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 *****

No comments:

Post a Comment