Test Conditons: Creating and Calling user define funtion 2b. Go to Tools menu --> General Options |
Blog for QuickTest Pro (QTP) posts for experts and contains frameworks and scripts
Creating and Calling user define funtion
Diff between Image & Bit map Check point
You can create bitmap checkpoints for all supported testing environments (as long as the appropriate add-ins are loaded).
Note: The results of bitmap checkpoints may be affected by factors such as operating system, screen resolution, and color settings.
How many ways we can parameterize data in QTP?
Test, action or component parameters enable you to use values passed from your test or component, or values from other actions in your test.
Data Table parameters enable you to create a data-driven test (or action) that runs several times using the data you supply. In each repetition, or iteration, QuickTest uses a different value from the Data Table.
Environment variable parameters enable you to use variable values from other sources during the run session. These may be values you supply, or values that QuickTest generates for you based on conditions and options you choose.
Random number parameters enable you to insert random numbers as values in your test or component. For example, to check how your application handles small and large ticket orders, you can have QuickTest generate a random number and insert it in a number of tickets edit field.
Choosing a test automation framework
Below are few links which provide you solid understanding of the software test automation frameworks.
http://www.ibm.com/developerworks/rational/library/591.html
www.research.ibm.com/journal/sj/411/rankin.pdf
http://www.wilsonmar.com/WRSAFS/Docs/CHAPTER1.doc
http://en.wikipedia.org/wiki/Test_Automation_Framework
www.michaeldkelly.com/images/Choosing_a_Test_Automation_Framework.PDF
www.logigear.com/resources/articles_lg/Automation_Framework.pdf
QTP E-books
Just click on the links to download..
Using Database Functions
The code below contains a set of useful functions that can be used in QuickTest Professional.
'Example of how to use functions.
''******************************************************************************************
' Example of how to use DSN created for the database of sample Flight application.
''******************************************************************************************
connection_string="QT_Flight32"
isConnected = db_connect ( curConnection ,connection_string )
' Execute the basic SQL statement
set myrs=db_execute_query( curConnection , SQL )
' Report the query and the connection string
Reporter.ReportEvent micInfo ,"Executed query and created recordset ","Connection_string is ==> " & connection_string & " SQL query is ===> " & SQL
' Show the number of rows in the table using a record set
msgBox " Quantity of rows in queried DB ( db_get_rows_count )==> " & db_get_rows_count( myrs )
' Show the number of rows in the table using a new SQL statement
msgBox " Quantity of rows in queried DB (db_get_rows_count_SQL ) ==> " & db_get_rows_count_SQL( curConnection , "SELECT COUNT(*) FROM ORDERS" )
' Change a value of a field in an existing row
rc = db_set_field_value (curConnection, "ORDERS" , "Agents_Name" , "test", "Agents_Name", "AGENT_TESTER")
' Examples of how to retrieve values from the table
msgBox "val row 0 col 0: " & db_get_field_value( myrs , 0 , 0 )
msgBox "val row 0 col 1: " & db_get_field_value( myrs , 0 , 1 )
msgBox "val row 1 col Name: " & db_get_field_value( myrs , 1 , "Agents_Name" )
msgBox "val SQL row 1 col Name: " & db_get_field_value_SQL( curConnection , "ORDERS" , 1 , "Agents_Name" )
''******************************************************************************************
''******************************************************************************************
' The function creates a new connection session to a database.
' curSession - The session name (string)
' connection_string - A connection string
' for example the connection_string can be "DSN=SQLServer_Source;UID=SA;PWD=abc123"
''******************************************************************************************
Function db_connect( byRef curSession ,connection_string)
set connection = CreateObject("ADODB.Connection")
db_connect= "Error # " & CStr(Err.Number) & " " & Err.Description
connection.Open connection_string
db_connect= "Error # " & CStr(Err.Number) & " " & Err.Description
''******************************************************************************************
' The function disconnects from the database and deletes the session.
' curSession - the session name (string)
''******************************************************************************************
Function db_disconnect( byRef curSession )
''******************************************************************************************
' The function executes an SQL statement.
' Note that a db_connect for (arg1) must be called before this function
' curSession - The session name (string)
''******************************************************************************************
Function db_execute_query ( byRef curSession , SQL)
set rs = curSession.Execute( SQL )
''******************************************************************************************
' ----------------------------
' The function returns the number of rows in the record set
' curRS - Variable, containing a record set, that contains all values that retrieved from the database by query execution
''******************************************************************************************
Function db_get_rows_count( byRef curRS )
''******************************************************************************************
' ------------------------------------
' The function returns the number of rows that are the result of a given SQL statement
' curSession - The session name (string)
''******************************************************************************************
Function db_get_rows_count_SQL( byRef curSession ,CountSQL )
set cur_rs = curSession.Execute( CountSQL )
db_get_rows_count_SQL = cur_rs.fields(0).value
''******************************************************************************************
' -----------------------------------
' curSession - Variable that denotes the current active connection
' tableName - Name of the table, from which the value should be retrieved
''******************************************************************************************
Function db_get_field_value_SQL( curSession , tableName , rowIndex , colName )
SQL = " select " & colName & " from " & tableName
set rs = curSession.Execute( SQL )
db_get_field_value_SQL = rs.fields(colName).value
''******************************************************************************************
' The function returns the value of a single item of an executed query.
' Note that a db_execute_query for (arg1) must called before this function
' curRecordSet - Variable, containing a record set, that contains all values retrieved from the database by query execution
' rowIndex - The row index number (zero-based)
' colIndex - The column index number (zero-based) or the column name.
' -1 - Requested field index more than exists more than once in record set
''******************************************************************************************
Function db_get_field_value( curRecordSet , rowIndex , colIndex )
count_fields = curRecordSet.fields.count-1
If ( TypeName(colIndex)<> "String" ) and ( count_fields < colIndex ) then
db_get_field_value = -1 'requested field index exists more than once in recordset
db_get_field_value = curRecordSet.fields(colIndex).Value
''******************************************************************************************
' The function changes the value of a field according to a search criteria.
' We search for a certain row according to a column name and the desired vale, then we change a value in that row according
' curConnection - The session name (string)
' tableName - Name of the table from which the value should be retrieved
' colFind - The column which to search for the criteria
' colFindValue - The value for which to search in the column
' colChange - The column in which we want to change the value
' colChangeValue - The new value
' -1 - Requested field index that does not exist in the recordset
''******************************************************************************************
Function db_set_field_value(curConnection, tableName , colFind , colFindValue, colChange, colChangeValue)
checkSQL = "select * from Details"
set myrs1 = db_execute_query( curConnection , SQL )
count_fields = myrs1.fields.count
If ( TypeName(colFind)<> "String" ) or ( TypeName(colChange)<> "String" ) then
db_set_field_value = -1 'requested field index that does not exists in the record set
updateSQL = "UPDATE " & tableName & " SET " & colChange & "='" & colChangeValue & "' WHERE " & colFind & "='" & colFindValue & "'"
set myrs1 = db_execute_query( curConnection , updateSQL )
db_set_field_value = 1 'operation suceeded
''******************************************************************************************
' The function adds a new row to the desired table
' curConnection - Variable, containing a recordset, that contains all the values to be retrieved from the database by query execution
' tableName - Name of the table, from which the value should be retrieved
' values - Array that contains values to be entered in a new row to the table
' Note: The function must receive values for all the columns in the table.
' -1 - The number of values to be entered to the table does not match the number of columns
' 1 - Execution of the query succeed and the data was entered to the table
''******************************************************************************************
Function db_add_row(curConnection, tableName , byRef values)
updateSQL = "INSERT INTO " & tableName & " VALUES ("
arrLen = UBound (values) - LBound (values) + 1
set myrs1=db_execute_query( curConnection , SQL )
count_fields = myrs1.fields.count
' Check whether the number of values match the number of columns
If arrLen <> count_fields then
updateSQL = updateSQL & values (i)
set myrs1 = db_execute_query( curConnection , updateSQL )
''******************************************************************************************
' represent_values_of_RecordSet
' ---------------------------------------------
' The function reports all the values of fields in a record set
' curRS - Variable, containing the recordset, that contains all the values that were retrieved from the database by the query execution
''******************************************************************************************
Function represent_values_of_RecordSet( myrs)
reporter.ReportEvent 4,"Fields quantity" , myrs.fields.count
count_fields = myrs.fields.count-1
curRowString = curRowString& "Field " &"==> " & myrs.fields(ii).Name &" : Value ==>" & myrs.fields(ii).Value & vbCrLf
reporter.ReportEvent 4,"Current row"& curRow , curRowString
Using the File System Object (FSO)
The following code includes a set of complex and simple functions to serve as examples of the possible uses and applications of Microsoft FSO.
' Create the file system object
set oFSO = CreateObject ("Scripting.FileSystemObject")
' *******************************************************************************************
' FilePath - location of the file and its name
' *******************************************************************************************
Function CreateFile (FilePath)
' Variable that will hold the new file object
set NewFile = oFSO.CreateTextFile(FilePath, True)
' *******************************************************************************************
' Check if a specific file exist
' FilePath - Location of the file and its name
' *******************************************************************************************
Function CheckFileExists (FilePath)
CheckFileExists = oFSO.FileExists(FilePath)
' *******************************************************************************************
' FileRef - Reference to the file
' str - Data to be written to the file
*******************************************************************************************
Function WriteToFile (byref FileRef,str)
' *******************************************************************************************
' FileRef - reference to the file
' *******************************************************************************************
Function ReadLineFromFile (byref FileRef)
ReadLineFromFile = FileRef.ReadLine
' *******************************************************************************************
' FileRef - Reference to the file
' *******************************************************************************************
Function CloseFile (byref FileRef)
'******************************************************************************************
' Opens a specified file and returns an object that can be used to
' read from, write to, or append to the file.
' FilePath - Location of the file and its name
' *******************************************************************************************
Function OpenFile (FilePath,mode)
' Open the txt file and return the File object
set OpenFile = oFSO.OpenTextFile(FilePath, mode, True)
' *******************************************************************************************
' FilePathSource - Location of the source file and its name
' FilePathDest - Location of the destination file and its name
' *******************************************************************************************
Sub FileCopy ( FilePathSource,FilePathDest)
' copy source file to destination file
oFSO.CopyFile FilePathSource, FilePathDest
' *******************************************************************************************
' FilePath - Location of the file to be deleted
' *******************************************************************************************
' Copy source file to destination file
' *******************************************************************************************
' FilePath1 - Location of the first file to be compared
' FilePath2 - Location of the second file to be compared
' FilePathDiff - Location of the differences file
' ignoreWhiteSpace - Controls whether or ignore differences in white space characters
' true - Ignore differences in white space
' false - Do not ignore difference in white space
' Return Value: true if files are identical, false otherwise'
' *******************************************************************************************
Function FileCompare (byref FilePath1, byref FilePath2, byref FilePathDiff, ignoreWhiteSpace)
set f1 = OpenFile(FilePath1,1)
set f2 = OpenFile(FilePath2,1)
set f_diff = OpenFile(FilePathDiff,8)
' Count how many lines there are in the first file
' Count how many lines there are in the second file
' Re-open the files to go back to the first line in the files
set f1 = OpenFile(FilePath1,1)
set f2 = OpenFile(FilePath2,1)
' compare the number of lines in the two files.
' assign biggerFile - The file that contain more lines
' assign smallerFile - The file that contain fewer lines
If ( rowCountF1 < rowCountF2) Then
str = "Line" & vbTab & "File1" & vbTab & vbTab & "File2"
' Loop on all the lines in the smaller file
While not smallerFile.AtEndOfStream
' Check if we need to ignore white spaces, if yes, trim the two lines
' If there is a difference between the two lines, write them to the differences file
str = lineNum & vbTab & str1 & vbTab & vbTab & str2
' Loop through the bigger lines, to write its line to the different file
While not biggerFile.AtEndOfStream
str1 = ReadLineFromFile(biggerFile)
str = lineNum & vbTab & "" & vbTab & vbTab & str2
FileCompare = Not differentFiles
' ************** Example of using these functions **********************
FilePath1 = "D:\temp\FSO\txt1.txt"
FilePath2 = "D:\temp\FSO\txt2.txt"
FilePathDiff = "D:\temp\FSO\txt_diff.txt"
d = FileCompare(FilePath1,FilePath2,FilePathDiff,false)
FilePath = "D:\temp\FSO\txt.txt"
set fold = FolderCreate ( "D:\temp\FSO
' = WriteToFile(f,"test line")
Fexist= CheckFileExists(FilePath)
d = WriteToFile(f,"first line")
d = WriteToFile(f,"second line")
Using Microsoft Outlook to Send Email
The code below illustrates two methods for sending email using the VBScript code in QuickTest Professional, and two different COM objects.
Function SendMail(SendTo, Subject, Body, Attachment)
Set ol=CreateObject("Outlook.Application")
Mail.Attachments.Add(Attachment)
Function SendMail(SendFrom, SendTo, Subject, Body)
Set objMail=CreateObject("CDONTS.Newmail")
Using Microsoft Word Spell Check
The following code shows a function for checking the number of spelling and grammar errors in a string.
Function NumberOfSpellErrors(strText)
Set objMsWord = CreateObject("Word.Application")
objMsWord.WordBasic.Insert strText
NumberOfSpellErrors = objMsWord.ActiveDocument.SpellingErrors.Count
objMsWord.Documents.Close (False)
objMsWord.Quit ' close the application
Set objMsWord = Nothing' Clear object memory
' The following function uses the Spell errors function to check a specific property
' of all the objects with a given description which are under a given parent
Sub CheckAllObjects(ParentObj, ObjDesc, PropName)
Dim ObjCol, idx, PropValue, OldReportMode
OldReportMode = Reporter.Filter
Reporter.Filter = 2 ' Report only errors
Set ObjCol = Desktop.ChildObjects(ObjDesc)
Set ObjCol = ParentObj.ChildObjects(ObjDesc)
PropValue = ObjCol.Item(idx).GetROProperty(PropName)
RetVal = NumberOfSpellErrors(PropValue) ' The actual spell check result
ReportText = "Object #" & idx+1 & ": The '" & PropName & "' Property has " & RetVal & " spell errors (" & PropValue & ")"
Reporter.ReportEvent 1, "Spell Check", ReportText
Reporter.Filter = OldReportMode
'''''''''''''''''''''''''''''''''''''
' Go over all the static objects in the Login window of the Flight Application
' and for each object check the text for spelling and grammatical errors
'''''''''''''''''''''''''''''''''''''
' Go over all the links in the page and report all the ones that fail the spellcheck
Set Desc = Description.Create()
Desc("nativeclass").Value = "Static"
Set Obj = Dialog("nativeclass:=#32770", "text:=Login")
' Invoke the Flight Application before calling the function
CheckAllObjects Obj, Desc, "text"