Date post: | 05-Dec-2014 |
Category: |
Technology |
Upload: | pragya-rastogi |
View: | 505 times |
Download: | 0 times |
© Copyright GlobalLogic 2008
1
QTP Utility Functions – File Operations
File Creation
1. CreateFile "C:","mytextfile.txt","hi how are you?"
2. Public Function CreateFile(filpath,filname,filcontent)
3. xml_file = filpath & "\" & filname
4. Dim fileobject, tf
5. Set fileobject = CreateObject("Scripting.FileSystemObject")
6. Set tf = fileobject.CreateTextFile(xml_file, True)
7. tf.Write (filcontent)
8. tf.Close
9. End Function
© Copyright GlobalLogic 2008
2
QTP Utility Functions – File Operations
• dim oFSO• ' creating the file system object• set oFSO = CreateObject ("Scripting.FileSystemObject")• • 'Option Explicit• ' *********************************************************************************************• ' Create a new txt file• • ' Parameters:• ' FilePath - location of the file and its name• ' *********************************************************************************************• Function CreateFile (FilePath)• ' varibale that will hold the new file object• dim NewFile• ' create the new text ile• set NewFile = oFSO.CreateTextFile(FilePath, True)• set CreateFile = NewFile• End Function• • ' *********************************************************************************************• ' Check if a specific file exist• • ' Parameters:• ' FilePath - location of the file and its name• ' *********************************************************************************************
© Copyright GlobalLogic 2008
3
QTP Utility Functions – File Operations
• Function CheckFileExists (FilePath)• ' check if file exist• CheckFileExists = oFSO.FileExists(FilePath)• End Function• • ' *********************************************************************************************• ' Write data to file• • ' Parameters:• ' FileRef - reference to the file• ' str - data to be written to the file• ' *********************************************************************************************• Function WriteToFile (byref FileRef,str)• ' write str to the text file• FileRef.WriteLine(str)• End Function• • ' *********************************************************************************************• ' Read line from file• • ' Parameters:• ' FileRef - reference to the file• ' *********************************************************************************************
© Copyright GlobalLogic 2008
4
QTP Utility Functions – File Operations
• Function ReadLineFromFile (byref FileRef)• ' read line from text file• ReadLineFromFile = FileRef.ReadLine• End Function• • ' *********************************************************************************************• ' Closes an open file.• ' Parameters:• ' FileRef - reference to the file• ' *********************************************************************************************• Function CloseFile (byref FileRef)• FileRef.close• End Function• • '*********************************************************************************************• ' Opens a specified file and returns an object that can be used to • ' read from, write to, or append to the file.• • ' Parameters:• ' FilePath - location of the file and its name• ' mode options are:• ' ForReading - 1• ' ForWriting - 2• ' ForAppending - 8• ' *********************************************************************************************
© Copyright GlobalLogic 2008
5
QTP Utility Functions – File Operations
• ' *********************************************************************************************• Function OpenFile (FilePath,mode)• ' open the txt file and retunr the File object• set OpenFile = oFSO.OpenTextFile(FilePath, mode, True)• End Function• • ' *********************************************************************************************• ' Copy a File
• ' Parameters:• ' 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• End Sub• • ' *********************************************************************************************• ' Delete a file.• • ' Parameters:• ' FilePath - location of the file to be deleted• ' *********************************************************************************************• Sub FileDelete ( FilePath)• ' copy source file to destination file• oFSO.DeleteFile ( FilePath)• End Sub
© Copyright GlobalLogic 2008
6
QTP Utility Functions – File Operations
• ' ************** Example of calling the file functions **********************• FilePath1 = "D:\temp\FSO\txt1.txt"• FilePath2 = "D:\temp\FSO\txt2.txt"• FilePathDiff = "D:\temp\FSO\txt_diff.txt"• • FilePath = "D:\temp\FSO\txt.txt"• • set fold = FolderCreate ( "D:\temp\FSO\new")• set f = OpenFile(FilePath,8)• ' = WriteToFile(f,"test line")• d = CloseFile(f)
• set f = CreateFile(FilePath)• • Fexist= CheckFileExists(FilePath)• d = WriteToFile(f,"first line")• d = WriteToFile(f,"second line")• d = CloseFile(f)• FileCopy "D:\temp\FSO\txt.txt","D:\temp\FSO\txt1.txt"• FileDelete "D:\temp\FSO\txt1.txt"
© Copyright GlobalLogic 2008
7
QTP Utility Functions – Excel Sheet Operations
• Function ReadExcelData(xlFile, sheetName, columnName, rowNum)• • ' Initializing the variables• varSheetName = sheetName• varColumnName = columnName• varRowNum = rowNum• • Set objExcel = CreateObject("Excel.Application")• objExcel.WorkBooks.Open xlFile• Set objSheet = objExcel.ActiveWorkbook.Worksheets(varSheetName)• rows = objSheet.Range("A1").CurrentRegion.Rows.Count• columns = objSheet.Range("A1").CurrentRegion.Columns.Count• For currCol = 1 to columns• If objSheet.Cells(1,currCol).Value = varColumnName then• ReadExcelData = objSheet.Cells(varRowNum+1,currCol).Value• currCol = columns• end if• Next• • objExcel.ActiveWorkbook.Close• objExcel.Application.Quit• Set objSheet = nothing• Set objExcel = nothing • End Function
© Copyright GlobalLogic 2008
8
QTP Utility Functions – Email Operations
• SendMail “[email protected]","hi","how r u",""
• Function SendMail(SendTo, Subject, Body, Attachment)
• Set ol=CreateObject("Outlook.Application")• Set Mail=ol.CreateItem(0)• Mail.to=SendTo• Mail.Subject=Subject• Mail.Body=Body• If (Attachment <> "") Then
• Mail.Attachments.Add(Attachment)
• End If• Mail.Send• ol.Quit• Set Mail = Nothing• Set ol = Nothing
• End Function
© Copyright GlobalLogic 2008
9
QTP Utility Functions – Timed Msg-Box
• MsgBoxTimeout (“Sample Text”,”Timed MsgBox”, 10)
• Public Sub MsgBoxTimeout (Text, Title, TimeOut)
Set WshShell = CreateObject("WScript.Shell")
WshShell.Popup Text, TimeOut, Title• End Sub
© Copyright GlobalLogic 2008
10
QTP Utility Functions – Keystroke Functions
• 'An example that presses a key using DeviceReplay.
• Set obj = CreateObject("Mercury.DeviceReplay")
• Window("Notepad").Activate
• obj.PressKey 63
© Copyright GlobalLogic 2008
11
QTP Utility Functions – keyboard Values
© Copyright GlobalLogic 2008
12
QTP Utility Functions – System Operations
• Running and Closing Applications Programmatically
• Syntax:• SystemUtil.Run “file, [params], [dir] “
• Example:• SystemUtil.Run “notepad.exe myfile.txt “
© Copyright GlobalLogic 2008
13
QTP Utility Functions – Clipboard Objects
The object has the same methods as the Clipboard object available in Visual Basic: Clear GetData GetFormat GetText SetData SetText
• Set cb = CreateObject("Mercury.Clipboard")cb.Clearcb.SetText "TEST"MsgBox cb.GetText