Monday, June 15, 2009
How manys we can write vbscript statements in QTP?
2.Manual writing.
3.Step Generator.
4.Descriptive Programming.
Thursday, June 4, 2009
Working with text filesin QTP ?
There are three ways to create an empty text file (sometimes referred to as a "text stream").
1.The first way is to use the CreateTextFile method. The following example demonstrates how to create a text file using the CreateTextFileMethod method.
Dim fso, f1
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.CreateTextFile("c:\testfile.txt", True)
2.The second way to create a text file is to use the OpenTextFile method of the FileSystemObject object with the ForWriting flag set.
Dim fso, ts
Const ForWriting = 2
Set fso = CreateObject("Scripting. FileSystemObject")
Set ts = fso.OpenTextFile("c:\test.txt", ForWriting, True)
3.A third way to create a text file is to use the OpenAsTextStream method with the ForWriting flag set.
Dim fso, f1, ts
Const ForWriting = 2
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile ("c:\test1.txt")
Set f1 = fso.GetFile("c:\test1.txt")
Set ts = f1.OpenAsTextStream(ForWriting, True)
Adding Data to the File
Once the text file is created, add data to the file using the following three steps:
Open the text file.
Write the data.
Close the file.
To open an existing file, use either the OpenTextFile method of the FileSystemObject object or the OpenAsTextStream method of the File object.
To write data to the open text file, use the Write, WriteLine, or WriteBlankLines methods of the TextStream object, according to the tasks outlined in the following table.
Task
Method
Write data to an open text file without a trailing newline character.
Write
Write data to an open text file with a trailing newline character.
WriteLine
Write one or more blank lines to an open text file.
WriteBlankLines
To close an open file, use the Close method of the TextStream object.
Note The newline character contains a character or characters (depending on the operating system) to advance the cursor to the beginning of the next line (carriage return/line feed). Be aware that the end of some strings may already have such nonprinting characters.
The following example demonstrates how to open a file, use all three write methods to add data to the file, and then close the file:[VBScript]
Sub CreateFile()
Dim fso, tf
Set fso = CreateObject("Scripting.FileSystemObject")
Set tf = fso.CreateTextFile("c:\testfile.txt", True)
' Write a line with a newline character.
tf.WriteLine("Testing 1, 2, 3.")
' Write three newline characters to the file.
tf.WriteBlankLines(3)
' Write a line.
tf.Write ("This is a test.")
tf.Close
End Sub
Read:
' Read the contents of the file. Response.Write "Reading file
Set ts = fso.OpenTextFile("c:\testfile.txt", ForReading)
s = ts.ReadLine
Response.Write "File contents = '" & s & "'"
ts.Close
Moving, Copying, and Deleting Files
The FSO object model has two methods each for moving, copying, and deleting files, as described in the following table.
Task
Method
Move a file
File.Move or FileSystemObject.MoveFile
Copy a file
File.Copy or FileSystemObject.CopyFile
Delete a file
File.Delete or FileSystemObject.DeleteFile
The following example creates a text file in the root directory of drive C, writes some information to it, moves it to a directory called \tmp, makes a copy of it in a directory called \temp, then deletes the copies from both directories.
To run the following example, create directories named \tmp and \temp in the root directory of drive C:
' Get a handle to the file in root of C:\.
Set f2 = fso.GetFile("c:\testfile.txt")
' Move the file to \tmp directory. f2.Move ("c:\tmp\testfile.txt
' Copy the file to \temp.
f2.Copy ("c:\temp\testfile.txt")
' Get handles to files' current location.
Set f2 = fso.GetFile("c:\tmp\testfile.txt")
Set f3 = fso.GetFile("c:\temp\testfile.txt")
' Delete the files.
f2.Delete
f3.Delete
Set ts = fso.OpenTextFile("c:\testfile.txt", ForReading)
While not ts.atEndOfLine
s = ts.ReadLine
msgbox s
Wend
Working with excel files in QTP using "Excel.Application" class?
Set xlWorkBook = xlApp.workbooks.add 'or Set xlbook=xlapp.activeworkbook
Set xlWorkSheet = xlWorkbook.worksheet.add 'or Set worksheet=xlbook.sheets(3)
xlWorkSheet.Range("A1:A10").value = "text" 'Will set values of all 10 rows to "text"
xlWorkSheet.Cells(1,1).value = "Text" 'Will set the value of first row and first col
rowsCount = xlWorkSheet.Evaluate("COUNTA(A:A)") 'Will count the # of rows which have non blank value in the column A
colsCount = xlWorkSheet.Evaluate("COUNTA(1:1)") 'Will count the # of non blank columns in 1st row
xlWorkbook.SaveAs "C:\Test.xls"
xlWorkBook.Close
Set xlWorkSheet = Nothing
Set xlWorkBook = Nothing
Set xlApp = Nothing
Hierarchy of test description ?
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).
you can only start from OR, and move to DP So this will not work:
VBWindow(“title:=notgood”).VBButton(“clickme”).Click
How to use Description Object?
Creates a new, empty description object in which you can add collection of properties and values in order to specify the description object in place of a test object name in a step.
For example, the statements below instruct QuickTest to enter the text: MyName in the first WebEdit object in the Mercury Tours page with the name UserName
Set EditDesc = Description.Create()
EditDesc("Name").Value = "userName"
EditDesc("Index").Value = "0"
Browser("Welcome: Mercury").Page("Welcome: Mercury").WebEdit(EditDesc).Set "MyName"
When working with Properties objects, you can use variable names for the properties or values to generate the object description based on properties or values you retrieve during a run session. You can also create several Properties objects in your test if you want to use programmatic descriptions for several objects.
'Returns the number of Property objects in the Properties collection
PropCount = MyDesc.Count
ex2:
Dim obj_Desc
Set obj_Desc = Description.Create
obj_Desc("name").value = "q"
obj_Desc("class name").value = "web edit"
browser("Google").Page("Google").WebEdit(obj_Desc).Set "hi"
Working with Dictionary Objects
Dim d ' Create a variable.
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens" '
Add some keys and items.
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
msgbox d("a") ' retrieves the Athens
a = d.Keys ' Get the keys
b = d.Items ' Get the items.
For i = 0 To d.Count -1 ' Iterate the array.
s = s & a(i) & space(1) & b(i) & vbnewline ' Create return string.
Next
msgbox s
How do we ensure we can close the correct browser?
Browser("hwnd:=" & Environment("env_BrowserHandle")).Close
Select a Radio Button Option
'has the value of "Two", and, if not, selects the "Two" radio button.
Sub Select_Example2()
CurrentValue = Browser("All types of radio buttons").Page("All types of radio buttons").WebRadioGroup("MYRADIO").GetTOProperty("value")
If CurrentValue <> "Two" Then
Browser("All types of radio buttons").Page("All types of radio buttons").WebRadioGroup("MYRADIO").Select "Two"
End If
end sub
'The following example uses the Select method to select the Window
'item in a Web radio group in the seating preference section of
'the Mercury Tours application.
Sub Select_Example1()
Browser("Mercury Tours").Page("Find Flights").WebRadioGroup("seat pref").Select "Window"
End Sub
'selectin radion buttons from google home page
Browser("MonsterIndia.com - More_2").Page("Google").WebRadioGroup("meta").Select "#0" 'selecting "the web"
Browser("MonsterIndia.com - More_2").Page("Google").WebRadioGroup("meta").Select "cr=countryIN" ' selecting "pages from india"
'some of the inportant properties
items count
selected item index
all items
type :radio
Checking the status of a checkbox
var_ChkStatus=Browser("MonsterIndia.com - More_2").Page("MonsterIndia.com - More").WebCheckBox("ctp").GetROProperty("checked")
msgbox var_ChkStatus '1
Browser("MonsterIndia.com - More_2").Page("MonsterIndia.com - More").WebCheckBox("ctp").Set "ON"
var_ChkStatus=Browser("MonsterIndia.com - More_2").Page("MonsterIndia.com - More").WebCheckBox("ctp").GetROProperty("checked")
msgbox var_ChkStatus '0
'checking for a particular item in the list box
var_SearchItem = "Hyderabad"
var_ItemsCount = Browser("MonsterIndia.com - More").Page("MonsterIndia.com - More").WebList("loc").GetROProperty("items count")
For i =1 to var_ItemsCount
var_Item = Browser("MonsterIndia.com - More").Page("MonsterIndia.com - More").WebList("loc").GetItem(i)
If ucase(var_SearchItem) = ucase(var_Item) Then
msgbox "found"
Exit for
End If
Next
Note:
While selecting an item from a list box the index starts from 0
and while getting an item from a list box the index starts from 1
Browser("MonsterIndia.com - More").Page("MonsterIndia.com - More").WebList("loc").Select i-1
'some of the important properties of the list object
items count
selected item index
all items
selected items count
value : selected item value
How to chek whether an object exists or not?
msgbox var_Exist 'Return true if object exists otherwise false
Adding a Parameter to RunTime DataTable in QTP
MyParam=DataTable.GetSheet ("MySheet").AddParameter("Time", "8:00")
You can also use this to add a parameter to the "MySheet" local sheet (note that no value is returned).
DataTable.GetSheet ("MySheet").AddParameter "Time", "8:00"
myparam = DataTable. LocalSheet.AddParameter("Time", "5.45")
MsgBox myparam 'output is 5.45
Displaying a Sentence( of words) in Reverse Order using QTP VB Scripting
str="I'm QuickTestProfessionall.Do you know me?"
arrStrings = Split(str,space(1))
msgbox ubound(arrStrings)
vstrReverse=empty
For each arrStr in arrStrings
vstrReverse = strReverse(arrStr) & space(1) & vstrReverse
msgbox arrStr'each string separated by a single space
Next
msgbox vstrReverse
or
Public Function ReverseString(ByVal InputString As String) _
As String
Dim lLen As Long, lCtr As Long
Dim sChar As String
Dim sAns As String
lLen = Len(InputString)
For lCtr = lLen To 1 Step -1
sChar = Mid(InputString, lCtr, 1)
sAns = sAns & sChar
Next
ReverseString = sAns
End Function
Disable recognition of vrtual objects while recording?
Tools-> Options -> General
How do we know current iteration value from Environment variable?
msgbox Environment.Value("TestIteration")
Indicates which action iteration is currently running.
Conditional loops int QTP?
Repeats a block of statements while a condition is True or until a condition becomes True.
Do [{While Until} condition]
[statements]
[Exit Do]
[statements]
Loop
Or, you can use this syntax:
Do
[statements]
[Exit Do]
[statements]
Loop [{While Until} condition]
ex:
Do
Loop While False
Do
Loop Until True
While...Wend Statement
Executes a series of statements as long as a given condition is True.
While condition
Version [statements]
Wend
For...Next Statement
Repeats a group of statements a specified number of times.
For counter = start To end [Step step]
[statements]
[Exit For]
[statements]
Next
For Each...Next Statement
Repeats a group of statements for each element in an array or collection.
For Each element In group
[statements]
[Exit For]
[statements]
Next [element]