Search This Blog

Thursday 27 October 2011

Read( ),Readline( ),ReadAll( ) methods with syntax's


QTP ReadLine(), ReadAll(), Read() SYNTAX

Readline ( ):-
                        We can use this method to read current line from file and move control to next line.

                                                Syntax:-  fo.readline( )

Readall( ):-
We can use this method to read all lines in the file at a time.

                        Syntax:-  fo.readall( )

Read( ):-
                   We can use this method to read specify no:of characters from current line.

                                                Ex:- My name is khan
                                                            ---------
                                                            ---------

                                                X=read(4)
                                                Print(x)                        o/p: My n
                                                                       
                                   
                        

Regular expression & methods with Examples


Regexp object:-
                        Regexp object is used to integrate QTP with regular expression utilization. This object consist of 3 properties and 3 methods

A)    Pattern:- we can use this property to store expected expression for matching.
Syntax:-          set r=new regexp
                        r.pattern=”regexp”
                        r.global=true/false
            B)  Global:-
                        We can use this property to specify pattern matching on given text until end of text.
                                    Syntax:-          set r=new regexp
                                                            r.pattern=”regexp”
                                                            r.global=true/false

c)      Ignorecase:-
We can use this property to specify case sensitiveness.

Syntax:-          set r=new regexp
                        r.pattern=”[A-Z][a-z]+”
                        r.global=true
                        r.ignorecase=true

In example code, a string will be matched with pattern which start’s with lowercase also. Because ignorecase is true.

            D) Execute( ):-          
                        We can use this method to apply given pattern on given text and find matched values in text.

                                    Ex:-     set r=new regexp
                                                r.pattern=”[0-9]+”
                                                r.global=true
                                                x=”jky345 acd 23 jkil34dd”
                                                set y=r.execute(x)
                                                for each z in y
                                                            print(z.value)
                                                next
           
E)Test:-
We can use this to apply given pattern on given text for matching. If pattern was matched with matched with text , then this method returns true.

            Ex:-     set r=new regexp
                        r.pattern=”[0-9]+”
                        r.global=true
                        x=”544755
                        if r.test(x) then
                                    print(“matched”)
                        else
                                    print(“mismatched”)
                        end if

A)    Replace( ):-
We can use this method to replace matched text with other text.

            Ex:-     set r=new regexp
                        r.pattern=”[0-9]+”
                        r.global=true
                        x=”my name is 5”
                        r.replace(x,”A”)
                        print(x)

            Ex:-8
                        To copy one file text into another file.

                        Option explicit
                        Dim fso,fo,l,fo1,fo2
                        Set fso=createobject(“scripting.filesystemobject”)
                        Set fo1=fso.opentextfile(“c:\sample1.txt”,1,false)
                        Set fo2=fso.opentextfile(“c:\sample2.txt”,2,true)
                        While fo1.atendofstream< >true
                                    l=fo1.readline
                                    fo2.writeline(l)
                        wend
                        fo1.close
                        fo2.close
                        set fo1=nothing
                        set fo2=nothing
                        set fso=nothing

            Ex:-9
                        Write vbscript in qtp , to copy numbers in file1 into file2.
           
                        Option explicit
                        Dim fso,fo1,fo2,l,nums,num
                        Set fso=createobject(“scripting.filesystemobject”)
                        Set fo1=fso.opentextfile(“c:\sample1.txt”,1,false)
                        Set fo2=fso.opentextfile(“c:\sample2.txt”,2,true)
                        While fo1.atendofstream< >true
                                    l=fo1.readline
                                    set r=new regexp
                                    r.pattern=”[0-9]+”
                                    r.global=true
                                    set nums=r.execute(l)
                                    for each num in nums
                                                fo2.writeline(num.value)
                                    next
                                    set numns=nothing
                                    set r=nothing
                        wend
                        fo1.close
                        fo2.close
                        set fo1=nothing
                        set fo2=nothing
                        set fso=nothing


            Ex:-10

To copy alphanumeric only from file1 to file2.

Option explicit
Dim fso,fo1,fo2,l,nums,num,r
Set fso=createobject(“scripting.filesystemobject”)
Set fo1=fso.opentextfile(“c:\sample1.txt”,1,false)
Set fo2=fso.opentextfile(“c:\sample2.txt”,2,true)
While fo1.atendofstream< >true
            l=fo1.readline
            set r=new regexp
            r.pattern=”[0-9]+[a-z]+”
            r.global=true
            set nums=r.execute(l)
            for each num in nums
                        fo2.writeline(num.value)
            next
wend
fo1.close
fo2.close
set fo1=nothing
set fo2=nothing
set fso=nothing


            Ex:-11
                        To copy dates in file1 into file2. Here date is “mm/dd/yy”

                        Option explicit
                        Dim fso,fo1,fo2,l,nums,num,r
                        Set fso=createobject(“scripting.filesystemobject”)
                        Set fo1=fso.opentextfile(“c:\sample1.txt”,1,false)
                        Set fo2=fso.opentextfile(“c;\sample2.txt”,2,true)
                        While fo1.atendofstream< >true
                                    l=fo1.readline
                                    set r=new regexp
                                    r.pattern=”(([0][0-9])|([1][0-2]))[/](([0][0-9])|([1][0-9])|([2][0-        
9])|([3][0-1]))[/][0-9]{2}”
            r.global=true
            set nums=r.execute(l)
            for each num in nums
                        fo2.writeline(num.value)
            next
            set nums=nothing
            set r=nothing
wend
set fo1=nothing
set fo2=nothing
set fso=nothing

Text Files/Flat Files/Sequential Files Examples


EX:-4
                        Write vbscript QTP, to display no:of words in every line of file.

                                    Option explicit
                                    Dim fso,fo,l,nw,p
                                    Set fso=createobject(“scripting.filesystemobject”)
                                    Set fo=fso.opentextfile(“c:/sample.txt”,1,false)
                                    While fo.atendofstream< >true
                                                l=fo.readline
                                                p=split(l,” “)
                                                nw=ubound(p)+1
                                    wend
                                    print(“no:of words in file”&nw)
                                    fo.close
                                    set fo=nothing
                                    set fso=nothing


            Ex:-5
                        To display only numeric letters in the given file.

                                    Option explicit
                                    Dim fso,fo,l,p,ps
                                    Set fso=createobject(“scripting.filesystemobject”)
                                    Set fo=fso.opentextfile(“c:\sample.txt”,1,false)
                                    While fo.atendofstream< >true
                                                l=fo.readline
                                                ps=split(l,” “)
                                                for each p in ps
                                                            if isnumeric(p) then
                                                                        print(p)
                                                            end if
                                                next
                                    wend
                                    fo.close
                                    set fo=nothing
                                    set fso=nothing

            EX:-6
                        To get inbuilt integer numbers in every line of file.

                                    Option explicit
                                    Dim fso,fo,l,nums,num
                                    Set fso=createobject(“scripting.filesystemobject”)
                                    Set fo=fso.opentextfile(“c:\sample.txt”,1,false)
                                    While fo.atendofstream< >true
                                                l=fo.readline
                                                set r=new regexp
                                                r.pattern=”[0-9]+”
                                                r.global=true
                                                set nums=r.execute(l)
                                                for each num in nums
                                                            print(num.value)
                                                next
                                                set nums=nothing
                                                set r=nothing
                                    wend
                                    fo.close
                                    set fo=nothing
                                    set fso=nothing
                                   

EX:-7
            To get only amount in below file.

                        Data.txt
                       
                        Sunitha salary is $202.12
                        $789 amount is salary of  kishore
                        Kiran sal is $222.33

                        Option explicit
                        Dim fso,fo,l,nums,num
                        Set fso=createobject(“scripting.filesystemobject”)
                        Set fo=fso.opentextfile(“c:\data.txt”,1,false)
                        While fo.atendofstream< >true
                                    l=fo.readline
                                    set r=new regexp
                                    r.pattern=”[$][0-9]+[\.]?[0-9]{2}”
                                    r.global=true
                                    set nums=r.execute(l)
                                    for each num in nums
                                                print(num.value)
                                    next
                                    set nums=nothing
                                    set r=nothing
                        wend
                        fo.close
                        set fo=nothing
                        set fso=nothing

Files concept "Instr ()" with an Example


Instr( ):-
                        We can use this function to search given word in a line of text. If word was found then it returns true.

                                    Syntax:-   Instr(line of text,”word”)


            EX:-3
                        To find the no:of lines in given file.

                                    Option explicit
                                    Dim fso,fo,l
                                    Set fso=createobject(“scripting.filesystemobject”)
                                    Set fo=fso.opentextfile(“c:\sample.txt”,1,false)
                                    l=0
                                    while fo.atendofstream< >true
                                                fo.readline
                                                l=l+1
                                    wend
                                    print(“no:of lines in the file”&l)
                                    fo.close
                                    set fo=nothing
                                    set fso=nothing


           


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

Dictionary objects methods "Add()","Item()","Keys()","Items()","key",Remove()","RemoveAll()","Exist()" & Examples


Add( ):-
                        We can use this method ,to “Add” a pair to dictionary.

Items( ):-
                   We can use this method , to get all “Items” in dictionary into array/list.

Keys( ):-
                   We can use this method, to get all “keys” in a dictionary into an array/list.

Item( ):-
                   We can use this property to get item of specified key.

                                                Syntax:-  dictionary.item(key)
EX:-3
            To create a dictionary, to store specify no:of subjects names and marks, then to display highest marks.

                                    Option explicit
                                    Dim marklist, i, j, high, nos, x,y
                                    Set marklist=createobject(“scripting.dictionary”)
                                    Nos=inputbox(“enter the no:of subjects”)
                                    For i=1 to nos
                                                J=inputbox(“enter the subject name”)
                                                K=inputbox(“enter the marks”)
                                                Marklist.add j,k
                                    Next
                                    X=marklist.items
                                    Max=marklist.item(“English”)
                                    For each y in x
                                                If (max<y) then
                                                            Max=y
                                                End if
                                    Next
                                    Print(max)

EX:-4
            To Display the employee name and his salary , who’s sal is highest in dictionary.
                                   
                                    Option explicit
                                    Dim noe, sal, i, j, k, x, y, emp
                                    Set emp=createobject(“scripting.dictionary”)
                                    Noe=inputbox(“enter the no:of emp’s)
                                    For i=1 to noe
                                                J=inputbox(“enter the emp name”)
                                                K=inputbox(“enter the emp sal”)
                                                Emp.add j, k
                                    Next
                                    X=emp.items
                                    Max=emp.item(“ram”)
                                    For each y in x
                                                If(clng(max)<y) then
                                                            Max=y
                                                End if
                                    Next
                                    X=emp.keys
                                    For each y in x
                                                If(clng(max)<emp.item(y))
                                                            Print(“emp name”&y”sal”&max)
                                                End if
                                    Next
                                    Set emp=nothing


Key:-
          We can use this property to change existing key to new key.

                        EX:-    set d=createobject(“scripting.dictionary”)
                                    d.add “math”,124
                                    --------------------
                                    --------------------
                                    --------------------
                                    d.key(“math”)=”mathes”
                                    --------------------
                                    --------------------

Remove( ):-
                        We can use this method to remove specified pair from dictionary.

                        EX:-    set d=createobject(“scripting.dictionary”)
                                    d.add “maths”,123
                                    d.add “physics”,232
                                    ------------------
                                    ------------------
                                    d.remove(“maths”)

Removeall( ):-
                   We can use this method to remove all pair in dictionary.

                        EX:-     set d=createobject(“scripting.dictionary”)
                                    d.add “maths”,123
                                    d.add “physics”,89
                                    d.add “social”,322
                                    -------------------
                                    -------------------
                                    d.removeall

Exist( ):-
                   We can use this method to verify the existence of specified pair in dictionary.

                        Ex:-    set d=createobject(“scripting.dictionary”)
                                    D.add “maths”,322
                                    d.add “physics”,89
                                    ------------------
                                    ------------------
                                    If d.exist(“maths”) then
                                                Print(“maths available”)
                                    End if

Note:-
            The data structures like variables ,arrays ,and dictionary are temporary ,because they are creating in ram. Due to this reason testers are using files, because files are saving in hard disk.