Saturday, 10 August 2013

How to Create a File in QTP

We first create a FSO object using CreateObject and then create a text file using CreateTextFile.
For Example: Suppose you want to create a file called “test.txt” located in C:
     Dim fso, file, file_location
     file_location = "C:\file_location"
     Set fso = CreateObject(“Scripting.FileSystemObject”)
     Set file = fso.CreateTextFile(file_location, True) // True--> file is to be overwritten if it already exists      
     else false  We would use the same example for the rest of this post.

How to Open a File in QTP

Set file= fso.OpenTextFile("C:\file_location", ForWriting, True)//2nd argument can be ForReading, ForWriting, ForAppending
//3rd argument is “True” if new file has to be created if the specified file doesn’t exist else false, blank signify false.
How to read content from a file?
Use ReadLine() method
For example:
Set file= fso.OpenTextFile("C:\file_location", ForReading, True) //2nd argument should always be "ForReading" in order to read contents from a file
Do while file.AtEndofStream  True
      data = file.ReadLine()
      msgbox data
Loop

How to Write Content to a File in QTP

You can use Write() or WriteLine() Methods to write text into a file. The difference between the Write() and WriteLine() Method is that the latter automatically inserts a new line character while the former doesn’t insert a new line character.
For example:


Set file= fso.OpenTextFile("C:\file_location", ForWriting, True) //2nd argument should always be "ForWriting" in order to write contents to a file
file.Write("This is a place to get all your qtp")
file.Write("questions and answers solved.")
//Output will be:
This is a place to get all your qtp questions and answers solved.

while

file.WriteLine("This is a place to get all your answer")
file.Write("questions and answers solved.")
//Output will be:

This is a place to get all your answer.
questions and answers solved.

How to Delete Content/file in QTP

Use DeleteFile() method to delete a file from a particular location
For Example:
file_location = "C:\file_location"
Set fso = CreateObject(“Scripting.FileSystemObject”)
fso.DeleteFile(file_location)

How to Restart the PC Using QTP

Use WMI to restart PC Dim OpSysSet Set OpSysSet = GetObject("winmgmts:{(Shutdown)}//./root/cimv2").ExecQuery("select * from   Win32_OperatingSystem where Primary=true") for each OpSys in OpSysSet   OpSys.Reboot()