Monday, October 10, 2011

Xpath Based QTP Object Identification

Xpath object identification is a new feature in QTP 11. I would like to share an example for it using Google web page

Dim oPage

Set oPage=Browser("name:=Google").Page("title:=Google")

oPage.WebEdit("xpath:=//INPUT[@name='q']").Set "advancedqtp"'Entering value in Google textbox.

oPage.WebButton("xpath:=//INPUT[@value='Google Search']").Click'Clicking Google Search button.

Try it and enjoy

Thursday, October 6, 2011

Getting File Path List Using Wild Card Characters

In this article, I would like to share how to get the file path for the files which are residing in a folder using wild card.

Consider the case where we have a folder containing text files temp.txt, tem.txt, temp1.txt, temp2.txt.

In the above text file list if we want to get path for the files using “temp*.txt” wild card, GetFiles (System.IO.Directory) method can be used.

Dim inFile,oColFilesList,oArrayList,oDI

Set oArrayList=DotNetFactory.CreateInstance("System.Collections.ArrayList","")
Set oDI=DotNetFactory.CreateInstance("System.IO.Directory","mscorlib")

Set oColFilesList=oArrayList.Adapter(oDI.GetFiles("C:\Files List","temp*.txt"))

For inFile=0 to Cint(oColFilesList.Count)-1
Msgbox oColFilesList(inFile)
Next

Set oArrayList=Nothing
Set oDI=Nothing
Set oColFilesList=Nothing
The above code will retrieve paths of temp.txt, temp1.txt, and temp2.txt files but not for tem.txt file.

Tuesday, October 4, 2011

Merging Images Using DOT NET Factory



strImage1Path="C:\Sunset.jpg"
strImage2Path="C:\Winter.jpg"
strMergedImagePath="C:\MergeImage.jpg"'Path where the merged image is to be stored.

Set oImage=DotNetFactory.CreateInstance("System.Drawing.Image","System.Drawing")
Set oGraph=DotNetFactory.CreateInstance("System.Drawing.Graphics","System.Drawing")
Set oPens=DotNetFactory.CreateInstance("System.Drawing.Pens","System.Drawing")
Set oImage1=oImage.FromFile(strImage1Path)
Set oImage2=oImage.FromFile(strImage2Path)

inWidth=Cint(oImage1.Width)+Cint(oImage2.Width)

If oImage1.Height >=oImage2.Height Then
inHeight=Cint(oImage1.Height )
Else
inHeight=Cint(oImage2.Height )
End If

Set oBmp=DotNetFactory.CreateInstance("System.Drawing.Bitmap","System.Drawing",inWidth,inHeight)

Set gr=oGraph.FromImage(oBmp)

gr.DrawRectangle oPens.Black,0,0,Cint(inWidth)-1,Cint(inHeight)-1

gr.DrawImage oImage1,0,0
gr.DrawImage oImage2,oImage1.Width,0

oBmp.Save(strMergedImagePath)

Set oImage=Nothing
Set oGraph=Nothing
Set oPens=Nothing
Set oImage1=Nothing
Set oImage2=Nothing
Set oBmp=Nothing
Set gr=Nothing

Monday, October 3, 2011

Retrieving Excel Column Names Using ADODB


strExcelPath="C:\Book1.xls"
strTableName="Sheet1$"'Note: $ symbol should be included with Table Name.
Set oCon=CreateObject("ADODB.Connection")

With oCon
.Provider = "MSDASQL"
.ConnectionString = "Driver={Microsoft Excel Driver (*.xls)};" & _
"DBQ="&strExcelPath&"; ReadOnly=False;"
.Open
End With

Set oRs1=oCon.OpenSchema(4,Array(Empty, Empty,strTableName,Empty))

Do Until oRS1.EOF
Msgbox oRs1.Fields.Item("COLUMN_NAME").Value
oRS1.MoveNext
Loop

oRs1.Close
oCon.Close

Set oRs1=Nothing
Set oCon=Nothing