My Blog List

Monday, June 8, 2015

UFT - Take full page screenshot by scrolling the page


'########################################################################################
'This is navigate through the full page and taking individual screenshot of visible area
'########################################################################################
Function TakeScreenshot
 Dim intScrolls, intScroll, strScrollPos
 Set pgApp = Browser("").Page("")

 intScrolls = Round(pgApp.RunScript("document.documentElement.scrollHeight / (screen.height)"), 2)
 If intScrolls < 1 Then intScrolls = -1

 pgApp.RunScript "window.scrollTo(0, 0);"
 Wait 1
 Browser("").CaptureBitmap "C:\screenshot0.png", True
 For intScroll = 0 To intScrolls
  If Environment.Value("Browser") = "CHROME" Then
   strScrollPos = "scrollY"
  Else
   strScrollPos = "document.documentElement.scrollTop"
  End If
  If pgApp.RunScript("document.documentElement.scrollHeight - " & strScrollPos) > pgApp.RunScript("screen.height") Then  
   intY = pgApp.RunScript(strScrollPos & " + screen.height - 168;")
   pgApp.RunScript "window.scrollTo(0, " & intY & ");"
   Wait 1  
   Browser("").CaptureBitmap "screenshot" & (intScroll + 1) & ".png", True
  End If          
 Next
 
 'To merge the images to generate full page screenshot
 Call MergeImages
End Function

'########################################################################################
'This is to merge all individual images to one full page screenshot
'########################################################################################
Function MergeImages   
    Dim drawImage, grapImage, imgFormat, imgLayer, imgPT
    Set drawImage = DotNetFactory.CreateInstance("System.Drawing.Bitmap", "System.Drawing", 1920, 1050)
    drawImage.SetResolution 50, 45
    Set grapImage = DotNetFactory.CreateInstance("System.Drawing.Graphics").FromImage(drawImage)
   
    Set imgLayer = DotNetFactory.CreateInstance("System.Drawing.Bitmap", "System.Drawing", "C:\screenshot0.png")
    Set imgPT = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing", 0, 0)
    grapImage.DrawImage Layer1, pt1
   
    Set imgLayer = DotNetFactory.CreateInstance("System.Drawing.Bitmap", "System.Drawing", "C:\screenshot1.png")
    Set imgPT = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing", 0, 540)
    grapImage.DrawImageUnScaled Layer2, pt2
   
    Set imgFormat = DotNetFactory.CreateInstance("System.Drawing.Imaging.ImageFormat", "System.Drawing", Nothing)
    drawImage.Save "finalScreenshot.png", format.PNG
End Function


This code is for UFT/VBScript and you can use the same logic by applying different APIs available for SELENIUM or any other tools.

3 comments:

  1. Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about Selenium

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Sorry, my previous comment was accidentally deleted. Thanks for the lovely post, the code worked like charm without making any changes to it.

      Arvy, Test Automation Engineer

      Delete