Site Loader
Auckland, New Zealand
1. How to get a property of object in QTP? Ans: There are two types of methods by which one can get the property of an object
  • GetROproperty
  • GetTOproperty
E.g. In our famous search engine Google.com you can see the search button as one shown below. In order to know the button type, we will use the GetROproperty of the object as one shown below the code.
Browser("Google").Page("tectrick - Google “).WebButton("Search").GetROProperty("type").
The type of button is “Submit”. Hence we can uniquely identify the object in a web page. 2. How does run time property can be set to an object? Ans: There are many situations where the object changes dynamically in Applications (Web site mainly) and hence the object stored in Object repository will be different to the object available in Application. In order to set the property for the Object during run-time we use a fascinating method available in QTP, which is SetTOProperty. Say for Example if we are in a situation to change the Type of the above shown Google button as “Submit2” from “Submit” then we can use the code snippet as one shown.
Browser("Google").Page("tectrick - Google “).WebButton("Search").SetTOProperty “type”, “Submit2”
3. How to get a particular value of text using VB Script? Ans: For this we can use many inbuilt Functions available in VB Script, one which is most popularly used is Mid () Function.
Description
Returns a specified number of characters from a string.
Syntax
Mid(stringstart[length]) The Mid function syntax has these arguments:
Part Description
String String expression from which characters are returned. If string contains Null, Null is returned.
Start Character position in string at which the part to be taken begins. If start is greater than the number of characters in string, Mid returns a zero-length string (“”).
Length Number of characters to return. If omitted or if there are fewer than length characters in the text (including the character at start), all characters from the start position to the end of the string are returned.
E.g. If we have a web page with the Screen showing the text as 1-10 of 20 Happens mostly in list screen. If we would like to get the count of the list screen (Here its 20). Then we can use the Mid function to get the count.
Dim Count
Count = Mid ("1 -10 of 20", 9,9)
MsgBox(myString)
Now this will return 20 as the Value. By this way we can get particular value of text. 4.How to format the date and time value? There are many situations while we struck with formatting our Date and Time, in order to get rid of this issue; we have a common function called FormatDateTime () Function.  Description Returns an expression formatted as a date or time.
Syntax
FormatDateTime(Date[,NamedFormat]) The FormatDateTime function syntax has these parts:
Part Description
Date Required. Date expression to be formatted.
NamedFormat Optional. Numeric value that indicates the date/time format used. If omitted, vbGeneralDate is used.
Settings The NamedFormat argument has the following settings:
Constant Value Description
vbGeneralDate 0 Display a date and/or time. If there is a date part, display it as a short date. If there is a time part, display it as a long time. If present, both parts are displayed.
vbLongDate 1 Display a date using the long date format specified in your computer’s regional settings.
vbShortDate 2 Display a date using the short date format specified in your computer’s regional settings.
vbLongTime 3 Display a time using the time format specified in your computer’s regional settings.
vbShortTime 4 Display a time using the 24-hour format (hh:mm).
E.g. If the Application returns both date and time and we needs only date not time with particular format then we can use the following function as one shown.
createdTime = FormatDateTime(“06/05/2009 10:15 AM”,2)
Msgbox(CreatedTime)
Output 06/05/2009 5. How to use Regular Expression? Ans: Regular Expression is a wide Topic and which is out of scope for this blog, but you can find more information from site http://www.regular-expressions.info/. In QTP we use Regular Expression widely to identify a dynamically changing objects. There are Totally 11 special characters used Regular Expressions, they are the opening square bracket [, the backslash , the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening round bracket ( and the closing round bracket ). These special characters are often called “metacharacters”. E.g. If there is any word say 1+1=2, then you can use it as in regular Expression as 1+1=2. Meaning “+” has special meaning, in order to skip the meaning we use “” d matches a single character that is a digit, w matches a “word character” (alphanumeric characters plus underscore), and s matches a white space character (includes tabs and line breaks). The actual characters matched by the shorthand depend on the software you’re using. Usually, non-English letters and numbers are included. Real time Example As discussed in our Question 4, the list screen Value 1-10 of 20, Which will be recognized by QTP until the value in Object Repository exactly matches with AUT (Application Under Test). But here is the Situation were you are adding some data and the Application list screen count changes from 20 to 21, and try to run the same script, OOPs !!!!. The QTP throws Error saying, “No object found in Object repository.” Try to run in Maintenance mode!. Hence we can change the Already recorded value of 1-10 of 20, in Object Repository with Regular Expression, since here 20 is just a digit (Numeric) we can use /d   Now the Object Repository should look like one shown below. How to compare difference in date? Ans: We can use simple DateDiff Function of VBScript and do it right away. DateDiff () Function
Description
Returns the number of intervals between two dates.
Syntax
DateDiff(interval, date1date2 [,firstdayofweek[, firstweekofyear]]) The DateDiff function syntax has these parts:
Part Description
interval Required. String expression that is the interval you want to use to calculate the differences between date1 and date2. See Settings section for values.
date1, date2 Required. Date expressions. Two dates you want to use in the calculation.
firstdayofweek Optional. Constant that specifies the day of the week. If not specified, Sunday is assumed. See Settings section for values.
firstweekofyear Optional. Constant that specifies the first week of the year. If not specified, the first week is assumed to be the week in which January 1 occurs. See Settings section for values.
Settings
The interval argument can have the following values:
Setting Description
yyyy Year
q Quarter
m Month
y Day of year
d Day
w Weekday
ww Week of year
h Hour
n Minute
s Second
E.g. Let take our previous question, question 5 for its FormatDateTime() function and lets check for the Difference in time. Here we are going to check Present date using QTP’s inbuilt method date (Returns Present date) and compare it with date passed in FormatDateTime function. It its true it shows present date else it shows that its not present date.
createdTime = FormatDateTime(“06/05/2009 10:15 AM”,2)
If DateDiff("d",createdTime,date) = 0 Then
Msgbox(“Its Present Date”)
Else
Msgbox(“Its not Todays date, its another day”)
End If
  9. How to pass parameter from one Action to another? Ans : Before passing a parameter from one action to another we should first create a input parameter for the Action which is going to accept a value as input from another Action. Lets take Action 2 is going to get an value from Action 1 as a parameter, then we should first create a input parameter for the Action 2. This can be done by right clicking on the Action à Action Properties à Click on Properties Tab and Click the “+” sign to create a parameter. Now lets say I am going to pass a integer variable from Action 1 to Action 2, hence I can create a Number parameter in the input parameter of Action2. Now the code snippet will look one like this. Action1 ******** a = 10 b= 20 c= a+b RunAction “Action2”, oneIteration,c Action2 ******** d = 10 result = Parameter(“c”) e = result+d msgbox(e) Note: Set C as input Parameter in Action2 10.How to create a procedure and pass argument to it, also explain calling a procedure? Ans: Procedure is a collection of functionality combined to perform a common action. This procedure can be called anywhere within an action and outside the action depending upon its type. A procedure can have an Argument or many argument or even no argument which all depends upon its requirement. Lets create a simple Procedure. Syntax Sub <Procedure Name> (Argument 1,…. <Argument N>) Codings … End Sub Sub Add (a,b) Dim c c = a+b Msgbox(“The value is :” c) End Sub Now to call a Procedure we can use Syntax Call <Procedure_Name> (Value to pass to argument … ) Call Add(10,10) Output The Value is: 20 10.How to create a file and passing data to it using VB Script? Ans: Creating a file using VBScript is done simply by following a code snippet given below. ‘ Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

Call objFSO.CreateTextFile(strFile, True)

Const ForAppending = 8

Set objTextFile = objFSO.OpenTextFile (strFile, ForAppending, True)

' Writes strText every time you run this VBScript

objTextFile.WriteLine("Test Name:," + TestName)

objTextFile.WriteLine("Tested Application:," + TestedApp)

objTextFile.WriteLine("Application Path:," + Webpath)

objTextFile.WriteLine("Login Info:," + Environment("LocalHostName"))

'objTextFile.WriteLine("Start Time:," + CString(Time))

objTextFile.WriteLine("")

objTextFile.WriteLine(strLogText)

objTextFile.Close
12.How to get local working folder of the script in QTP? Ans: Use the given simple code snippet
myDir = Environment("TestDir")
This will reveal the present working directory of the Script. 13.How to traverse from data table rows and columns once imported? Ans: Use a For Loop statement and then use the following code snippet as one given below.
Datatable.ImportSheet “C:myTestData.xls” ,1 , "Global"

For i = 0 to datatable.GetRowCount -1

Required Action to Perform

datatable.SetNextRow

Next
14.How to Create User defined Object for Windows Application and storing it in Object Repository? Ans: Use the following code snippets.
Dim UserDefObj

Dim qtApp

Set RepositoryTo = CreateObject("Mercury.ObjectRepositoryUtil")

Set qtApp = CreateObject("QuickTest.Application")

Set UserDefObj = qtApp.Options.ObjectIdentification("WebElement").CreateUserDefinedObject("AIG")

UserDefObj.OrdinalIdentifier = "location" ' Set the selector type for the created user-defined object

UserDefObj.MandatoryProperties.RemoveAll ' Clear the MandatoryProperties list.

UserDefObj.MandatoryProperties.Add ("innertext")

UserDefObj.EnableSmartIdentification = True
Please let us know if readers require any new code snippets !!! Thanks, Karthik KK

Post Author: Karthik kk

Leave a Reply

Your email address will not be published. Required fields are marked *