Using VBScript Code In QTP

VBScripting in QTP  we will cover the following :
  1. Description. Create.
  2. Function to check if a DataTable sheet exists or not
  3. Insert Call to Copy
  4. Insert Call to Existing
  5. Action Parameters
  6. Resources
  7. Environment Variables.  

Set oDosW = Description.Create
oDosW("regexpwndtitle").Value = "C:\\Windows\\System32\\cmd\.exe"
oDosW("regexpwndtitle").RegularExpression = False
'Launch the window with title MyPuttyTesting
SystemUtil.Run "cmd", "/K title MyPuttyTesting"
'Launch the window with title MyFTPTesting
SystemUtil.Run "cmd", "/K title MyFTPTesting"
'Uniquely Recognize console application simultaneously without any ordinal identifier
To get the current script name we can use the below line of code
Msgbox Environment("TestName")
'Description: Function to import the all the sheets present in the file
'@FileName - File to import
Function ImportAllSheets(ByVal FileName)
                    Dim oExcel, oBook
                    'Launch excel
                    Set oExcel = GetObject("", "Excel.Application")
                    'Open the file in read only mode
                    Set oBook = oExcel.WorkBooks.Open(FileName,,True)
                    'Enumerate through all the sheets present in the file
                    For each oSheet in oBook.WorkSheets
                                         'Check if a DataTable with current name already exists
                                         If Not IfDataSheetExist(oSheet.Name) Then
                                                             'DataTable cannot be imported if the sheet does not exist
                                                             DataTable.AddSheet oSheet.Name
                                         End If
                                         'Import the sheet
                                         DataTable.ImportSheet FileName, oSheet.Name,oSheet.Name
                    Set oBook = Nothing
                    'Quit Excel
                    Set oExcel = Nothing
End Function
'Function to check if a DataTable sheet exists or not
Function IfDataSheetExist(ByVal SheetName)
                    IfDataSheetExist = True
                    On error resume next
                    Dim oTest
                    Set oTest = DataTable.GetSheet(SheetName)
                    If err.number Then IfDataSheetExist = False
                    On error goto 0
End Function
Smart identification is a algorithm which QTP uses when it is not able to identify a object. 
The algorithm tries to figure out if there is a unique control on the page which matches the some of the properties 
of the failed object.This happens because when QTP SI starts looking for a Logout button there is none, but there is
 only one button on the page. So QTP SI assumes that it is the Logout button which we were looking for.
Setting("DisableReplayUsingAlgorithm") = 1
'or using AOM code
CreateObject("QuickTest.Application").Test.Settings.Run.DisableSmartIdentification = True
  Insert Call to Existing – User to insert call to a re-usable action located within the same test or some other test.  This inserts the call to the existing action. In case the action in present in some other test case then a read only copy of action is inserted.

Insert Call to Copy - Inserts call to an existing re-usable action and creates an editable copy of that action. Actions cannot be deleted from a Test from Expert view. To delete a action one must go to the keyword view and delete the action.

An action call cannot be inserted directly by writing code in Expert View, it has to be added through the GUI first.

Global Data Table – Data table for Test flow

Local data table – Data table for every action

Environment variables are global variables available to all Actions Environment variables are of two types
1.      Built-in
2.      User-Defined
  • Built in environment variables give information about the system and the current test
  • User-defined Environment variables added in the Environment tab of Test Settings are Read-only during the test run
  • Environment variables can be added during runtime also using code
       Environment.Value("OrderNumber") = "ABCDEF"
  • Environment variables can be loaded at run-time from a XML file using the below code
       Environment.LoadFromFile "C:\TestEnvironment.xml"
  • Parameters provide another way of parameterizing the test cases.
  • There are two types of parameters
1.      Test parameters
2.      Action parameters
  • Test parameters can be set in Test->Settings…->Parameters (Tab)
  • Test parameters value can be provided when replaying the test
  • Test arguments can be accessed in the test using TestArgs(””)
Action Parameters
  • Used to pass parameters to Action
  • Output parameters can only be used when Action is being called for a single iteration
  • Ex – RunAction “Login”, oneIteration, “TestUser”, “TestPass”, out
  • A parameter can be accessed using
  • Scripts written in VBScript language can be add as a Resource to the test
  • All code written in the script is available across all Actions
  • A VBScript can also be loaded in an Action by using ExecuteFile function. Ex –
ExecuteFile “C:\Init.vbs”
  • In case of multiple files QTP combines all the files into a single one and executes the code. The files are combine in bottom to top order
·         Only Database and XML checkpoints can be inserted in idle mode.
·         Rest all checkpoints can only be added during Recording or through Active screens.

  • Object Based DP
       Set btnSearch = Description.Create : btnSearch("name").Value = "Search"
       Set brwGoogle = Description.Create : brwGoogle("title").value = "Google"
       Set pgGoogle = Description.Create : pgGoogle("title").value = "Google"

  • Description objects can also be used to get all child objects matching a criterion. Ex –
       Set oDesc = Description.Create
       oDesc("name").Value  = "txt_.*"
       oDesc("name").RegularExpression = True
       Set allMatchingObjects = Browser().Page().ChildObjects(oDesc)
       Msgbox allMatchingObjects.Count
       Msgbox allMatchingObjects(0).GetROProperty("name")
  • By default all property values are considered as regular expression patterns
  • When using string description all regular expression must be used with escape character for literal meaning. Ex - …Link(”text:=Logout \(Tarun\)”).Click
  • DP based Object repository can be created in any file
  • Code can be copied from one script to another without copying the object repository
  • Custom implementation of object is easier. Ex –
       objStrDesc = "Browser(""title:=Test"").Page(""title:=Test"").Link(""text:=Login"")"
       Execute "Set obj = " & objStrDesc 

Post a Comment

Previous Post Next Post