Aug 27 2014

Calculating Page Size in Pixels

This short post describes a handy way to calculate the page size in pixels.
The steps:

  • The function GetPageNumPixels accepts a Page test object and calls the function GetDocumentElement.
  • The function GetDocumentElement accepts the same Page object and retrieves, via the Object property, the documentElement for either a Page that is standards-compatible or not.
  • The function GetDocumentElement then calculates the rectangle occupied by the page (including hidden parts) based on its client Height and Width and returns it. If some problem arises, the function returns -1.
Function GetPageNumPixels(ByRef oPage)
Dim result
Dim root
Set root = GetDocumentElement(oPage)
If Not root Is Nothing Then
result = root.clientHeight*root.clientWidth
result = -1
End If
GetPageNumPixels = result
End Function
Function GetDocumentElement(ByRef oPage)
Dim root
On Error Resume Next
With oPage.Object
Select Case LCase(.document.compatMode)
Case "backcompat"
Set root = .document.body
Case Else
Set root = .document.documentElement
End Select
End With
If Err.Number <> 0 Then
Reporter.ReportEvent micFail, "GetDocumentElement", "Could not get document element for page."
Set root = Nothing
End If
On Error Goto 0
Set GetDocumentElement = root
End Function

About Meir Bar-Tal

Meir Bar-Tal is an Automation Architect with extensive technical, educational and leadership experience in Software Design, Development and Testing. For many years Meir has helped companies establish solid foundations for their testing activities with a wide variety of software applications and systems and across different technologies and platforms. Meir is a well-known expert in Data-Driven, Keyword Driven and Hybrid methodologies and has published a number of influential articles on the applications of Design Patterns to Test Automation. For the last five years, he has been pushing forward an ambitious program towards the development of an Object Oriented automation framework named Sunscrit, a huge project which is still underway. Meir is the owner of www.AdvancedQTP.com of which he was a co-founder in 2007 and served as its Editor in Chief, Author and Forums Administrator ever since. Meir lives in Israel and cooperates closely with HP R&D and other colleagues to improve the user experience and productivity with HP Software tools.