How to click any button using vbscript - vbscript

I need to launch qtp.exe application using vbscipt, but im not getting the idea like how to click on certain button and how to open a particular test file using vbscript.
Please give me some hint or ideas.

You can do it easily by creating COM object of QTP.
Dim QTPObj,QTPTest
Set QTPObj=CreateObject("QuickTest.Application")
'Check if the application is not already Launched
If Not QTPObj.Launched then
QTPObj.Launch
end if
QTPObj.Visible=True
QTPObj.Open "D:\MyQTPTests\Test1001 1001" 'name of the start up script
Set QTPTest=QTPObj.Test
QTPTest.Run 'Run the Test
QTPTest.Close 'Close the Test
QTPObj.Quit 'Quit the QTP Application

Related

How to execute VBS?

I found how to make this
Download_Example
I have a question about how to make execute VBS in vb6 (VBS haves form3 (from vb6 project .)show)
I made a dialog with Microsoft common dialog control 6.0
CommonDialog1.Filter = "File (*.vbs)|*.txt|All Files (*.*)|*.*"
CommonDialog1.DefaultExt = "vbs"
CommonDialog1.DialogTitle = "Select File"
CommonDialog1.ShowOpen
The FileName property gives you the variable you need to use
A work-around might be just executing the script using Shell.
Shell "wscript.exe c:\myscript.vbs", vbNormalFocus
Shell "wscript.exe " & CommonDialog1.FileName, vbNormalFocus
See Microsoft's wscript documentation.
vbNormalFocus is there to restore focus to your vb6 program. It is optional but you probably want it. See documentation.
Looks like you are trying to run a VBScript from your VB6 app to open a dialog in the VB6 app.
VB6 -> VBScript -> Same VB6
You cannot do this with Shell since it runs the script as a separate process. The Script does not know what Form3 is because it is a component of the VB6 app and would not exist as a separate entity once the app is compiled.
Edit: Looks like what you want to do is possible but with Microsoft Script Control. Here are a few examples. Thank you #GSerg for pointing this out.
This or this might be used as a work-around but I don't think it is the right way to go.
Go back to your requirements. What exactly are you trying to accomplish? There has to be a better way.

Wscript does not run .vbs script due to an excel message box which pops up during refresh process

Task: I am currently working on an automation task via Windows Task Scheduler, so users can have a daily / weekly report.
Issue: I am running .vbs script, from a .bat file. The macro in the .vbs script refreshes all the pivot tables / slicers within the report and saves updated report, then it closes the Excel report application.
During testing I noticed that the .bat file stops at the point of running the refresh because Excel report comes up with the below msgBox message and then requires YES/NO in order to continue thereafter that another msg box asking to click YEs to replace the current file after refresh.
Below are the two msgbox that comes up in Excel:
Question: How do I write the .vbs script to automatically click yes to the msg boxes instead of doing it manually, there is no point of automation if there is any manual work needed. I need a clean smooth automation and I have tried to write the .vbs script in different ways.
the .vbs script below:
'Input Excel File's Full Path
'ExcelFilePath = ***Excel report file path
'Input Module/Macro name within the Excel File
MacroPath = "RefreshReport"
'Create an instance of Excel
Set ExcelApp = CreateObject("Excel.Application")
'Do you want this Excel instance to be visible?
ExcelApp.Visible = True 'or "False"
'Prevent any App Launch Alerts (ie Update External Links)
ExcelApp.DisplayAlerts = False 'or "True"
'Open Excel File
Set wb = ExcelApp.Workbooks.Open(ExcelFilePath)
'Execute Macro Code
ExcelApp.Run MacroPath
'Save Excel File (if applicable)
wb.Save
'Reset Display Alerts Before Closing
ExcelApp.DisplayAlerts = True 'or "False"
'Close Excel File
wb.Close
'End instance of Excel
ExcelApp.Quit
'Leaves an onscreen message!
'MsgBox "Your Automated Task successfully ran at " & TimeValue(Now), vbInformation
Have you tried sending {Enter} in the script to accept the MsgBox?
set shell = createobject("wscript.shell")
shell.sendkeys ("{Enter}")
https://ss64.com/vb/sendkeys.html

exploring avaliable commands in an applescript program

I have an application and I can do the following command on (I know this because I googled for it):
tell app "TextMate" to reload bundles
What I would really like is to be able to ask the "TextMate" program:
tell app "TextMate" to list all commands
and have it list out all the things I can ask it to do:
... 'reload bundles', 'exit', 'open files'...
is there a way to do that with applescript?
The way to find all the commands of an app is to open its dictionary in your script editor. Usually "Open Dictionary ... " in the File menu, or drop the application onto the script editor.
[EDIT]
For applications that have AppleScript support, you can actually script opening the app itself with the Script Editor, a la:
set pathToApp to (choose file of type "APPL")
tell application "Script Editor"
open pathToApp
end tell
BUT this will be problematic with a non-scriptable app. You'll get an error, but Script Editor will actually open some part of the app (and it will be slow about it), then give you an unusable document. There's no way to catch this error. If you use the Smile script editor, you can use this method ...
set p to (choose file of type "APPL")
try
OpenDictionary(alias (p as string))
on error e
end try
... to open the dictionary of an app, and if it doesn't work (if the app doesn't have a dictionary), it returns an error but doesn't do anything else (but again, you can't catch the error and not have it complain, without hacking Smile)
[EDIT 2]
A rabbit hole to go down is trying System Events or the Finder to check for boolean of has scripting terminology property of a process, but I don't recommend it because I haven't found it to be reliable.
[EDIT 3]
Ach! I knew there was another method, but forgot what it was. As #mklement0 points out (thank you), you can do this to check for an app's script-ability prior to opening the app in Script Editor:
set pathToApp to (choose file of type "APPL") as text
set isScriptable to false
try
class of application pathToApp
-- only AppleScriptable applications have a class property
set isScriptable to true
end try
isScriptable

Minimize windows, application startup

Is there a way to minimize all windows automatically when launching an application?
I tried to call a .vbs file with the content above:
Set shell = wscript.CreateObject("Shell.Application")
Shell.MinimizeAll
But doing this, application is minimized too.
I'm not sure if this minimizes the windows, but it will probably satisfy your requirement:
set objShell = CreateObject("shell.application")
objShell.ToggleDesktop
This is equivalent to clicking the show desktop button.
Or as just extending your code:
shell.ToggleDesktop
Edit: Nevermind... this does the same thing as your code above. But why not just run this and then display your window? For instance, if it is your app... call the vbs and then display the window. Or if you have no control over the app, call it from a .bat file and run this vbs then your program.
What you want to do is first minimize all applications or toggle the desktop; THEN (perhaps even after a delay) open your application.

How to set focus on file dialog opened in Vbscript

Our Team is automating tests/test data preparation in QTP and we do the scripting in VBScript.
In several tests the tester who runs the script need to supply an MS-Excel file with the indata. We use UserAccounts.CommonDialog for this and it works great. Except for one litle problem, when we run this from QTP the file dialog does not get focus. It's opened in the background and it's easy for the tester that runs the script to miss it and waste time waiting for the dialog.
How do we give the file dialog focus?
Code Example:
Set ObjFSO = CreateObject("UserAccounts.CommonDialog")
ObjFSO.Filter = "MS-Excel Spreadsheets|*.xls|All Files|*.*"
while ObjFSO.ShowOpen = false
msgbox "Script Error: Please select a file!"
wend
msgbox "You selected the file: " & ObjFSO.FileName
My guess is that since the dialog is modal, the ShowOpen method doesn't return the execution control back to the script until the dialog is closed. So there's no way to interact with the dialog as part of your test script.
As a workaround, you could spawn a parallel script that would wait for the dialog and activate it. But I guess QTP cannot run two scripts in parallel, so you'll probably need an external shell script (written in VBScript / JScript / PowerShell / etc).
Edit: Try the following:
Create an external VBScript file (.vbs) with the following contents:
Set oShell = CreateObject("WScript.Shell")
While Not oShell.AppActivate("Open")
WScript.Sleep 500
Wend
This script calls WshShell.AppActivate to activate a window whose title contains Open (replace it with the actual dialog title). If there's no such widnow at the monent, it retries the attempt after 0.5 sec (you can increase the delay if you wish).
Launch this script from your QTP test before opening the dialog. (Use SystemUtil.Run or something like this.)
I'm not sure, but I think this should do the trick.
Did you try recording a click on the dialog - so that QTP will click on it to set focus before proceeding?

Resources