Your SlideShare is downloading. ×
0
Gl qtp day 3   2
Gl qtp day 3   2
Gl qtp day 3   2
Gl qtp day 3   2
Gl qtp day 3   2
Gl qtp day 3   2
Gl qtp day 3   2
Gl qtp day 3   2
Gl qtp day 3   2
Gl qtp day 3   2
Gl qtp day 3   2
Gl qtp day 3   2
Gl qtp day 3   2
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Gl qtp day 3 2

233

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
233
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 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 Function1 © 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 Sub5 © 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 Function7 © 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 Function8 © 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 Sub9 © 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 6310 © Copyright GlobalLogic 2008
  • 11. QTP Utility Functions – keyboard Values11 © 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.GetText13 © Copyright GlobalLogic 2008

×