Monday, 19 August 2013

What is the QuickTest Automation Object Model?

You can use the QuickTest Professional Automation Object Model to write programs that automate your QuickTest operations. The QuickTest Automation Object Model provides objects, methods, and properties that enable you to control QuickTest from another application.
The new QuickTest Professional Automation Object Model enables you to automate test management.
You can now control virtually every QuickTest feature and capability using the objects, methods and properties included in the QuickTest Professional Automation Object Model. Automation scripts make it easy to perform any QuickTest operation multiple times in multiple tests without having to open the QuickTest application, for example,
You can write a script that modifies the test object description properties in the Object Identification dialog box and performs an update run on all tests in a specified file folder.
After installing a new add-in, an automation script can associate this add-in to all relevant tests.
You can write an automation script to run a selected batch of tests. For each test, you can retrieve the associated add-ins list. Then, if the necessary add-ins are not already loaded, you can close QuickTest, load the necessary add-ins, reopen QuickTest, and run the test.
You can define your settings for a test in QuickTest, then click “Generate Script” in the Generate tab of the Test Settings dialog box to generate an automation script based on the current test settings. You can then apply those same settings automatically to multiple tests using the whole automation script or excerpts from the generated file.
Example:
You can create and run an automation program from Microsoft Visual Basic that loads the required add-ins for a test, starts QuickTest in visible or minimized mode, opens the test, configures settings that correspond to those in the Options, Test Settings, and Record and Run Settings dialog boxes, runs the test, and saves the test.
Creating automation programs:
The Properties tab of the Test Settings dialog box, the General tab of the Options dialog box, and the Object Identification dialog box each contain a “Generate Script” button. Clicking this button generates a automation script file (.vbs) containing the current settings from the corresponding dialog box.
You can run the generated script as is to open QuickTest with the exact configuration of the QuickTest application that generated the script, or you can copy and paste selected lines from the generated files into your own automation script.
Generating an automation script for QuickTest Professional options:
1. Go to Tools -> Options.
2. Select the General tab.
3. Click .
4. Save the script to the desired location.
5. Click to close the Options dialog.
Generating an automation script for test settings:
1. Go to Test -> Settings.
2. Select the Properties tab.
3. Click .
4. Save the script to the desired location.
5. Click to close the Test Settings dialog.
Generating an automation script for object identification settings:
1. Go to Tools -> Object Identification.
2. Click .
3. Save the script to the desired location.
4. Click to close the Object Identification dialog.
The QuickTest Automation Object Model Reference file is a help file that provides detailed descriptions, syntax information, and examples for the objects, methods, and properties in the QuickTest Automation Object Model.
When QuickTest Professional 9.2 is installed you can open the QuickTest Automation Object Model Reference from:
The QuickTest program folder (Start -> Programs -> QuickTest Professional -> Documentation -> Automation Object Model Reference)
The QuickTest Help menu (Help -> QuickTest Automation Object Model Reference)

When and Why to use Descriptive programming?

Below are some of the situations when Descriptive Programming can be considered useful:
1. The objects in the application are dynamic in nature and need special handling to identify the object. The best example would be of clicking a link which changes according to the user of the application, Ex. “Logout ”.
2. When object repository is getting huge due to the no. of objects being added. If the size of Object repository increases too much then it decreases the performance of QTP while recognizing a object.
3. When you don’t want to use object repository at all. Well the first question would be why not Object repository? Consider the following scenario which would help understand why not Object repository Scenario 1: Suppose we have a web application that has not been developed yet. Now QTP for recording the script and adding the objects to repository needs the application to be up, that would mean waiting for the application to be deployed before we can start of with maki! ng QTP scripts. But if we know the descriptions of the objects that will be created then we can still start off with the script writing for testing Scenario 2: Suppose an application has 3 navigation buttons on each and every page. Let the buttons be “Cancel”, “Back” and “Next”. Now recording action on these buttons would add 3 objects per page in the repository. For a 10 page flow this would mean 30 objects which could have been represented just by using 3 objects. So instead of adding these 30 objects to the repository we can just write 3 descriptions for the object and use it on any page.
4. Modification to a test case is needed but the Object repository for the same is Read only or in shared mode i.e. changes may affect other scripts as well. 5. When you want to take action on similar type of object i.e. suppo! se we have 20 textboxes on the page and there names are in the form txt_1, txt_2, txt_3 and so on. Now adding all 20 the Object repository would not be a good programming approach. How to use Descriptive programming? There are two ways in which descriptive programming can be used
1. By creating properties collection object for the description.
2. By giving the description in form of the string arguments.
1. By creating properties collection object for the description. To use this method you need first to create an empty description Dim obj_Desc ‘Not necessary to declare
Set obj_Desc = Description.Create
Now we have a blank description in “obj_Desc”.
Each description has 3 properties “Name”, “Value” and “Regular Expression”.
obj_Desc(“html tag”).value= “INPUT”
When you use a property name for the first time the property is added to the collection and when you use it again the property is modified. By default each property that is defined is a regular expression. Suppose if we have the following
description obj_Desc(“html tag”).value= “INPUT” obj_Desc(“name”).value= “txt.*”
This would mean an object with html tag as INPUT and name starting with txt. Now actually that “.*” was considered as regular expression. So, if you! want the property “name” not to be recognized as a regular expression then you need to set the “regularexpression” property as FALSE
obj_Desc(“html tag”).value= “INPUT”
obj_Desc(“name”).value= “txt.*” o
bj_Desc(“name”).regularexpression= “txt.*”
This is how of we create a description. Now below is the way we can use it
Browser(“Browser”).Page(“Page”).WebEdit(obj_Desc).set “Test”
When we say .WebEdit(obj_Desc) we define one more property for our description that was not earlier defined that is it’s a text box (because QTPs WebEdit boxes map to text boxes in a web page). If we know that we have more than 1 element with same description on the page then we must define “index” property for the that description Consider the HTML code given below
!
Now the html code has two objects with same description. So distinguish between these 2 objects we will use the “index” property.
Here is the description for both the object
For 1st textbox:
obj_Desc(“! html tag”).value= “INPUT”
obj_Desc(“name”).value= “txt_Name” obj_Desc(“index”).value= “0”
For 2nd textbox:
obj_Desc(“html tag”).value= “INPUT”
obj_Desc(“name”).value= “txt_Name”
obj_Desc(“index”).value= “1”
! Consider the HTML Code given below:
We can use the same description for both the objects and still distinguish between both of them
obj_Desc(“html tag”).value= “INPUT”
obj_Desc(“name”).value= “txt_Name”
When I want to refer to the textbox then I! will use the inside a WebEdit object and to refer to the radio button I will use the description object with the WebRadioGroup object.
Browser(“Browser”).Page(“Page”).WebEdit(obj_Desc).set “Test” ‘
Refers to the text box Browser(“Browser”).Page(“Page”).WebRadioGroup(obj_Desc).set “Test” ‘
Refers to the radio button But if we use WebElement object for the description then we must define the “index” property because for a webelement the current description would return two objects. Hierarchy of test description: When using programmatic descriptions from a specific point within a test object hierarchy, you must continue to use programmatic descriptions from that point onward within the same statement. If you specify a test object by its object repository name after other objects in the hierarchy have been described using programmatic descriptions, QuickTest cannot identify the object.
For example, you can use Browser(Desc1).Page(Desc1).Link(desc3),
since it uses programmatic descriptions throughout the entire test object hierarchy. You can also use
Browser(“Index”).Page(Desc1).Link(desc3),
since it uses programmatic descriptions from a certain point in the description (starting from the Page object description). However, you cannot use
Browser(Desc1).Page(Desc1).Link(“Example1″),
since it uses programmatic descriptions for the Browser and Page objects but then attempts to use an object repository name for the Link test object (QuickTest tries to locate the Link object based on its name, but cannot locate it in the repository because the parent objects were specified using programmatic descriptions).
Getting Child Object: We can use description object to get all the objects on the page that matches that specific description. Suppose we have to check all the checkboxes present on a web page. So we will first create an object description for a checkboxe and then get all the checkboxes from the page
Dim obj_ChkDesc
Set obj_ChkDesc=Description.Create
obj_ChkDesc(“html tag”).value = “INPUT”
obj_ChkDesc(“type”).value = “checkbox”
Dim allCheckboxes, singleCheckBox
Set allCheckboxes = Browse(“Browser”).Page(“Page”).ChildObjects(obj_ChkDesc)
For each singleCheckBox in allCheckboxes singleCheckBox.Set “ON” Next
The above code will check all the check boxes present on the page.!
To get all the child objects we need to specify an object description i.e. we can’t use the string arguments that will be discussed later in the 2nd way of using the programming description. Possible Operation on Description Object Consider the below code for all the solutions
Dim obj_ChkDesc
Set obj_ChkDesc=Description.Create
obj_ChkDesc(“html tag”).value = “INPUT”
obj_ChkDesc(“type”).value = “checkbox”
Q: How to get the no. of description defined in a collection
A: obj_ChkDesc.Count ‘Will return 2 in our case
Q: How to remove a description from the collection
A: obj_ChkDesc.remove “html tag” ‘would delete the html tag property from the collection
Q: How do I check if property exists or not in the collection? !
A: The answer is that it’s not possible. Because whenever we try to access a property which is not defined its automatically added to the collection. The only way to determine is to check its value that is use a if statement “
if obj_ChkDesc(“html tag”).value = empty then”.
Q: How to browse through all the properties of a properties collection?
A: Two ways 1st:
For each desc in obj_ChkDesc
Name=desc.Name
Value=desc.Value R
E = desc.regularexpression
Next
2nd:
For i=0 to obj_ChkDesc.count – 1
Name= obj_ChkDesc(i).Name
Value= obj_ChkDesc(i).Value
RE = obj_ChkDesc(i).regularexpression
Next
2. By giving the description in form of the string arguments. You can describe an object directly in a statement by specifying property:=value pairs describing the object instead of specifying an object’s name.
The general syntax is:
TestObject(“PropertyName1:=PropertyValue1″, “…” , “PropertyNameX:=PropertyValueX”)
TestObject—the test object class could be WebEdit, WebRadioGroup etc….
PropertyName:=PropertyValue—the test object property and its value.
Each property:=value pair should be separated by commas and quotation marks. Note that you can enter a variable name as the property value if you want to find an object based on property values you retrieve during a run session. Consider the HTML Code given below:
Now to refer to the textbox the statement would be as given below
Browser(“Browser”).Page(“Page”).WebEdit(“Name:=txt_Name”,”html tag:=INPUT”).set “Test”
And to refer to the radio button the statement would be as given below
Browser(“Browser”).Page(“Page”).WebRadioGroup(“Name:=txt_Name”,”html tag:=INPUT”).set “Test”
If we refer to them as a web element then we will have to distinguish between the 2 using the index property Browser(“Browser”).Page(“Page”).WebElement(“Name:=txt_Name”,”html tag:=INPUT”,”Index:=0”).set “Test” ‘
Refers to the textbox
Browser(“Browser”).Page(“Page”).WebElement(“Name:=txt_Name”,”html tag:=INPUT”,”Index:=1”).set “Test” ‘
Reference: . “Mercury QuickTest Professional, User’s Guide, Version 8.0.1”

How to send a key command to a Web object

Some Web objects will perform actions when certain key commands, such as ALT+RETURN, are entered. These Web objects do not have a type method associated with them that can be used to replay these keys. How can the key combination be replayed?
——————————————————————————–
Solution: Use the Windows Scripting SendKeys method
1. Create a WScript.Shell object.
2. Activate the browser in which you want to execute the keys.
3. Use the SendKeys method to type the key combination.
Example:
‘ This code executes the CTRL+F key combination (search) on a browser.
Set WshShell = CreateObject(“WScript.Shell”)
WshShell.AppActivate “Put the label of the browser” ‘ Activate the browser window
wait(3)
WshShell.SendKeys “^f” ‘ The caret (^) represents the CTRL key.
wait(2)
object.SendKeys(string)
object
A WshShell object.
string
The string value indicating the keystroke(s) you want to send.
The SendKeys method will send keystroke(s) to the active window. To send a single character (for example, x), use “x” as the string argument. To send multiple characters (for example, abc), use “abc” as the string argument. You can also send special characters such as SHIFT, CTRL, and ALT, which are represented by the plus sign (+), the caret (^), and the percent sign (%), respectively.
For more information on the SendKeys method and other special keys, such as the backspace or a function key, please refer to the MSDN SendKeys Method page.
‘ Name: KeyboardShortCut

‘ Input:
‘ ByRef Key As Variant – The keyboard key to be typed.
‘ ByRef Pane As Variant – The window/pane to type in.
‘ Purpose: To type keyboard input onto the open window.
‘=======================================================================
Function KeyboardShortCut (Key)
dim obj
Set obj = Window(“Window Name” ).WinObject(“Object”)
obj.type Key
Environment(“LastResult”) = “Success”
End Function
Solution2: Pressing Function keys should be recorded automatically
Directly recording on the application and pressing the Function Keys (F1, F2, etc.) should generate code for replay.
Example:
‘Here is an example recorded against Notepad:
Window(“Notepad”).Activate
Window(“Notepad”).WinEditor(“Edit”).Type micF5
If the above does not work, then you can use DeviceReplay to simulate pressing keyboard keys.
Example:
‘Here is an example that does the same thing above, but used DeviceReplay.
Set obj = CreateObject(“Mercury.DeviceReplay”)
Window(“Notepad”).Activate
obj.PressKey 63
Note:
The PressKey method uses the appropriate ASCII or IBM Scan Code value for the key. “63″ is the IBM Scan Code value for F5.

How to compare 2 text files

The following function can be used to check and compare the content of the two files.
Note:
This function is not part of QuickTest Professional/Astra QuickTest. It is not guaranteed to work and is not supported by Mercury Interactive Technical Support. You are responsible for any and all modifications that may be required.
CompareFiles (FilePath1, FilePath2)
FilePath1
The path to the first file to compare
FilePath2
The path to the second file to compare
Public Function CompareFiles (FilePath1, FilePath2)
Dim FS, File1, File2
Set FS = CreateObject(“Scripting.FileSystemObject”)
If FS.GetFile(FilePath1).Size FS.GetFile(FilePath2).Size Then
CompareFiles = True
Exit Function
End If
Set File1 = FS.GetFile(FilePath1).OpenAsTextStream(1, 0)
Set File2 = FS.GetFile(FilePath2).OpenAsTextStream(1, 0)
CompareFiles = False
Do While File1.AtEndOfStream = False
Str1 = File1.Read(1000)
Str2 = File2.Read(1000)
CompareFiles = StrComp(Str1, Str2, 0)
If CompareFiles 0 Then
CompareFiles = True
Exit Do
End If
Loop
File1.Close()
File2.Close()
End Function
Return value:
The function returns 0 or False if the two files are identical, otherwise True.
Example:
File1 = “C:\countries\apple1.jpg”
File2 = “C:\countries\apple3.jpg”
If CompareFiles(File1, File2) = False Then
MsgBox “Files are identical.”
Else
MsgBox “Files are different.”
End If

QTP frequently asked question

1) How to Stop the execution of the test.
Syntax
Services.Abort 2)How to add folders
Sub AddNewFolder(path, folderName) Dim fso, f, fc, nf
Set fso = CreateObject(”Scripting.FileSystemObject”)
Set f = fso.GetFolder(path)
Set fc = f.SubFolders
If folderName “” Then
Set nf =
fc.Add(folderName)
Else
Set nf = fc.Add(”New Folder”)
End If End Sub
An error occurs if the folderName already exists.
3 )How to get the ASCII valuse for a character
Remarks
In the following example, Asc returns the ANSI character code of the first letter of each string:
Dim MyNumber MyNumber = Asc(”A”)
‘ Returns 65.
MyNumber = Asc(”a”)
‘ Returns 97.
MyNumber = Asc(”Apple”) ‘ Returns 65.Note The AscB function is used with byte data contained in a string. Instead of returning the character code for the first character, AscB returns the first byte. AscW is provided for 32-bit platforms that use Unicode characters. It returns the Unicode (wide) character code, thereby avoiding the conversion from Unicode to ANSI.
4) How to get Character fron ASCII value
Remarks
Numbers from 0 to 31 are the same as standard, nonprintable ASCII codes. For example, Chr(10) returns a linefeed character.
The following example uses the Chr function to return the character associated with the specified character code:
Dim MyChar
MyChar = Chr(65) ‘ Returns A.
MyChar = Chr(97) ‘ Returns a.
MyChar = Chr(62) ‘ Returns >. MyChar = Chr(37) ‘ Returns %.Note The ChrB function is used with byte data contained in a string. Instead of returning a character, which may be one or two bytes, ChrB always returns a single byte. ChrW is provided for 32-bit platforms that use Unicode characters. Its argument is a Unicode (wide) character code, thereby avoiding the conversion from ANSI to Unicode.
5)Accessing User-Defined Properties of Web Objects
You can use the attribute/ notation to access native properties of Web objects and use these properties to identify such objects with programmatic descriptions. For example, suppose a Web page has the same company logo image in two places on the page:   You could identify the image that you want to click using a programmatic description by including the user-defined property LogoID in the description as follows: Browser(“Mercury Tours”).Page(“Find Flights”).Image(“src:=logo.gif”,”attribute/LogoID:=123″).Click 68, 12 Note: The attribute/ notation is not supported in Netscape 4.x.
6)How to Run Your Automation Program
There are several applications available for running automation programs. You can also run automation programs from command line using Microsoft’s Windows Script Host. For example, you could use the following command line to run your automation program: WScript.exe /E:VBSCRIPT myScript.vbs 7) What is the other way of making coments instead of astrik(‘)
Ans: “Hello” : Rem Comment after a statement separated by a colon.
8)How to run commands in command prompt from qtpAns:You can run standard DOS commands in your QuickTest test using the VBScript Windows Scripting Host Shell object (WSCript.shell). For example, you can open a DOS command window, change the path to C:\, and execute the DIR command using the following statements: Dim oShell Set oShell = CreateObject (“WSCript.shell”) oShell.run “cmd /K CD C:\ & Dir” Set oShell = Nothing 9) what is optional stepAns:
Description
Causes a step to be bypassed if an object in that step is not found.
Syntax
OptionalStep.StatementToMakeOptional
Example
The following example uses the OptionalStep object to make the Paris selection from the Depart WebList an optional step. OptionalStep.Browser(“Mercury Tours”).Page(“Find Flights”).WebList(“depart”).Select “Paris” 10)What is the alternative way to getROpropertyAns) object.QueryValue(property to get)11)How to get the number of childs in a tree a) Object.children.lengthex: Browser(“xxx”).Page(“xxx”).Image(“xxx”).Object.children.length11)How to know that the table has no recordsa) rs.RecordCount 12)What are the types of parameters available in QTPa) 3 types 1) Action Parameters(Input ,output)These parameters are restricted to that action only.i.e we can not use them for another action.Note on’t think that the output action parameter can pass values between action.The only purpouse with output action parameter is to assign the values at run time rather than design time from the external source(application)Advantage: These parameters are bound to the action even this action is called from another test.How to Declare: Using Action property tabSyntax: Input parameter —variable= parameter(“name”) Output parameter—Parameter(“name”)=value 2) Test Parameters(Input ,output)These parameters are not restricted to that action i.e we can use them for any nother action.Note These parameters are not found and unable to use them in any action is called from another test.Advantage: test parameters can pass values between action.How to Declare: Using Test Setting-à parameters tabSyntax: Input parameters— variable= TestArgs (“name”)Output parameters– TestArgs (“name”)=value3)Local ParametersThese are also same as test parameters but we need to Declare and use them from code not from IDE.Advantage:These parameters still can used even the actions are called from any testSyntax:Declarationà LocalParameter(“name”)=valueUse: variable= LocalParameter(“name”)13)what are the different ways to delay the execution steps in QTPAns)3 ways1) object.waitProperty ”propertyName”,”value”,”time in milli sec”This is called conditional wait This will pauses the execution as long as the specified value of the property exist in the AUT or specified time out which ever is earlier2) Wait(seconds)This is called unconditional wait.i.e it will wait for the specified time3) Services.ThinkTime 10Same as wait14) How to minimize QTP while runninga) Set qtApp = CreateObject(“QuickTest.Application”)
qtApp.WindowState = “Minimized”
Set qtApp = Nothing