Changing Script Input Data “On the Fly”
Posted by admin - Jun 3, 2008 Articles, QTips, Yaron Assa 0 0 Views : 398 Receive Updates For This Category
Article Tools
- Print this page
- Add Comment
- Send to Friend
- Last Updated on :
Jul 16, 2011
One of the most frustrating experiences in QTP is watching a very long script run into an error, while you sit there helplessly, without any chance to effect the disappointing outcome. This means that a lot of very long test-runs have to be re-run, only because some of the test data / instructions weren’t accurate enough.
Sometimes QTP’s debugging capabilities are enough to fix the problems, and QTP 9.5 adds the very useful feature of “Maintenance run mode”; but usually we need a little something extra to make the script flow in the right direction.
VBScript offers a possible solution in the form if an InputBox, which allows the script to accept input from the user “on the fly”:
sDefault = "Some data"
sNewData = InputBox("Please enter new data", "Enter Data", sDefault)
'Here we can act upon the new user data
The new data can be input data for a field, identification strings for Descriptive Programming, or ever actual code we can perform using the Execute method. By placing these Input crossroads before critical stages in our script, we can assure we have some degree of influence over our script at real time.
However, the InputBox method has one huge drawback – it not only allows user input, it requires it. This means that if no one is around to input the data, the script will be stuck, forever waiting for an input that will never come. This kind of ruins the whole point of running an automated script.
Luckily, there’s a workaround. The WScript.Shell object has a Popup method, which can disappear after a given length of time. Using this, we can understand if the user want to enter a new input, and only then present the inputbox:
sDefault = "Some Data"
Set oShell = CreateObject("Wscript.Shell") 'Create the object
'Let's find out if the user wan't to input data! This popup will disappear after 5 sec.
iUserSelection = oShell.Popup ("Press OK if you want to enter new data", 5, "Select")
'iUserSelection holds the user's selection. -1 means the popup closed without any user interaction
'So if iUserSelection is anything but -1, we should show the inputbox
If iUserSelection <> -1 Then sNewData = Inputbox ("Please enter new data", "Enter Data", sDefault)
The InputBox and Popup functions have much more options up their sleeve. You can loop InputBox up in QTP’s help, and if you want to know more about WScript.Shell popup method, you can read about it here.


