Search This Blog

Thursday 27 October 2011

Working with Text Files/Flat Files/Sequenctial Files & Examples


Working with text files:-

                                   

                        From the above diagram, QTP is able to work with flat files/ text files/ sequenctial files. To open a text file in QTP by using VBScript , we can use below code.

                        Syntax:-          option explicit
                                                Dim fso,fo
                                                Set fso=createobject(“scripting.filesystemobject”)
                                                Set fo=fso.opentextfile(“path of file”,1/2/8,true/false)

            True=file was create, while file was not found,                     1=read mode
            False=file not create, while file was not found                      2=write mode
                                                                                                            8=append mode


EX:-1
            To display existing lines in specified files.

            File name:-      sample.txt
                       
                                    1.my name is kishore
                                    2.hello
                                    3. how are you?

                        Option explicit
                        Dim fso,fo,l
                        Set fso=createobject(“scripting.filesystemobject”)
                        Set fo=fso.opentextfile(“c:\sample.txt”)
                        While fo.atendofstream< >true
                                    l=fo.readline
                                    print(l)
                        wend
                        fo.close
                        set fo=nothing
                        set fso=nothing
           

            EX:-2
            To display the “hi” string line from the file.

                        Option explicit
                        Dim fso.fo.l
                        Set fso=createobject(“scripting.dictionary”)
                        Set fo=fso.opentextfile(“c:\sample.txt”,1,false)
                        While fo.atendofstream< >true
                                    l=fo.readline
                                    if insrt(l,”hi”) then
                                                print(l)
                                    end if
                        wend
                        fo.close
                        set fo=nothing
                        set fso=nothing

No comments:

Post a Comment