Monday, January 9, 2017

Capture the whole web page using UFT/QTP

I would like to share how to take the screen shot of entire page (Not just visible) of a web application in QTP/UFT Test.

In order to do take the screenshot of complete page, I have used DotNetFactory  and System.Drawing dot net library.

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
Please find the below temporary images and final images 






Monday, September 9, 2013

Dot Net Factory - Basics

Dot Net Factory is a utility object that enables QTP scripting to directly access methods and properties of Dot Net Object

Because of this method, QTP stands at no limit in its capability.  Using Dot Net Factory, QTP can access the Dot Net’s static methods and properties of a class that does not have instance constructor and also user defined custom dot net classes.

Syntax of Dot Net Factory


DotNetFactory.CreateInstance (TypeName [,Assembly] [,args])


TypeName – The Full Name of the object Type. E.g. System.DateTime

Assembly – This is optional. You need to pass the assembly for type. If the assembly is preloaded in the registry, you do not need to enter it. If you do not pass this argument, QTP assumes that the assembly is resident in memory. If QuickTest does not find the assembly, an error message is displayed during the run session.


Args – This is also optional, You need to pass the arguments for typename you specified or for assembly (if any)


Let us go with examples to get a clear idea,

Example 1:


Working with Dot net Date Parser


Dim dSysDate , objDate
Set dSysDate = Dotnetfactory.CreateInstance(“System.DateTime”) ' Creating an object for Dot Net Component System.DateTime
Set objDate = dSysDate.Parse(“Mon, 9 Sep 2013″)

strDate = objDate.Day & “/” & objDate.Month & “/” & objDate.Year
msgbox strDate
Set dSysDate = Nothing
Set objDate = Nothing

The output will be “09/09/2013”.

Example 2: 

Simple user defined forms 

The below example deals with creating a simple Dot net form with a button, text box and label using dot net component “System.Windows.Forms”

‘Creates a Form
Set objFrm = DotNetFactory.CreateInstance(“System.Windows.Forms.Form”, “System.Windows.Forms”)
‘Creates a Button
Set objBtn = DotNetFactory.CreateInstance(“System.Windows.Forms.Button”, “System.Windows.Forms”)
‘Created a Text Box
Set objEdt = DotNetFactory.CreateInstance(“System.Windows.Forms.TextBox”, “System.Windows.Forms”)
intx=60
inty=30

‘Creates an object to system drawing point to draw a form and to get the locations(X, Y) for the controls
Set objpnt = DotNetFactory.CreateInstance(“System.Drawing.Point”,”System.Drawing”,intx,inty)

‘Creates a label
Set objlbl= DotNetFactory.CreateInstance(“System.Windows.Forms.Label”,”System.Windows.Forms”) 

‘Configuring the label
objlbl.Text=”Enter Password”
objlbl.Location= objpnt

‘Adding the label in Form
objFrm.Controls.Add(objlbl)
objpnt.Y=CInt(objlbl.Height)+40
objEdt.Location= objpnt

objEdt.UseSystemPasswordChar=true ’To set the password character From system
objFrm.Controls.Add(objEdt) ‘Adding the Text box in Form
objBtn.Text=”Close”

objpnt.Y=Cint(objpnt.Y)+CInt(objEdt.Height)+20
objBtn.Location=p1

objFrm.CancelButton= objBtn ‘Adding a button as cancel button in form
objFrm.Controls.Add(objBtn)
objFrm.Text=”Dot Net Factory Dialog Box”
objFrm.StartPosition=CenterScreen
objForm.ShowDialog ‘Showing the designed form


The above mentioned method is suitable for simple forms and may look little complex when we are creating a complex form or form with more controls.

Most of the time we prefer IDE based design for designing a form rather then creating them using code, so let’s look into the IDE based form design in my next article on Dot Net Factory.