Thursday

QTP - Associate Object repository at run time in QTP

Methods
Add
Adds an object repository file to the specified position in the collection.
Find
Finds the position of the specified object repository file.
MoveToPos
Moves the object repository file entry from the current position to the specified new position.
Remove
Removes the object repository that is located in the specified position.
RemoveAll
Removes all object repository files from the collection.
SetAsDefault
Sets the object repository files associated with the current action as the default files for all new actions.




'************************************************************************************************************************


'Description:


'


'This example opens a test, configures an action's object repositories collection


'and saves the test.


'


'Assumptions:


'There is no unsaved test currently open in QuickTest.


'For more information, see the example for the Test.SaveAs method.


'************************************************************************************************************************





Dim qtApp 'As QuickTest.Application ' Declare the Application object variable


Dim qtRepositories 'As QuickTest.ObjectRepositories ' Declare an action's object repositories collection variable


Dim lngPosition





' Open QuickTest


Set qtApp = CreateObject("QuickTest.Application") ' Create the Application object


qtApp.Launch ' Launch QuickTest


qtApp.Visible = True ' Set QuickTest to be visible





' Open a test and get the "Login" action's object repositories collection


qtApp.Open "C:\Tests\Test1", False, False ' Open a test


Set qtRepositories = qtApp.Test.Actions("Login").ObjectRepositories ' Get the object repositories collection object of the "Login" action





' Add MainApp.tsr if it's not already in the collection


If qtRepositories.Find("C:\MainApp.tsr") = -1 Then ' If the repository cannot be found in the collection


qtRepositories.Add "C:\MainApp.tsr", 1 ' Add the repository to the collection


End If





' If InnerWnd.tsr is moved down the list - place it back at position 1


If qtRepositories.Count > 1 And qtRepositories.Item(2) = "C:\InnerWnd.tsr" Then ' If there's more than one object repository and InnerWnd.tsr is in position 2


qtRepositories.MoveToPos 1, 2 ' Switch between the first two object repositories


End If





' If Debug.tsr is in the collection - remove it


lngPosition = qtRepositories.Find("C:\Debug.tsr") ' Try finding the Debug.tsr object repository


If lngPosition <> -1 Then ' If the object repository was found in the collection


qtRepositories.Remove lngPosition ' Remove it


End If





' Set the new object repository configuration as the default for all new actions


qtRepositories.SetAsDefault ' Set object repositories associated with the "Login" action as the default for all new actions





'Save the test and close QuickTest


qtApp.Test.Save ' Save the test


qtApp.Quit ' Quit QuickTest





Set qtRepositories = Nothing ' Release the action's shared repositories collection


Set qtApp = Nothing ' Release the Application object

'********************
'***************************


'The following example retrieves an object repository's objects and properties,


'looks for specific test objects using several methods, and copies a test object


'to another object repository.


'********************
'***************************
Dim ImageObj, PageObj, RepositoryFrom, RepositoryTo




Set RepositoryFrom = CreateObject("Mercury.ObjectRepositoryUtil")


Set RepositoryTo = CreateObject("Mercury.ObjectRepositoryUtil")




RepositoryFrom.Load "C:\QuickTest\Tests\Flights.tsr"


RepositoryTo.Load "E:\Temp\Tests\Default.tsr"


Function EnumerateAllChildProperties(Root)


'The following function recursively enumerates all the test objects directly under


'a specified parent object. For each test object, a message box opens containing the


'test object's name, properties, and property values.


Dim TOCollection, TestObject, PropertiesCollection, Property, Msg




Set TOCollection = RepositoryFrom.GetChildren(Root)




For i = 0 To TOCollection.Count - 1


Set TestObject = TOCollection.Item(i)


Msg = RepositoryFrom.GetLogicalName(TestObject) & vbNewLine


Set PropertiesCollection = TestObject.GetTOProperties()




For n = 0 To PropertiesCollection.Count - 1


Set Property = PropertiesCollection.Item(n)


Msg = Msg & Property.Name & "-" & Property.Value & vbNewLine


Next


MsgBox Msg




EnumerateAllChildProperties TestObject


Next




End Function




Function EnumerateAllObjectsProperties(Root)


'The following function enumerates all the test objects under a specified object.


'For each test object, a message box opens containing the test object's name,


'properties, and property values.


Dim TOCollection, TestObject, PropertiesCollection, Property, Msg




Set TOCollection = RepositoryFrom.GetAllObjects(Root)




For i = 0 To TOCollection.Count - 1


Set TestObject = TOCollection.Item(i)


Msg = RepositoryFrom.GetLogicalName(TestObject) & vbNewLine




Set PropertiesCollection = TestObject.GetTOProperties()


For n = 0 To PropertiesCollection.Count - 1


Set Property = PropertiesCollection.Item(n)


Msg = Property.Name & "-" & Property.Value & vbNewLine


Next




MsgBox Msg


Next




End Function




Function RenameAllImages(Root)


'The following function sets a new name for all image test objects under a specified object.


Dim TOCollection, TestObject, PropertiesCollection, Property




Set TOCollection = RepositoryTo.GetAllObjectsByClass("Image")




For i = 0 To TOCollection.Count - 1


Set TestObject = TOCollection.Item(i)


RepositoryTo.RenameObject (TestObject, "Image " & i)


RepositoryTo.UpdateObject TestObject


Next




End Function




Function RemoveAllLinks(Root)


'The following function recursively enumerates all the test objects under a specified object.


'It looks for all test objects of class Link and removes them from their parent objects.


Dim TOCollection, TestObject, PropertiesCollection, Property




Set TOCollection = RepositoryFrom.GetChildren(Root)




For i = 0 To TOCollection.Count - 1


Set TestObject = TOCollection.Item(i)


TOClass = TestObject.GetTOProperty("micclass")




If TOClass = "Link" Then


RepositoryFrom.RemoveObject Root, TestObject


End If




EnumerateAllChildProperties TestObject


Next




End Function

Monday

QTP/QC - Schedule the testset in QC by VBS

'User must Admin at the machine where he want to run the testset
'This can run manual as well as Automated test suite
' It can be schedule at any partcular future time
' User need not to open the QC

Public Sub RunTestSet(otdc,tsFolderName,tSetName,HostName,runWhere)


Dim TSetFact, tsList
Dim theTestSet
Dim tsTreeMgr
Dim tsFolder
Dim Scheduler
Dim nPath
Dim execStatus
' Get the test set tree manager from the test set factory
'tdc is the global TDConnection object.
Set TSetFact = otdc.TestSetFactory


Set tsTreeMgr = otdc.TestSetTreeManager


' Get the test set folder passed as an argument to the example code


nPath = "Root\" & Trim(tsFolderName)


Set tsFolder = tsTreeMgr.NodeByPath(nPath)


If tsFolder Is Nothing Then


err.Raise vbObjectError + 1, "RunTestSet", "Could not find folder " & nPath


End If



' Search for the test set passed as an argument to the example code



Set tsList = tsFolder.FindTestSets(tSetName)



If tsList Is Nothing Then


err.Raise vbObjectError + 1, "RunTestSet", "Could not find test set in the " & nPath


End If



If tsList.Count > 1 Then


MsgBox "FindTestSets found more than one test set: refine search"


Exit Sub


ElseIf tsList.Count < 1 Then

MsgBox "FindTestSets: test set not found"


Exit Sub


End If


Set theTestSet = tsList.Item(1)


Debug.Print theTestSet.ID


'Start the scheduler on the local machine


Set Scheduler = theTestSet.StartExecution(HostName)



'msgbox "pass"


'Set up for the run depending on where the test instances


' are to execute.




Select Case runWhere


Case "RUN_LOCAL"


'Run all tests on the local machine


Scheduler.RunAllLocally = True


Case "RUN_REMOTE"



'Set Scheduler = theTestSet.StartExecution(HostName)


'Run tests on a specified remote machine


Scheduler.TdHostName = HostName


'Scheduler.TdHostName=runWhere


' RunAllLocally must not be set for


' remote invocation of tests.


' Do not do this:


' Scheduler.RunAllLocally = False


Case "RUN_PLANNED_HOST"


'Run on the hosts as planned in the test set


Dim TSTestFact, TestList


Dim tsFilter


Dim TSTst


'Get the test instances from the test set


Set TSTestFact = theTestSet.TSTestFactory


Set tsFilter = TSTestFact.Filter


tsFilter.Filter("TC_CYCLE_ID") = theTestSet.ID


Set TestList = TSTestFact.NewList(tsFilter.Text)


Scheduler.RunAllLocally = False


End Select



'Run the tests


Scheduler.run



Set execStatus = Scheduler.ExecutionStatus



While (RunFinished = False)


execStatus.RefreshExecStatusInfo "all", True


RunFinished = execStatus.Finished


Wend


End Sub




'================================




Const qcHostName = "GiveQChost:8080"


Const qcDomain = "GiveDomain name"


Const qcProject = "GiveProject" 'Please define here the name of the project


Const qcUser = "User ID" 'Please define here the username



Const qcPassword = "Give Password HGBGH%3&42" 'Please define here the password


Dim tdc


Dim qcServer


Dim objArgs


Dim strArg


Dim strTestSet


Dim bRunCode



'======GETTING ARGUMENTS==============


set objArgs = WScript.Arguments


If WScript.Arguments.Count<1>2 Then


WScript.Echo "Remote_Scheduler"


bRunCode = False


Else


For Each strArg in objArgs


WScript.Echo strArg&" is starting…"


strTestSet = strArg


bRunCode = True


Next


End If


'===========================================================




If bRunCode Then


qcServer = "http://" & qcHostName


qcServer = qcServer & "/qcbin"


Set tdc = CreateObject("tdapiole80.tdconnection")




If (tdc Is Nothing) Then


MsgBox "tdc object is empty"


End If


tdc.InitConnectionEx qcServer


tdc.Login qcUser, qcPassword


tdc.Connect qcDomain, qcProject




RunTestSet tdc, "GiveFolder Name of Test Set","GiveTestSet name ","Givemachinename", "RUN_REMOTE"


'Disconnect from the project


If tdc.Connected Then


tdc.Disconnect


End If


'Log off the server


If tdc.LoggedIn Then


tdc.Logout


End If


'Release the TDConnection object.


tdc.ReleaseConnection


'"Check status (For illustrative purposes.)


Set tdc = Nothing


End IF

Friday

QTP - Thing to know , things of practical learning, small things but worth to spent time with

This post will update whenever I will find anything new/Intresting or just to remember or to know (n FAQ) :

Q1. what is OTA API in QC
The Open Test Architecture API is a COM library that enables you to:
nintegrate external applications with Quality Center
ninteract with the Quality Center application without having to use the GUI front-end
ninteract with the QC databases bypassing DBA

More Info: connectedtesting.com/Presentations/QC%20OTA%20presentation.ppt

QTP/QC - Running bat file from QC

Povide the bat file 'Driverbat.bat' remove ' sign and put bat under folder C:\QTP_Resource.
'cd C:\QTP_Resource
'DriverScript_QTP
'C:\QTP_Resource\Utility2.exe auto some other utility
'Exit

In QC > Create a VAPI-XP test > in test script put the code

Dim objShell


Set objShell = CreateObject("WScript.Shell")


'objShell.Run "CMD.exe"



objShell .Run "%comspec% /k c: & cd C:\QTP_Resource Driverbat.bat"
BR>
'objShell.Run "%comspec% /k c: & cd C:\QTP_Resource\\Driverbat.bat"
BR>
'qtAppWin.sendkeys "~"

QTP - Creating a XML file (Output.xml) by FileSystemObject

Call createLog


Call OpenBrowser


Call TestCase1


Call TestCase2


Call TestCase3


Call CloseLog


Function CreateLog()


Dim TRFSO, MyTRFile,TRFileName


TRFileName = "C:\Automation\Result\Output.xml"


Set TRFSO=CreateObject("Scripting.FileSystemObject")


Set MyTRFile = TRFSO.CreateTextFile(TRFileName)


MyTRFile.WriteLine("")


MyTRFile.WriteLine("")


MyTRFile.close


Set TRFSO=Nothing


End Function




Function CloseLog()


Dim TRFSO, MyTRFile,TRFileName


TRFileName = "C:\Automation\Result\Output.xml"


Set TRFSO=CreateObject("Scripting.FileSystemObject")


Set MyTRFile=TRFSO.OpenTextFile(TRFileName,8,True)


MyTRFile.WriteLine("
")


MyTRFile.close


Set TRFSO=Nothing


End Function




Function WriteLog(Msg)


Dim TRFSO, MyTRFile,TRFileName


TRFileName = "C:\Automation\Result\Output.xml"


Set TRFSO=CreateObject("Scripting.FileSystemObject")


Set MyTRFile=TRFSO.OpenTextFile(TRFileName,8,True)


MyTRFile.WriteLine(Msg)


MyTRFile.close


Set TRFSO=Nothing


End Function




Function OpenBrowser()


SystemUtil.Run "C:\Program Files\Internet Explorer\IEXPLORE.EXE"


'Rerecord SystemUtil step


Browser("X").Page("Y").Sync


Browser("X").Page("Y").Sync.Navigate "http://www.Someapp.com"


End Function




Function TestCase1()


On Error Resume Next


Browser("X").Page("Y").WebEdit("Z").Set "Set Some TestCase"


'do something more and the write in log to post in output.xml


If Err.Number <> 0 Then


WriteLog("Failed")


Else


WriteLog("Passed")


End If


End Function




Function TestCase2()


On Error Resume Next


Browser("X").Page("Y").WebEdit("Z").Set "Set Some TestCase"


'do something more and the write in log to post in output.xml


If Err.Number <> 0 Then


WriteLog("Failed")


Else


WriteLog("Passed")


End If


End Function




Function TestCase3()


On Error Resume Next


Browser("X").Page("Y").WebEdit("Z").Set "Set Some TestCase"


'do something more and the write in log to post in output.xml


If Err.Number <> 0 Then


WriteLog("Failed")


Else


WriteLog("Passed")


wait(2)


if Browser("X").Exist Then


Browser("X").close


End If


End If


End Function

Tuesday

QTP- Execute the bat file , dos command from QTP

dim appset app=createobject("wscript.shell")
SystemUtil.run "cmd.exe"
app.sendkeys "cd C:\Documents and Settings\'<>\"
app.sendkeys "~"
app.sendkeys "MyDosFile.bat"
app.sendkeys "~" app.sendkeys "~"
wait(5)

'In bat file you can give and save
Date
echo "hello World"
pause

Monday

QTP - Blocking/Unblocking the pop-Up by QTP

Sometime the Pop-ups create problem while running the script (Browser pop-ups). To overcome this problem we can manipulate Registry key.
QTP can use this function.

'Create the shell object
Set WshShell = CreateObject("WScript.Shell")

'Path to edit in registry
popupKeyPath = "HKCU\Software\Microsoft\Internet Explorer\New Windows\PopupMgr"

'Disable the IE pop-up blocker
WshShell.RegWrite popupKeyPath, "no", "REG_SZ”

‘Enable the IE pop-up blocker
WshShell.RegWrite popupKeyPath, "yes", "REG_SZ”