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
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.
Returns a specified number of characters from a string.
Mid(string, start[, length])
The Mid function syntax has these arguments:
||String expression from which characters are returned. If string contains Null, Null is returned.
||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 (“”).
||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.
Count = Mid ("1 -10 of 20", 9,9)
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.
Returns an expression formatted as a date or time.
The FormatDateTime function syntax has these parts:
||Required. Date expression to be formatted.
||Optional. Numeric value that indicates the date/time format used. If omitted, vbGeneralDate is used.
The NamedFormat argument has the following settings:
||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.
||Display a date using the long date format specified in your computer’s regional settings.
||Display a date using the short date format specified in your computer’s regional settings.
||Display a time using the time format specified in your computer’s regional settings.
||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)
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”.
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
Returns the number of intervals between two dates.
DateDiff(interval, date1, date2 [,firstdayofweek[, firstweekofyear]])
The DateDiff function syntax has these parts:
||Required. String expression that is the interval you want to use to calculate the differences between date1 and date2. See Settings section for values.
||Required. Date expressions. Two dates you want to use in the calculation.
||Optional. Constant that specifies the day of the week. If not specified, Sunday is assumed. See Settings section for values.
||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.
The interval argument can have the following values:
||Day of year
||Week of year
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”)
Msgbox(“Its not Todays date, its another day”)
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.
a = 10
RunAction “Action2”, oneIteration,c
d = 10
result = Parameter(“c”)
e = result+d
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.
Sub <Procedure Name> (Argument 1,…. <Argument N>)
Sub Add (a,b)
c = a+b
Msgbox(“The value is :” c)
Now to call a Procedure we can use
Call <Procedure_Name> (Value to pass to argument … )
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))
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
14.How to Create User defined Object for Windows Application and storing it in Object Repository?
Ans: Use the following code snippets.
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.EnableSmartIdentification = True
Please let us know if readers require any new code snippets !!!