Monday, October 31, 2011

Running Javascript in QTP

You can run Javascript in QTP using Runscript method. This is a new feature in QTP 11. Here is an example for it.

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

Set oTxtGoogle=oPage.RunScript("document.getElementsByName('q')(0);")
'The above line gets DOM object for Google textbox using Javascript.

oTxtGoogle.Value="qtp"

I hope it is helpful.

Friday, October 21, 2011

Retrieve values from Quality Center Test Lab and Defects tab using OTA

Getting the Number of Passed, Failed, No Run and Not Completed Test cases

We can make use of the Filter as given below to get the number of Test cases according to the status Passed, Failed, No Run, Not Completed,etc.

Dim tdc, qcServer
Dim qcUsername,qcPassword,qcDomain,qcProject

Set tdc = CreateObject("TDApiOle80.TDConnection")
qcServer = "QC URL"
tdc.InitConnectionEx qcServer

If tdc.Connected Then
Msgbox "Connected to QC"
Else
MsgBox "Not connected to QC"
End If

qcUsername = "Your QC Username" 'Username
qcPassword = "Your Password" 'Password
tdc.Login qcUsername, qcPassword

If tdc.LoggedIn Then
MsgBox "Logged in to QC"
Else
MsgBox "Logged out from QC"
End If

qcDomain = "Domain" 'QC Domain Name
qcProject = "Project" 'QC Project Name

tdc.Connect qcDomain, qcProject

Dim vPath: vPath="Root\Account1\Proj1" 'Path to the folder containing test set
Dim vPass,vFail,vNR,vNC

Dim oTestSet: Set oTestSet = tdc.TestSetTreeManager.NodeByPath(vpath).TestSetFactory.NewList("").Item(1).TsTestFactory 'Item(1) Refers to the 1st test set in the path
Dim testFilter1: Set testFilter1 = oTestSet.Filter

'To Filter by Passed status
testFilter1.Filter("TC_STATUS") = "Passed"
vPass = vPass + oTestSet.NewList(testFilter1.Text).Count

'To Filter by Failed Status
testFilter1.Filter("TC_STATUS") = "Failed"
vFail = vFail + oTestSet.NewList(testFilter1.Text).Count

'To Filter by No Run status
vNR = vNR + oTestSet.NewList("[Filter]{TableName:TESTCYCL,ColumnName:TC_STATUS,LogicalFilter:" & Chr(39) & "No" & Chr(32) & "Run" & Chr(39) & ",VisualFilter:" & Chr(39) & "No" & Chr(32) & "Run" &Chr(39) & ",NO_CASE:}").Count

'To Filter by Not Completed status
vNC = vNC + oTestSet.NewList("[Filter]{TableName:TESTCYCL,ColumnName:TC_STATUS,LogicalFilter:" & Chr(39) & "Not" & Chr(32) & "Completed" & Chr(39) & ",VisualFilter:" & Chr(39) & "Not" & Chr(32) &"Completed" & Chr(39) & ",NO_CASE:}").Count

Msgbox "Passed: "&vPass&" Failed: "&vFail&" No Run: "&vNR&" Not Completed: "&vNC

Set testFilter1=Nothing

Set oTestSet=Nothing

Set tdc=Nothing


Getting the Number of Test cases executed by a tester in a specific test set

We can use the below code to get the count of test cases executed by a tester in a particular test set.

Dim vPath: vPath="Root\Account1\Proj1" 'Path to the folder containing test set

Dim oTestSet: Set oTestSet = tdc.TestSetTreeManager.NodeByPath(vpath).TestSetFactory.NewList("").Item(1).TsTestFactory 'Item(1) Refers to the 1st test set in the path ; tdc refers to the test
connection object in the previous snippet

Dim testFilter1: Set testFilter1 = oTestSet.Filter
Dim vtestcount: vtestcount=0
Dim tester_name:tester_name="divya" 'QC username of the tester for whom the count has to be calculated
testFilter1.Filter("TC_ACTUAL_TESTER") =chr(34)&Cstr(tester_name)&chr(34)

vtestcount = vtestcount + oTestSet.NewList(testFilter1.Text).Count
Msgbox "Number of Test Cases executed by "&tester_name&" : "&vtestcount
Set testFilter1=Nothing
Set oTestSet=Nothing


Getting the number of defects raised by a tester in a specific release according to severity

We need to apply multiple filters as we are using Tester name, Release Name and Defect Severity as the criteria.


Dim Bug_Ct:Bug_Ct=0
Dim tester_name:tester_name="divya"
Dim test_severity:test_severity="2-High"

Dim oBugFactory: Set oBugFactory=tdc.BugFactory
Dim TotBugcnt: TotBugcnt=oBugFactory.NewList("").Count 'To get the total number of defects in Defects tab.
Dim oBugFilter1:Set oBugFilter1=oBugFactory.Filter

oBugFilter1.Filter("BG_DETECTED_BY")=tester_name 'QC username of the tester for whom the defect count has to be calculated
oBugFilter1.Filter("BG_DETECTED_IN_REL") = "^Releases\proj1\1.1.01.201^" 'Name of the Release
oBugFilter1.Filter("BG_SEVERITY")=test_severity 'Defect Severity. Eg.: Can be 2-High, 1-Critical, 4-Low. Possible Values can be obtained from QC defects tab

Bug_Ct = Bug_Ct + oBugFactory.NewList(oBugFilter1.Text).Count
Msgbox Bug_Ct

Set oBugFilter1=Nothing


Getting the number of defects in a specific release according to defect status

Here we need to apply the following criteria: Release Name, Defect status. If we want to be more specific , say, we need to find the same for a particular tester, then we can include tester name as a filter criteria.

The following code will give the output for number of defects raised in a specific release having the status "Closed" which were raised by tester1 and tester2.


Dim Bug_Ct:Bug_Ct=0
Set oBugFactory=tdc.BugFactory
Set oBugFilter1=oBugFactory.Filter
oBugFilter1.Filter("BG_DETECTED_IN_REL") = "^Releases\proj1\1.1.01.201^" 'Name of the Release
oBugFilter1.Filter("BG_STATUS")="Closed" 'Defect status for which count is calculated
oBugFilter1.Filter("BG_DETECTED_BY")="tester1 or tester2" 'QC Username of the testers for whom the count is calculated
Bug_Ct = Bug_Ct + oBugFactory.NewList(oBugFilter1.Text).Count
Msgbox Bug_Ct
Set oBugFilter1=Nothing

Tuesday, October 11, 2011

How To Get Cursor Position From WebEdit

The below snippet can be used to get the cursor position from WebEdit
Dim oPage:Set oPage=Browser("name:=Google").Page("title:=Google")
Dim oSel:Set oSel=oPage.Object.Selection.createRange
Dim oWebEdit:Set oWebEdit=oPage.WebEdit("name:=q").Object

oSel.moveStart "character",-Len(oWebEdit.value)

Msgbox Len(oSel.text)'Here you will get the cursor position.

Set oPage=Nothing
Set oSel=Nothing
Set oWebEdit=Nothing

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

Thursday, September 29, 2011

QTP Codes blog is being updated

QTP Codes blog is being updated. You will see post design changes shortly.

More Methods For PathFinder Object

Most of us are aware of the Locate method available for PathFinder object. But there are more useful and unfamiliar methods associated with PathFinder Object. I would like to share how these methods can be used in QTP.

# INSERT
Description:
This method is used to add a new folder path to the Folders list (Tools-> Options-> Folders).

Syntax:
PathFinder.Insert [Path],[Index]
Example:
PathFinder.Insert "C:\Test",0
The above example will add new folder path to the Folders list.

# FIND
Description:
This method returns the Index of the specified path.

Syntax:
Msgbox PathFinder.Find [Path]
Example:
Msgbox PathFinder.Find("C:\Test")
The above example returns the Index of the specified path. If the path is not found, it will return -1.

# MOVEITEM
Description:
This method can be used to swap the Index of two folder paths.

Syntax:
Pathfinder.MoveItem [Source_Index],[Destination_Index]
Example:
Pathfinder.MoveItem 1,0

# REMOVE
Description:
This method removes the specified folder path from the folders list.

Syntax:
Pathfinder.Remove [Path]
Example:
Pathfinder.Remove("C:\Test")

# REMOVEALL
Description:
This method removes all the items from the folders list.

Syntax/Example:
Pathfinder.RemoveAll
Note: It removes default folder item as well.

# REMOVEBYINDEX
Description:
This method removes the folder path item using Index.

Syntax:
PathFinder.RemoveByIndex([Path_Index])
Example:
PathFinder.RemoveByIndex(0)

# SAVE
Description:
This method can be used to commit all the changes which are done using PathFinder object. If Save method is not used, your changes in Folder List will not be retained.

Syntax/Example:
PathFinder.Save