Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

How to write to Text Files using fileSystemObject in QTP

Using FileSystemObject we can write data to a text file. Below are the steps to write to a text file.


1. Create an instance of the FileSystemObject

 This is done through
Set objFSO = CreateObject("Scripting.FileSystemObject")

2. Use OpenTextFile to open an existing file or CreateTextFile to create a new text file to write data into.

Set otf = objFSO.CreateTextFile(FilePath,[OverWrite])
where FilePath is path of file to be created.OverWrite – Flag for overwriting content 

OR

 Set otf = objFSO.OpenTextFile(FilePath,ioMode)

where FilePath is path of file to be opened.ioMode – Define mode in which file to be opened. Use 1 to overwrite data and 8 to append data. 


3. Use below methods to write to the file.


a. Write(strText) - Write specified string in the text file.

Otf.Write(strText)

b. Writeline(strText) - writes a specified string and newline character.

otf.writeline(strText)

c. WriteblankLines(intLine) - Writes the specified number of blank lines in the text file

otf.WriteblankLines(4)

4. Close the text file.

otf.close This will close the file. It will close the text file. Note file is saved automatically when we write to a text file.


5. Set all objects as nothing


How to read text files in QTP using VBScript- Working With FileSystem Object


We can read the content of a text file using FileSystemObject methods and properties. Below are the various methods and properties used to extract data from file.

1. Creating an instance of filesystem object

Before reading content from a file, we have to create instance of filesystemobject.
Set objFSO = CreateObject("Scripting.FileSystemObject")

2. Opening an existing file to read

Set objTxtFile = objFSO.OpenTextFile(FilePath,iomode)

Iomode can have following values:

0 – For reading – opens the file for reading.
1 – For Writing – opens the file for writing, does overwrite the existing content in file.
8 – For Appending – opens the file for appending, new content is appended at the end of file.

3. ReadAll method 

It stores all the content of a file in a variable as string.sContent = objTxtFile.ReadAll

4. ReadLine method

It allows a script to read individual lines in a text file.
sContent = objTxtFile.Readline.
To read through all lines, use a Do Loop that continues until the AtEndOfStream property is True.
Below code snippet shows how to read through each line.

i=0
Do Until objTxtFile.AtEndOfStream
        sContent = objTxtFile.ReadLine
        sDataArray(i) = objTxtFile.ReadLine
        MsgBox sContent
        i = i+1
Loop

Content of each line can be stored in an array also.

5. Read(chr) 

 The Read method allows you to read only a specified number of characters.
Stores number of characters as defined in chr.

sContent = objTxtFile.Read(5)

6. Skip(chr) 

This method skips the number of characters defined in chr from the text file
skip(5)

7. SkipLine

This method skips line of data from the text file.
skipline

8. AtEndOfLine Property

 Returns true if the file pointer is positioned immediately before the end-of-line marker in a TextStream file. Can be useful to find characters in a line or reading characters till end of line.
Do While file.AtEndOfLine <> True 

9. Line property

gives the current line number of the textStream.
objTxtFile.line

10. Column Property

 Read-only property that returns the column number of the current character position in a TextStream file.
objTxtFile.Column       

11. OpenasTextStream Method

This method is used with getfile method to read from file.

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTxtFile = objFSO.getFile(FilePath)
var objts = objTxtFile.OpenAsTextStream(iomode)

How to use and load Environment variables in QTP

Environment variables are a set of dynamic named values that can affect the way running processes will behave on a computer. Environment variables in qtp are very useful to define the environment condition in which QTP is working.

Types of Environment variables

There are two types of Environment variables in QTP

Built-in variable

Built-in variables are QTP defined variables that contains information of Test Properties, computer’s information, Information related to operating system and Action’s property.

User defined variable

Other than environment variable defined in-built in QTP, We can define user defined environment variables. The main purpose of defining user defined variables is to define global variables that can be used throughout test execution irrespective of working with different Actions. So scope of user defined variable is throughout the test execution. This can be of type internal or external if loaded from a file.

Loading of User defined Environment variable


Environment variable can be loaded in following ways:


1.  Through QTP UI

 User defined Environment variables can be created through UI as shown below

Adding new environment Parameter

 Option to export user-defined environment variables and loading of environment variables from external file is also available.


2.  Using ExternalFileName Property and LoadfromFile Method

 Loading of variables from external file as shown above can be done through code:

'Check if an External Environment file is loaded and if not, load it.
fileName = Environment.ExternalFileName
if (fileName = "") Then
    Environment.LoadFromFile("C:\Env.xml")
End If

3.  Using Environment.Value

 Value of environment variable can be extracted at runtime as 
StrEnv=environment.value(envName)

Similarly value of environment variable can be set at runtime as
Environment.value(“envname”)=”some value”

Where to Access Environment Variable 


To access the environment variable, Go to File>Settings>Environment and select built-in or user defined from the drop down.

Accessing environment variables

Use of Environment variable 


  • We can define parameters in QTP using environment variables.
  • Environment variables can act as global variables which can be used across different  tests/actions.
  • We can dynamically load environment variables during start up of QTP execution and can store url, environment, login details in a xml file,which will be loaded each time test is executed.
  • Built- in Environment variables gives us useful information about the test environment and extracting the information can be useful.