WORKING WITH QTP

TestEveryThinG

Launch QTP by VB Script

Posted by rajivkumarnandvani on August 28, 2009

Hi All,

Some time we required to launch QTP via Script . Here i am showing how to launch QTP via VB script. We can use this for load Library file/ Object repository / Recovery  at qtp Startup. Just copy the script and save the file with .vbs extension

open the vbs file with microsoft based script host OR wsh

‘ This function closes all previous instances/processes of QTP one by one
Public Function  fn_CloseApplication( byval sApplicationExe)
Dim strComputer
Dim objWMIService
Dim colProcesses
Dim objProcess
strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set colProcesses = objWMIService.ExecQuery (“Select * from Win32_Process Where Name = ‘”&sApplicationExe&”‘”)
For Each objProcess in colProcesses
objProcess.Terminate()
Next
Set objWMIService = Nothing
Set colProcesses=Nothing
End Function

rem  call  function fn_CloseApplication  for close the instances/processes of QTP

call fn_CloseApplication( “QTPro.exe”)
call fn_CloseApplication( “QTAutomationAgent.exe”)

rem  launch QTP
Set objQtpApp = CreateObject(“QuickTest.Application”)
objQtpApp.Launch
objQtpApp.Visible = True
‘ check the QTP settings
objQtpApp.Test.Settings.Launchers(“Web”).Active = False
objQtpApp.Test.Settings.Launchers(“Web”).Browser = “IE”
objQtpApp.Test.Settings.Launchers(“Web”).Address = “http://newtours.mercury.com
objQtpApp.Test.Settings.Launchers(“Web”).CloseOnExit = True
objQtpApp.Test.Settings.Launchers(“Windows Applications”).Active = False
objQtpApp.Test.Settings.Launchers(“Windows Applications”).Applications.RemoveAll
objQtpApp.Test.Settings.Launchers(“Windows Applications”).RecordOnQTDescendants = True
objQtpApp.Test.Settings.Launchers(“Windows Applications”).RecordOnExplorerDescendants = False
objQtpApp.Test.Settings.Launchers(“Windows Applications”).RecordOnSpecifiedApplications = True
objQtpApp.Test.Settings.Run.IterationMode = “rngAll”
objQtpApp.Test.Settings.Run.StartIteration = 1
objQtpApp.Test.Settings.Run.EndIteration = 1
objQtpApp.Test.Settings.Run.ObjectSyncTimeOut = 20000
objQtpApp.Test.Settings.Run.DisableSmartIdentification = False
objQtpApp.Test.Settings.Run.OnError = “Dialog”
objQtpApp.Test.Settings.Resources.DataTablePath = “<Default>”
objQtpApp.Test.Settings.Resources.Libraries.RemoveAll
objQtpApp.Test.Settings.Resources.Libraries.SetAsDefault
objQtpApp.Test.Settings.Web.BrowserNavigationTimeout = 60000
objQtpApp.Test.Settings.Web.ActiveScreenAccess.UserName = “”
objQtpApp.Test.Settings.Web.ActiveScreenAccess.Password = “”


Advertisements

21 Responses to “Launch QTP by VB Script”

  1. arthi said

    Run time error when the above script is executed

    Microsoft (R) Windows Script Host Version 5.7
    Copyright (C) Microsoft Corporation. All rights reserved.

    C:\Documents and Settings\a\Desktop\ExampleQTPLanch.vbs(14, 20) Microsoft VBScript compilation error: Expected ‘=’

    ***** script completed – exit code: 1 *****

  2. Hi please check this script on another machine and check ur script syntax also for single quote and double quote . copy this script in qtp then replace all double quoate with actual double becoz in blog script double quote something different . First try to run in qtp
    let me know if u face problem again

  3. Maybe you can give me a little assistance, too. I have a vb script that I use to call QTP. The test validates that a series fo URLs exists. This works the first time through but when I execute the script a second time, I receive an error message “checkurl.vbs(8,1) (null): The server threw an exception”
    The script is:
    Dim qtApp ‘As QuickTest.Application
    ‘Declare the Application object variable
    Dim qtTest ‘As QuickTest.Test
    ‘Declare a Test object variable
    Dim qtResultsOpt ‘Declare a Run Results Options object variable
    Set qtApp = CreateObject(“QuickTest.Application”)
    ‘Create the Application object
    qtApp.Launch ‘Start QuickTest
    qtApp.Visible = True ‘Make the QuickTest application visible
    qtApp.Open “C:\JMSC QTP Automation\CheckURL”, True
    ‘Open the test in read-only mode
    ‘set run settings for the test
    Set qtTest = qtApp.Test
    qtTest.Run ‘Run the test
    WScript.StdOut.Write “Status is: ” & qtTest.LastRunResults.Status
    ‘Check the results of the test run
    qtTest.Close ‘Close the test
    qtApp.quit ‘Close QuickTest Pro
    Set qtResultsOpt = Nothing ‘Release the Run Results Options object
    Set qtTest = Nothing ‘Release the Test object
    Set qtApp = Nothing ‘Release the Application object

  4. Hi Frank,
    Thanks for asking.First you make sure that you are able to run the script/test from QTP itself more that one time( means not from launch QTP Vbs). If you able to run from QTP more than one times then you can try this..
    Before launch the QTP make sure all process of QTP must be closed you can do this by close QTP process from task manager there is a script i already created in my blog you will find it use this https://rajivkumarnandvani.wordpress.com/2009/05/03/close-application-process-qtp/
    call fn_CloseApplication( “QTPro.exe”)

    call fn_CloseApplication( “QTAutomationAgent.exe”)
    after run this then launch ur QTP launch Vbs OR you can add this in starting of your VBS
    Note:- In ur script you are not using Set qtResultsOpt = Nothing object so there is no any need of this 🙂
    Best of luck

  5. Nams said

    I am working on a oracle application and Excel sheets. When i execute my code it is giving me a “General RunTime Error”. When I did “msgbox err.number” I got the error number as “-2147417851″. then i did a “DescribeResult(Err.Number)” in debug mode and I got this error message “The server threw an exception.”. I am confused that when its saying about the QTP server or the Oracle apps server.

    How to resolve this problem.

    Please help. Its really urgent for me.

    Thanks in advance

  6. Hi Nams
    i think its application server connection error
    and there is no qtp server first you make sure that oracle addin you added it would be more good if you send me the code so that i can figure out
    best of luck

  7. Nams said

    Set new_obj=OracleFormWindow|Invoices+
    If new_obj.Exist(20) Then
    strValue = PO Default
    Set oDesc = Description.Create()
    oDesc(“micclass”).Value = “OracleTextField”
    oDesc(“description”).Value = “Type”
    Set chld = new_obj.ChildObjects(oDesc) ‘ Object for Type
    Set chld1 = chld.item(count1)
    chld1.setFocus
    chld1.Enter strValue

    This is my code.
    application server connection error means: QTP is not able to connect to oracle server!
    Actuallly I when I an running this my script in a batch i am getting this error but when I am executing only this code, its working fine.

    Please suggest something.

    thanks in advance.

  8. Nams said

    sorry

    strValue= “PO Default”

  9. Hi Nmas,

    The code you posted here i did not seems any connection type script here its just a enter the value in a text box.In which steps QTP throwing the Error?.
    you saying when you running the alone script then u able to run successfully means problem with batch mode not with script how you executing the script in batch mode make sure initial and end condition must be full fill to run script.

  10. Nams said

    Thanks for ur help and quick replies rajiv.

    I gpt the problem and resolved it.

    I more issue:

    I am getting a Invalid procedure call or argument:’Mid’ in the below line.

    FileattName = Mid(FileName,FileattSplit,(Len(FileName)-FileattSplit+1))

    Can you please tell me wat might be the possible reason?

    Thanks in advance.

  11. Hi Nmas,
    Good to know you solved the problem can u share it wt was it.
    I would like to help you but the information you provided me is not complete you mentioned this
    FileattName = Mid(FileName,FileattSplit,(Len(FileName)-FileattSplit+1))
    what the value of parameter like FileName ,FileattName where split is function that split the string into array with specified seprator . you using FileattSplit is it mixed or u forget to space between Fileatt Split please provide the full information what exactly you want to do.
    best of luck

  12. Malik said

    Hello Rajiv,

    I have been looking the solution of the script you mentioned above.

    It’s been long time figuring out the the workaround.

    The problem is that when run the vbs eaither by double clicking or through command line I get the Runtime error, pure virtual function call.

    The script is working fine in windows XP but not in Vista.

    Your help would greatly be appreciated

  13. Hi Malik,

    I do not have vista in my machine right now. What can I suggest you, try to do this, may be this will work.Open the QTP in VISTA go File menu ->> Setting–> Properties Tab You will find Generate script Option. Click that and save the script file in VBS extension. Now close the QTP after that double click on that vbs file.
    If still this does not work Then run that VBS file as administrator.
    1. Make sure VBS debugger is installed ( it will ask initially when u installed QTP)(This can be check if u able to debug the script in QTP means it is installed).
    2.Off the USER CONTROL option follow the following steps:
    Click Start >Control Panel>User Accounts and Family Safety>User Accounts
    and uncheck the Turn User Account Control On or OFF option
    refer this https://rajivkumarnandvani.wordpress.com/2010/04/21/qtp-not-recording-web-application-in-vista/

    Let me know if this work for you or not.

  14. Starter said

    Hi i m getting below error sometimes only.The result folder is not used by any other folder still getiing below error.
    “run-time error -2147352567 (80020009) .Unable to create specified run results folder.”

    what could be the issue and wats the solution?

  15. Hi Nidhi,

    Please tell what script are you executing?Without knowing the script i can’t help.There could be many reasons . It seems script running in looping statement and same time created folder is using in others process like under the same folder you can’t rename the parent folder…
    Please mentioned the script .

  16. Vani said

    Hi

    I am lauching QTP from windows task scheduler by invoking the .vbs file to run the scripts. We are executing the scripts on 6 machines, different OS and browsers.

    We have scheduled the task to execute every one hour in all the machines. But sometime ‘The Server threw an exception’ error is returned at qtpapp.launch line.

    This is observed frequently Windows 7 and Vista machine.

    I’m releasing the objects by using the following at the end of .vbs file.

    Set qtTest = Nothing ‘ Release the Test object
    qtApp.Quit
    Set qtApp = Nothing ‘ Release the Application object

    Can you please help me how this issue can be handled. I have read in some article that we need to check whether QTP.exe process is not active in Task manager before the .vbs file tries to open QTP.

  17. Hi Vani,

    Thanks for reading the blog. Yes before launching the QTP make sure no process of QTP is running. In the post i am already doing this.

    rem call function fn_CloseApplication for close the instances/processes of QTP

    call fn_CloseApplication( “QTPro.exe”)
    call fn_CloseApplication( “QTAutomationAgent.exe”)

  18. Vani said

    Hi Rajiv

    Thank for the reply, i have already tried using the function ‘call fn_CloseApplication( “QTPro.exe”)’.

    Here is the script that i’m using the .vbs

    ‘This function closes all previous instances/processes of QTP one by one
    Public Function fn_CloseApplication( byval sApplicationExe)
    Dim strComputer
    Dim objWMIService
    Dim colProcesses
    Dim objProcess
    strComputer = “.”
    Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)
    Set colProcesses = objWMIService.ExecQuery (“Select * from Win32_Process Where Name = “&sApplicationExe&””)
    For Each objProcess in colProcesses
    objProcess.Terminate()
    Next
    Set objWMIService = Nothing
    Set colProcesses=Nothing
    End Function
    rem call function fn_CloseApplication for close the instances/processes of QTP
    call fn_CloseApplication( “QTPro.exe”)
    call fn_CloseApplication(“QTAutomationAgent.exe”)

    Dim qtApp ‘As QuickTest.Application ‘Declare the Application object variable
    Dim qtTest ‘As QuickTest.Test ‘ Declare a Test object variable
    Set qtApp = CreateObject(“QuickTest.Application”) ‘Create the Application object
    qtApp.Launch ‘Start QuickTest
    qtApp.Visible = True ‘Make the QuickTest application visible
    qtApp.Open “C:\TestAutomation\Scripts\Test_Automation”,True ‘ Open the test in read-only mode
    ‘ set run settings for the test
    Set qtTest = qtApp.Test

    qtApp.Test.Settings.Resources.Libraries.add “C:\Test_Automation\Library\GenericLibrary.vbs” ‘Opening the library files
    qtApp.Test.Settings.Resources.Libraries.add “C:\Test_Automation\Library\BusinessLibrary.vbs”
    qtApp.Test.Settings.Resources.Libraries.add “C:\Test_Automation\Library\DriverLibrary.vbs”

    qtTest.Run ‘ Run the test
    ‘WScript.StdOut.Write “Status is:” & 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
    qtApp.Quit
    Set qtApp = Nothing ‘ Release the Application object

    Receiving the following error.. at line no 10 Error code: 80041017.

    One more question, does the script throw error if there is no QTP agent in process to terminate?

  19. Hi Vani,

    1. One more question, does the script throw error if there is no QTP agent in process to terminate?

    *No it will not throw an error because if its find then only it will terminate else not.we are using loop statement For Each objProcess in colProcesses*

    Which line number means line where you facing the error. also check the *err *.*description* value

    Is it coming to all system or specific to any one??

  20. Anonymous said

    when i am using above code just command prompt opening , I am not able to open qtp.But qtp task is running in Taskmanager. pls suggest me

  21. Anonymous said

    I am able to launch QTP via VB script(excel) as mentioned above and also able perform setting as mentioned. but one thing I am not able to do with this is “Allow Other HP products to Run Test and Components” option (Tools–> Options–>GUI Testing–> Test Runs)

    Please help in this regard.

    Thanks in Advance.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: