More Related Content
Similar to Gl qtp day 3 2
Similar to Gl qtp day 3 2 (20)
More from Pragya Rastogi (18)
Gl qtp day 3 2
- 1. QTP Utility Functions – File Operations
File Creation
3. CreateFile "C:","mytextfile.txt","hi how are you?"
4. Public Function CreateFile(filpath,filname,filcontent)
5. xml_file = filpath & "" & filname
6. Dim fileobject, tf
7. Set fileobject = CreateObject("Scripting.FileSystemObject")
8. Set tf = fileobject.CreateTextFile(xml_file, True)
9. tf.Write (filcontent)
10. tf.Close
11. End Function
1
© 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
• ' *********************************************************************************************
2
© 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
• ' *********************************************************************************************
3
© 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
• ' *********************************************************************************************
4
© 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
5
© Copyright GlobalLogic 2008
- 6. QTP Utility Functions – File Operations
• ' ************** Example of calling the file functions **********************
• FilePath1 = "D:tempFSOtxt1.txt"
• FilePath2 = "D:tempFSOtxt2.txt"
• FilePathDiff = "D:tempFSOtxt_diff.txt"
•
• FilePath = "D:tempFSOtxt.txt"
•
• set fold = FolderCreate ( "D:tempFSOnew")
• 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:tempFSOtxt.txt","D:tempFSOtxt1.txt"
• FileDelete "D:tempFSOtxt1.txt"
6
© 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
7
© Copyright GlobalLogic 2008
- 8. QTP Utility Functions – Email Operations
• SendMail “rajat.gupta@globallogic.com","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
8
© 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
9
© 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
10
© 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 “
12
© 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.Clear
cb.SetText "TEST"
MsgBox cb.GetText
13
© Copyright GlobalLogic 2008