Saturday, July 12, 2008

QTP & File Handling

When we need to interact with text files using QTP. Interaction can be(but not limited to) in the form of reading input from a file, writing output to a file. This post describe in detail "File handling using QTP".

We use FSO object to do this.

What is FSO?

FSO stands for File System Object. This is used to support text file creation and manipulation through the TextStream object and is contained in the Scripting type library (Scrrun.dll)

The FSO Object Model has a rich set of properties, methods and events to process folders and files.

How to create a file?

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?

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?


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 qtp")

file.Write("questions and answers solved.")

//Output will be:

This is a place to get all your qtp

questions and answers solved.

How to delete content?

Use DeleteFile() method to delete a file from a particular location

Foe Example:

file_location = "C:\file_location"

Set fso = CreateObject(“Scripting.FileSystemObject”)

fso.DeleteFile(file_location)

qtp,textfile,read,write,delete

2 comments:

Unknown said...

Very good explanation..Thanks.

Pot-Pot-Noodles said...

How do you browse for a file?

Quality Center


Quality Center

A Quality Center project is a database for collecting and storing data relevant to a testing process. For QuickTest to access a Quality Center project, you must connect to the local or remote Web server where Quality Center is installed. When QuickTest is connected to Quality Center, you can create tests and save them in your Quality Center project. After you run your tests, you can view the results in Quality Center.


Quality Center helps you maintain a project of all kinds of tests (such as QuickTest tests, business process tests, manual tests, tests created using other Mercury products, and so forth) that cover all aspects of your application's functionality. Each test in your project is designed to fulfill a specified testing requirement of your application. To meet the goals of a project, you organize the tests in your project into unique groups.

You can instruct QuickTest to automatically submit a defect to a Quality Center project for each failed step in your test. You can also manually submit a defect for a specific step to Quality Center directly from within your QuickTest Test Results window. These options are only available when you are connected to a Quality Center project.

Saving Tests to a Quality Center Project

When QuickTest is connected to a Quality Center project, you can create new tests in QuickTest and save them directly to your project. To save a test, you give it a descriptive name and associate it with the relevant subject in the test plan tree. This helps you to keep track of the tests created for each subject and to quickly view the progress of test planning and creation. In QuickTest, click Save or choose File > Save to save the test. The Save Test to Quality Center dialog box opens and displays the test plan tree.

The Save Test to Quality Center dialog box opens only when QuickTest is connected to a Quality Center project. To save a test directly in the file system, click the File System button to open the Save QuickTest Test dialog box. You can return to the Save Test to Quality Center project dialog box by clicking the Quality Center button.

Submitting Defects to a Quality Center

When viewing the results of a run session, you can submit any defects detected to a Quality Center project directly from the Test Results window.

To manually submit a defect to Quality Center: or click the Choose Tools > Quality Center ConnectionQuality Center Connection button to connect to a Quality Center project. Choose Tools > Add Defect or click the Add Defect button to open the Add Defect dialog box in the specified Quality Center project. The Add Defect dialog box opens. You can modify the defect information if required. Basic information about the test and any checkpoints (if applicable) is included in the description. Click submit button.

To automatically submit defects to Quality Center: Choose Tools > Options or click the Options button . The Options dialog box opens. Click the Run tab. Select the Submit a defect to Quality Center for each failed step check box.