Pages

Showing posts with label QTP Utility Objects. Show all posts
Showing posts with label QTP Utility Objects. Show all posts

Tuesday, November 13, 2012

Quick Test Professional (QTP) - Utility Objects

QTP provides several utility objects to enhance the power of scripting. Below is a list of all QTP utility objects.

Crypt, DataTable, Description, DTParameter, DTSheet, Environment, Extern, OptionalStep, Parameter, PathFinder, Properties(Collection), QCUtil, Random Number, Recovery, Reporter, Services, Setting, TextUtil, TSLTest, XMLUtil

Only some of the QTP objects are covered here and rest will be covered in other post.


Quick Test Professional (QTP) - RegisterUserFunc Object


This object is used to add new methods to a test object or to override existing ones. The syntax is :

RegisterUserFunc TOClass, Method, FunctionName, SetAsDefault

If we want to override the SetFunction for WebEdit test object then we need to create a new function and then assign it using the RegisterUserFunc object as follows:

'New set method for webEdit objects
Sub NewSet(Object , newValue)
  Object.Set Ucase(newValue)
End Sub

RegisterUserFunc "WebEdit", "Set", "NewSet"
Browser("Browser").Page("Page").WebEdit("Text").Set "Nikhil"

The last statement in our example now calls out NewSet function and changes the input value
to upper case and then assign the value.

If we want to use the NewSet function to handle different types of test objects, then we can use GetTOProperty function to determine which type of object has been passed to the funtion.

If we want define a new Set method for all the objects like "WebEdit", WebCheckBox", WebList", "WebRadioButton" etc, then we can create a function like one shown below:

'Generic set method for various objects
Sub GenericSet(Object, newValue)

  'Get the type of the object 
  sObjectType = Object.GetTOProperty("micclass")

  Select Case sObjectType
    Case "WebEdit", "WebCheckBox"
      WebSet Object, newValue

    Case "WebList", WebRadioButton"
      WebSelect Object.newValue
  End Select
End Sub

WebSet and WebSelect functions used in the code given above are user defined functions that are being used to do operation on specific types of object.

Quick Test Professional (QTP) - SystemUtil Object


This is used to run and close the processes. Below are few example of starting the processes.

'Run internet explorer
SystemUtil.Run "iexplore.exe"

'Run the internet explorer and pass the staring URL
SystemUitl.Run "iexplore.exe", "http://www.yahoo.com"

Below are few examples of closing a process.

'Give # of closed process
numClosed = SystemUtil.CloseProcessByName("explore.exe")

'Title with a regular expression
SystemUtil.CloseProcessByWndTitle "Microsoft Internet Explorer. *", True

'Title without regular expression
SystemUtil.CloseProcessByWndTitle "Notepad"

'Closing a process by window handle
hwnd = Browser("creationtime:=0").GetROProperty("hwnd")
SystemUtil.CloseProcessByHwnd hwnd

'Close process by the process id
SystemUtil.CloseProcessById processID

'Close all processes opened by QTP
SystemUtil.CloseDescendentProcesses

Quick Test Professional (QTP) - WebUtil Object

This provides and undocumented method to delete browser cookies:

'Delete cookies
WebUtil.DeleteCookies

NOTE: The WebUtil object is implicitly available after installing QTP Plus, and it can be explicitly instantiated using the following statement:

'Create the WebUtil object
Set WebUtil = CreateObject("Mercury.GUI_WebUtil")

'Delete cookies and destroy the object
oWebUtil.DeleteCookies
Set oWebUtil = Nothing

Quick Test Professional (QTP) - Setting Object


You can use the Setting object to control how QuickTest run tests by setting and retrieving testing options during a run session.

QuickTest testing options affect how you work with tests. For example, you can set the maximum time that QuickTest allows when loading a Web page, before determining that the URL address cannot be found.



Some options are global and others affect only the current test. After you use a Setting object to set a testing option, the setting remains in effect until it is changed again or until the end of your current QuickTest session. You can also use the Setting object to change a setting for a specific part of a specific test.


Some of the testing options that you can set using the Setting object are also available in the Options dialog box (global options) or the Test Settings dialog box (test specific settings). When you use the Setting object to set these options, the change is reflected in the relevant dialog box. Other test settings can be accessed using only one method, either the relevant dialog box or the Setting object.



This object provides the following properties and method to modify test properties at run-time:
  • Add method
  • Exists method
  • Remove method
  • Item property

The Add method:
This method is used to define user-defined settings to the object:

'Add the setting to QTP
Setting.Add "LogErrors", "Yes"
Msgbox Setting.Item("LogErrors")  'Will return "Yes"

'Since Item is the default property for Setting object, we can also use the following syntax:
Msgbox Setting ("LogErrors")  'Will return "Yes"

The Exists method:
This method is used to check if a setting is currently defined:

'Check if LogErrors setting exists or not
If Setting.Exists("LogErrors") then
  Msgbox Setting("LogErrors")  'Will return "Yes"
Else
  Setting.Add "LogErrors", "Yes"
  Msgbox Setting("LogErrors")  'Will return "Yes"
End If

The Remove method
This method is used to remove user-defined settings from the object:

'Will return False
Msgbox Setting.Exists("LogErrors")
Setting.Add "LogErrors", "Yes"

'Will return True
Msgbox Setting.Exists("LogErrors")

Setting.Remove "LogErrors", "Yes"

'Will return False
Msgbox Setting.Exists("LogErrors")

QTP also provides the following build-in Setting object values:
  • AutomaticLinkRun
  • DefaultLoadTime
  • DefaultTimeOut
  • ReplayType
  • SnapshotReportMode
  • WebTimeOut

Quick Test Professional (QTP) - RandomNumber Object

This object provides a method to get a random number between twp specified values.

'This will randomly assign a number to x between 1-200
x = RandomNumber.value(1, 200)

Since value is the default property for RandomNumber object, we can also use the 
following syntax:

'This will randomly assign a number to x between 1-200
x = RandomNumber(1, 200)


Parameter Options Dialog Box (Random Number)

This dialog box enables you to define settings for a random number parameter.
The image below shows the dialog box that opens when you select to parameterize a checkpoint expected value. The dialog boxes for parameterizing other value types such as argument values, object property values, and output storage locations provide similar options.



Quick Test Professional (QTP) - PathFinder Object


This object can be used to find the absolute path to a file. QTP allows setting folder path in the Tools->Options->Folders (Tab) as shown in the figure and in situations where we want to determine which particular folder a file reside in we can use PathFinder.Locate method. Consider the following example:




Let's say we want find the key.txt file located in the "C:\Setup" folder.
'this will assign "C:\Setup\key.txt" to x
x = PathFinder.Locate("key.txt")

Even if "C:\Support" folder has a "key.txt" file, the Locate method will still return "C:\Setup\key.txt" as the "C:\Setup"folder has higher priority than the "C:\Support" folder.

Quick Test Professional (QTP) - OptionalStep Object


This is used to make a statement optional in situations where a statement might fail. Consider the below statement:

'Click Yes on the security warning dialog box
Browser("Browser").Dialog("Security Warning").WinButton("Yes").Click

This statement could have been recorded where a security warnings were enabled, but then run on another PC who's browser does not have those warning enabled-in that scenario the statement will fail. So when a statement depends on a system state, it should be maed optional using the OptionalStep object:

'Click Yes on the security warning dialog box
'In case the step fails proceed to nect step without failing the status in the test result summary
OptionalStep.Browser("Browser").Dialog("Security Warning").WinButton("Yes").Click


How to Set Optional Steps from Keyword tab
This task describes how to set an optional step. Do one of the following:
In the Keyword View, right-click the step and select Optional Step. The Optional Step icon is added next to the selected step.




Quick Test Professional (QTP) - Crypt Object


This object encrypts strings in a format that the QTP SetSecure function understands. Encrypt is the only method provided by the object.

'Encrypt the string
myVar = Crypt.Encrypt("QTPText")

The above statement will assign myVar a value which looks something like
"456955244855c54535454f5455784456f66446s7r464a64r64kj464b6467h443g3234"

This value produced by Crypt.Encrypt is not constant because encrypting the same string again generates a different encrypting string. Now the question arises that when should this capability be used? Consider a script which is executed on a tester's PC with the results sent to others. Is the script has a step top enter a password:

'Set the text in the WebEdit
Browser("Browser").Page("Page").WebEdit("password").Set "QTP"

Then this step will appear in the result summary and will include the "QTP" value. This could compromise security, so let's replace the above statement with a new one:

'Encrypt and then set the password
sEncryptPassword = Crypt.Encrypt("QTP")
Browser("Browser").Page("Page").WebEdit("password").SetSecure sEncryptPassword

Now the password won't appear in the test result summary as we are using the secure mode of setting the password.

NOTE: We can also create an instance of the crypt object is VBScript using the following code :
Set myCrypt = CreateObject("Mercury.Encrypter")