Facebook

Course Name Start Date Time Duration Registration Link
No Training Programs Scheduled ClickHere to Contact
Please mail To sudhakar@qtpsudhakar.com to Register for any training

Thursday, January 26, 2012

Automation Object Model

Open QTP and Connect to Quality Center
'*************************************************************************************************
'*************************************************************************************************
'Open QTP and Connect to Quality Center
'*************************************************************************************************
'*************************************************************************************************
Dim qtApp ' Declare the Application object variable
Set qtApp = CreateObject("QuickTest.Application") ' Create the Application object
qtApp.Launch ' Start QuickTest
qtApp.Visible = True ' Make the QuickTest application visible

' Make changes in a test on Quality Center with version control
qtApp.TDConnection.Connect "QC URL", "DOMAIN Name", "Project Name", "User Name", "Password", False ' Connect to Quality Center

If qtApp.TDConnection.IsConnected Then ' If connection is successful
 MsgBox "Succesfully connected to Quality Center"
Else
    MsgBox "Cannot connect to Quality Center"
End If

qtApp.Quit ' Exit QuickTest
Set qtApp = Nothing ' Release the Application object
'*************************************************************************************************
'*************************************************************************************************
Start QTP and open New test
'*************************************************************************************************
'*************************************************************************************************
'Start  QTP and open New test
'*************************************************************************************************
'*************************************************************************************************
Dim qtApp ' Declare the application object variable
Set qtApp = CreateObject("QuickTest.Application") ' Create the application object

qtApp.Launch ' Start QuickTest
qtApp.Visible = True ' Make the QuickTest application visible

qtApp.New ' Open a new test

Set qtApp = Nothing ' Release the Application object
'*************************************************************************************************
'*************************************************************************************************
Start QTP, open an existing test and Run the Test
 '*************************************************************************************************
'*************************************************************************************************
'Start  QTP, open an existing test and Run the Test
'*************************************************************************************************
'*************************************************************************************************
Dim qtApp
Dim qtTest

Set qtApp = CreateObject("QuickTest.Application") ' Create the Application object
qtApp.Launch ' Start QuickTest
qtApp.Visible = True ' Make the QuickTest application visible

' Set QuickTest run options
qtApp.Options.Run.ImageCaptureForTestResults = "OnError"
qtApp.Options.Run.RunMode = "Fast"
qtApp.Options.Run.ViewResults = False

qtApp.Open "C:\Tests\Test1", True ' Open the test in read-only mode

' set run settings for the test
Set qtTest = qtApp.Test
qtTest.Settings.Run.OnError = "NextStep" ' Instruct QuickTest to perform next step when error occurs

qtTest.Run ' Run the test

MsgBox qtTest.LastRunResults.Status ' Check the results of the test run
qtTest.Close ' Close the test

Set qtTest = Nothing ' Release the Test object
Set qtApp = Nothing ' Release the Application object
'*************************************************************************************************
'*************************************************************************************************
Start QTP, open an existing test, Run the Test with configured Run and Result options
 '*************************************************************************************************
'*************************************************************************************************
'Start  QTP, open an existing test and Run the Test with configured Run options
' And Store Run Results in Specified Folder
'*************************************************************************************************
'*************************************************************************************************
Dim qtApp
Dim qtTest
Dim qtResultsOpt

Set qtApp = CreateObject("QuickTest.Application") ' Create the Application object
qtApp.Launch ' Start QuickTest
qtApp.Visible = True ' Make the QuickTest application visible

' Set QuickTest run options
qtApp.Options.Run.ImageCaptureForTestResults = "OnError"
qtApp.Options.Run.RunMode = "Fast"
qtApp.Options.Run.ViewResults = False

qtApp.Open "C:\Tests\Test1", True ' Open the test in read-only mode

' set run settings for the test
Set qtTest = qtApp.Test

qtTest.Settings.Run.IterationMode = "rngIterations" ' Run only iterations 2 to 4
qtTest.Settings.Run.StartIteration = 2
qtTest.Settings.Run.EndIteration = 4
qtTest.Settings.Run.OnError = "NextStep" ' Instruct QuickTest to perform next step when error occurs

Set qtResultsOpt = CreateObject("QuickTest.RunResultsOptions") ' Create the Run Results Options object
qtResultsOpt.ResultsLocation = "C:\Tests\Test1\Res1" ' Set the results location

qtTest.Run qtResultsOpt ' Run the test

MsgBox qtTest.LastRunResults.Status ' Check the results of the test run
qtTest.Close ' Close the test

Set qtResultsOpt = Nothing ' Release the Run Results Options object
Set qtTest = Nothing ' Release the Test object
Set qtApp = Nothing ' Release the Application object 
'*************************************************************************************************
'*************************************************************************************************
Start QTP, open an existing test, associate libraries and save the test
'*************************************************************************************************
'*************************************************************************************************
'Start  QTP, open an existing test, associate libraries and save the test
'*************************************************************************************************
'*************************************************************************************************
Dim qtApp
Dim qtLibraries
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 its libraries collection
qtApp.Open "C:\Tests\Test1", False, False ' Open a test
Set qtLibraries = qtApp.Test.Settings.Resources.Libraries ' Get the libraries collection object

' Add Utilities.vbs if it's not in the collection
If qtLibraries.Find("C:\Utilities.vbs") = -1 Then ' If the library cannot be found in the collection
    qtLibraries.Add "C:\Utilities.vbs", 1 ' Add the library to the collection
End If

'Save the test and close QuickTest
qtApp.Test.Save ' Save the test
qtApp.Quit ' Quit QuickTest

Set qtLibraries = Nothing ' Release the test's libraries collection
Set qtApp = Nothing ' Release the Application object
'*************************************************************************************************
'*************************************************************************************************
Start QTP, open an existing test, associate Object Repositories and save the test
'*************************************************************************************************
'*************************************************************************************************
'Start  QTP, open an existing test, associate Object Repositories and save the test
'*************************************************************************************************
'*************************************************************************************************
Dim qtApp
Dim qtRepositories
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

'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
'*************************************************************************************************
'*************************************************************************************************
Open and minimize QTP Window
'*************************************************************************************************
'*************************************************************************************************
'Open and minimize QTP Window
'*************************************************************************************************
'*************************************************************************************************
Dim qtApp
Set qtApp = CreateObject("QuickTest.Application") ' Create the Application object
qtApp.Launch ' Start QuickTest
qtApp.Visible = True ' Make the QuickTest window visible
qtApp.WindowState = "Minimized" ' Maximize the QuickTest window

Set qtApp = Nothing ' Release the Application object
'*************************************************************************************************
'*************************************************************************************************
Start QTP, Open an Existing Test and Define Environment Variables
'*************************************************************************************************
'*************************************************************************************************
'Start QTP, Open an Existing Test and Define Environment Variables
'*************************************************************************************************
'*************************************************************************************************
Dim qtApp

Set qtApp = CreateObject("QuickTest.Application") ' Create the Application object
qtApp.Launch ' Start QuickTest
qtApp.Visible = True ' Make the QuickTest application visible

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

' Set some environment variables
qtApp.Test.Environment.Value("Root") = "C:\"
qtApp.Test.Environment.Value("Password") = "QuickTest"
qtApp.Test.Environment.Value("Days") = 14

qtApp.Test.Save ' Save the test

qtApp.Quit ' Exit QuickTest
Set qtApp = Nothing ' Release the Application object
'*************************************************************************************************
'*************************************************************************************************
Start QTP, Open an Existing Test and Get All Available Action Names From the Test
'*************************************************************************************************
'*************************************************************************************************
'Start QTP, Open an Existing Test and Get All Available Action Names From the Test
'*************************************************************************************************
'*************************************************************************************************
Dim qtApp

' Open QuickTest
Set qtApp = CreateObject("QuickTest.Application") ' Create the Application object
qtApp.Launch ' Launch QuickTest
qtApp.Visible = True ' Set QuickTest to be visible

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

oActCount=qtApp.Test.Actions.Count

For iCounter=1 to oActCount

 ' Get the first action in the test by index (start from 1)
 MsgBox qtApp.Test.Actions(iCounter).Name

Next

'Close QuickTest
qtApp.Quit ' Quit QuickTest

Set qtApp = Nothing ' Release the Application object
'*************************************************************************************************
'*************************************************************************************************
Start QTP with specified views
'*************************************************************************************************
'*************************************************************************************************
'Start QTP with specified views
'*************************************************************************************************
'*************************************************************************************************
Dim qtApp
Set qtApp = CreateObject("QuickTest.Application") ' Create the Application object
qtApp.Launch ' Start QuickTest

qtApp.ActivateView "ExpertView" ' Display the Expert View
qtApp.ShowPaneScreen "ActiveScreen", True ' Display the Active Screen pane
qtApp.ShowPaneScreen "DataTable", False ' Hide the Data Table pane
qtApp.ShowPaneScreen "DebugViewer", True ' Display the Debug Viewer pane

qtApp.WindowState = "Maximized" ' Maximize the QuickTest window
qtApp.Visible = True ' Make the QuickTest window visible

Set qtApp = Nothing ' Release the Application object
'*************************************************************************************************
'*************************************************************************************************
_______________________________________

12 comments :

  1. Hi I got a request for you.Can you please write a code to get test run results of a test set and save it into an excel sheet.?(use OTA)Let me know

    ReplyDelete
  2. You Blog is really superb..... Learner can become an expert with this BLOG..... Keep rocking .....So may i can from where are you?

    ReplyDelete
  3. Sudhakar Reddy KakunuriJune 28, 2009 at 8:33 AM

    Thanks. I am from hyderabad

    ReplyDelete
  4. Thank you so much ....

    ReplyDelete
  5. Is it possible to associate OR to the whole test instead of action by action in a test?

    ReplyDelete
    Replies
    1. By creating Test Shared repository we can.......

      Delete
  6. Very nice work ..I just Amazed..and conformed that VB scripting it so powerful with QTP..
    Thank you so much for this blog..

    ReplyDelete
  7. This is the best blog for QTP Automation objects learning.. Keep up the good work and thank you!

    ReplyDelete
  8. It is good. but i m stuck with my assignment. I have to fetch custom result of QTP test using this QTP automation object model. currently using Test.LastRunError, i m able to fetch last error but i am unable to fetch custom report that we are writtem in QTP Test using Reporter.ReportEvent. Can you please help me?

    ReplyDelete
  9. Thanks for your codes snippet for AOM

    ReplyDelete