QTP Image Capture :Capturing Full Webpage screenshot using Dot Net Factory and Snagit:
QTP/UFT by dafault has CaptureBitmap method to Capture the screenshot.
One limitation of this method is it always captures visible part of the webpage.
But imagine if you have to capture full webpage:
Below code can be used to capture full webpage image.
Lets go step by step to the solution,
As part of implementing the solution, we need to get the height and
weight of the entire page. Inorder to get that we using DOM of a page
using .object method.
''#Get the Full Height of Page
FullHeight = Browser("Wikipedia, the free encycloped").Object.document.body.scrollheight
''#Get the Full width of Page
Fullwidth = Browser("Wikipedia, the free encycloped").Object.document.body.scrollwidth
Once we found the complete page size, we need to find the client size (how much browser can show)
''#Get the visible height - Viewable part of the page
BrowserHeight = Browser("Wikipedia, the free encycloped").Object.document.body.clientHeight
''#Get the visible width - Viewable part of the page
Browserwidth = Browser("Wikipedia, the free encycloped").Object.document.body.clientwidth
Next we need to implement required dot net libraries using Dot Net Factory
Set oGraphics=DotNetFactory.CreateInstance("System.Drawing.Graphics")
Set oPoint=DotNetFactory.CreateInstance("System.Drawing.Point")
Set oImgFormat=DotNetFactory.CreateInstance("System.Drawing.Imaging.ImageFormat","System.Drawing", Nothing)
Set oImageLib = DotNetFactory.CreateInstance("System.Drawing.Image")
Set oPens=DotNetFactory.CreateInstance("System.Drawing.Pens","System.Drawing")
We will loop through the page and take screenprints separately. finally using Dotnet library we will merge the images.
''#Dot Net Factory - Importing required Dot net classes
Set oGraphics=DotNetFactory.CreateInstance("System.Drawing.Graphics")
Set oPoint=DotNetFactory.CreateInstance("System.Drawing.Point")
Set oImgFormat=DotNetFactory.CreateInstance("System.Drawing.Imaging.ImageFormat","System.Drawing", Nothing)
Set oImageLib = DotNetFactory.CreateInstance("System.Drawing.Image")
Set oPens=DotNetFactory.CreateInstance("System.Drawing.Pens","System.Drawing")
''#Launch IE and Maximize the Browser
systemutil.Run "iExplore","https://en.wikipedia.org/wiki/Main_Page",,,3
''#Get the Full Height of Page @@ hightlight id_;_Browser("Wikipedia, the free encycloped").Page("Wikipedia, the free encycloped").Link("Link")_;_script infofile_;_ZIP::ssf3.xml_;_
FullHeight = Browser("Wikipedia, the free encycloped").Object.document.body.scrollheight
''#Get the visible height - Viewable part of the page
BrowserHeight = Browser("Wikipedia, the free encycloped").Object.document.body.clientHeight
''#Get the Full width of Page
Fullwidth = Browser("Wikipedia, the free encycloped").Object.document.body.scrollwidth
''#Get the visible width - Viewable part of the page
Browserwidth = Browser("Wikipedia, the free encycloped").Object.document.body.clientwidth
''#Calculate how many times page needs to be scroll down
If round(FullHeight/BrowserHeight) < (FullHeight/BrowserHeight) Then
Heightscrollcount = round(FullHeight/BrowserHeight) + 1
else
Heightscrollcount = round(FullHeight/BrowserHeight)
End If
''#Calculate how many times page needs to be scroll right
If round(Fullwidth/Browserwidth) < (Fullwidth/Browserwidth) Then
widthscrollcount = round(Fullwidth/Browserwidth) + 1
else
widthscrollcount = round(Fullwidth/Browserwidth)
End If
''#Scroll down through the page and take screen prints
For HeightIterator = 1 To Heightscrollcount
''#Scroll right through the page and take screenprints
For WidthIterator = 1 To widthscrollcount
Browser("Wikipedia, the free encycloped").Page("Wikipedia, the free encycloped").CaptureBitmap "C:\temp1\Image_" & HeightIterator & "_" & WidthIterator & ".png"
Browser("Wikipedia, the free encycloped").object.document.parentwindow.scrollBy Browserwidth, 0
Next
''#Scroll left back to normal
For WidthIterator = 1 To widthscrollcount
''# Giving scrollby in negative to scroll left
Browser("Wikipedia, the free encycloped").object.document.parentwindow.scrollBy (0-Browserwidth), 0
Next
Browser("Wikipedia, the free encycloped").object.document.parentwindow.scrollBy 0, BrowserHeight
Next
''#Merging Images
finalImage = "C:\temp1\Image_Final.png"
''#Final Image height and Width should match with Scrollheight and Width. so lets create final image with scroll height and width
''#set img3 = oBmp.Bitmap(Fullwidth, FullHeight)
Set img3=DotNetFactory.CreateInstance("System.Drawing.Bitmap","System.Drawing",Fullwidth, FullHeight)
set g = oGraphics.FromImage(img3)
HeightCount = 0
WidthCount = 0
For HeightIterator = 1 To Heightscrollcount
For WidthIterator = 1 To widthscrollcount
set img = oImageLib.FromFile("C:\temp1\Image_" & HeightIterator & "_" & WidthIterator & ".png")
''# For the last peace of image
If WidthIterator > 1 Then
If WidthCount + Cint(img.Width) > Fullwidth Then
WidthCount = WidthCount - (WidthCount + Cint(img.Width) - Fullwidth)
End if
End if
If HeightCount + Cint(img.Height) > FullHeight Then
HeightCount = HeightCount - (HeightCount + Cint(img.Height) - FullHeight)
End if
''#Drawing the image
g.DrawRectangle oPens.Black,WidthCount,HeightCount,WidthCount+Cint(img.Width),HeightCount+Cint(img.Height)
g.DrawImage img, WidthCount, HeightCount
If WidthIterator > 1 Then
''#Add Right
WidthCount = WidthCount + Cint(img.Width)
else
''#AddBottom
HeightCount = HeightCount + Cint(img.Height)
End if
img.Dispose
Next
Next
g.Dispose
img3.Save finalImage, oImgFormat.Png
img3.Dispose
