Tricentis Tosca 16.0 Released on Feb-2023 ----- UFT has been upgraded from UFT 15.0.1 to UFT One 15.0.2, Beginning at November 2020.

Tuesday, 29 October 2019

Regular Expressions in UFT

Regular expressions are the combination of wild cards and characters which is used to perform pattern matching.

It is string that provides a complex search phrase(is an expression consisting of one or more words.)

Uses of Regular expression is Scripting.

1. Test for a pattern withing a string - To check for existing of substring is a string. For example we can test an input string to see if a telephone number pattern or a credit card number pattern occurs withing the string, it is called data validation.

2. Replace Text - To find and replace a string with another string. We can use a regular expression to identify specific text in a document and either remove it completely or replace it with other text.

3. Extract a substring from a string based upon a pattern match- To get a string based on pattern match. we want all the words starting with "A" from a document. In this case we will use regular expression which will create pattern match and will return all words starting with "A".

Note - Regular Expression can be used to find the text strinng valus and objects with varying values. Regular Expression will be helpful when the expected value of any object or string is regularly changing.

In Vb-script we have "RegExp" class which has the following properties and methods.

Properties -

1. Global - It is used to specify pattern matching on given text until end of text.
2. Ignore Case  - It is used to specify case sensitiveness.
3. Pattern - It is used to store expected expression for matching.

Methods

1. Execute - It is used to apply given pattern on givin text and find matched values in text.

2. Replace - It is used to replace matched text with other text. 

3. Test - It is used to apply given pattern on given text for matching. If pattern was matched with text, then this method returns true. 


Properties Syntax:-

Pattern:  It is used to store expected expression for matching.

Syntax:-

set r=new regexp
r.pattern="\w"


Global: It is used to specify pattern matching on given text until end of text.

Syntax:-

set r=new regexp
r.pattern="\d"
r.global=true\false

IgnoreCase: It is used to specify case sensitiveness.

Syntax:-

Set r=new regexp
r.pattern="[A-Z][a-z]"
r.global=true
r.ignorecase=true

Methods Examples:-

Execute():-It is used to apply given pattern on given text and find matched values in text.

Example:-

Set rg=NewRegExp
rg.Pattern="[0-5]+"
rg.Global=True
String1="mno343de5rfdsdfsd105dsfjdk"
Set x=rg.Execute(string1)
ForEach y In x
    MsgBox y.value
Next


Output-
343
5
105

Test:- It is used to apply given pattern on given text for matching, if pattern was matched with text, then this method returns true.

Syntax:-

Set r=NewRegExp
r.Pattern="[0-6]+"
r.Global=True
x="34218943124981"
If r.Test(x) Then
   MsgBox "Data is matched"
   Else
   MsgBox "Mismatch in data"
EndIf

OutPut-


Data is matched
   WildCards-

Dot matches with any single characters. 

Example-  A.B  -- A2B,  APB,  A   B, A&B,   A. B.

{n}-  It matches with, n occurrences of previous characters.

Examples-  

Yaho{2}-----         Yahoo
Go{3}gle----         Google
(Zup){3}------       Zup Zup Zup 

{n,m}- It matches with minimum 'n' occurrences  and maximum 'm' occurrences of previous characters. 

Examples-

Yaho{0,2} -----      Yah
                             Yaho
                             Yahoo

(Ding){1,3}----      Ding
                             Ding Ding    
                             Ding Ding Ding

? -  It matches with Zero(0) or 1 concurrences of previous character.

Examples-  

Dinga{0,1}----      Ding
                            Dinga

- It matches with one or more occurences of previous character which is same as {1,  infinite}

Examples- 

Yaho{1,  }--          Yaho
                            Yahooo
                            Yahooooooooooooooooooo...................................

Yaho+ ---              Yaho
                            Yahooo
                            Yahoooooooooooooo...........................................

 - It matches with zero of more occurence of previous character, which is same as {0,  infinite}. 

Examples

Yaho* ----             Yah
                            Yaho
                            Yahooooooooooooooooooo...........................................

X|Y - It matches with either X(Complete Left Side) or Y(Complete Right Side).

Examples--

F|Wood --              F
                            Woood

(F|W)ood-----        Food
                            Wood

(R|C|B)at ----------Rat
                            Cat
                            Bat

[ABC]  -  It matches with any one of the character.

Examples--

[RCB]at  -----        Rat
                            Cat
                            Bat

Emp[A&B] ---        Emp A
                            Emp &
                            Emp B

[A- Z] - It matches with any one of the letter in the range. 

Examples

Emp [D - H ]  ----- Emp D
                            Emp E
                            Emp F
[0 - 9] - It matches with any one of the digit in the range . 

Examples

Emp[2 - 5] -          Emp 2
                            Emp 3
                            Emp 4
                            Emp5

Count the no of occurrences of a characters in a given string

Dim Str, ChkDup,Cnt,d
Set d = CreateObject("Scripting.Dictionary")
Str = "Community"
For i = 1 to Len(Str)
temp = 0
'Check if the key already exists
If d.Exists(mid(Str,i,1)) Then
    temp = d.Item(mid(Str,i,1)) 'Assign item to temp
    temp = CInt(temp) + 1 'Since temp is string convert to integer
    d.Item(mid(Str,i,1)) = temp 'Update the key
else
    d.Add mid(Str,i,1), 1 'if key doesn't exist, add it
End If
Next
sCharacters = d.Keys ' Get the keys.
iOccurrence = d.Items ' Get the items.
For x = 0 To d.Count-1 ' Iterate the array.
Print sCharacters(x) & " :" & iOccurrence(x)
Next


OutPut:-
C :1
o :1
m :2
u :1
n :1
i :1
t :1
y :1

Count the no of occurrences of string in a given String..?

inputstring = "This String has multiple statements and each statement has multiple occurrences"
Set o1 = new RegExp
o1.pattern = "statement"
o1.ignorecase = true
o1.global = true
Set r1 = o1.execute(inputstring)
msgbox r1.count


Out put:-
2

Monday, 28 October 2019

Text Files in UFT

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.

Create a File :

First we want create a FSO object using CreateObject and then create a text file using CreateTextFile.



Dim fso, file, file_location
file_location = “C:file_location”
Set fso = CreateObject(“Scripting.FileSystemObject”)

'True–> file is to be overwritten if it already exists else false
Set file = fso.CreateTextFile(file_location, True)  

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.

Read content from a file :

Using  ReadLine() method, we can read the content of the file.


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


Find the given string  “UFT”  in  line from the text file.

Option explicit
Dim fso,fo,l
Set fso=createobject("scripting.filesystemobject")
Set fo=fso.opentextfile("D:/Sample.txt",1,false)
While fo.atendofstream <> true
        l=fo.readline
        if instr(l,"UFT"then
           print(l)
        end if
wend
fo.close
set fo=nothing
set fso=nothing

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


To display the “hi” string lines from the Given test file. ... ?

Option explicit
Dim fso,fo,l
Set fso=createobject("scripting.filesystemobject")

Set fo=fso.opentextfile("D:/Sample.txt",1,false)
While fo.atendofstream <> true
        l=fo.readline
        if instr(l,"hi"then
           print(l)
        end if
wend

fo.close
set fo=nothing
set fso=nothing


OutPut:-
hi how are you.
hey man hi
hey raju hi

To display existing lines in specified text file....?

Option explicit
Dim fso,fo,l
Set fso=createobject("scripting.filesystemobject")
Set fo=fso.opentextfile("D:/Sample.txt")
While fo.atendofstream<>true

       l=fo.readline
          print(l)
 wend
fo.close

set fo=nothing
set fso=nothing


Output:
Hi How are you.
What you doing.
Where are you.

Find the no:of lines in given text file....?

Option explicit
Dim fso,fo,l
Set fso=createobject("scripting.filesystemobject")
Set fo=fso.opentextfile("D:/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


Output:
no:of lines in the file 3

Copy one file text into another file.....?


Option explicit
Dim fso,fo,l,fo1,fo2
Set fso=createobject("scripting.filesystemobject")
Set fo1=fso.opentextfile("D:\Sample.txt",1,false)
Set fo2=fso.opentextfile("D:\Sample1.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


Copy numbers in file1 into file2....?

Option explicit
Dim fso,fo,l,fo1,fo2,nums,r,num
Set fso=createobject("scripting.filesystemobject")
Set fo1=fso.opentextfile("D:\Sample.txt",1,false)
Set fo2=fso.opentextfile("D:\Sample1.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 nums=nothing
      set r=nothing
wend

fo1.close
fo2.close
set fo1=nothing
set fo2=nothing
set fso=nothing


Copy alphanumeric only from file1 to file2.....?

Option explicit
Dim fso,fo,l,fo1,fo2,nums,r,num
Set fso=createobject("scripting.filesystemobject")
Set fo1=fso.opentextfile("D:\Sample.txt",1,false)
Set fo2=fso.opentextfile("D:\Sample1.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
      set nums=nothing
      set r=nothing
wend

fo1.close
fo2.close
set fo1=nothing


set fo2=nothing
set fso=nothing


Copy dates in file1 into file2. Here date is “mm/dd/yy” ...?

Option explicit
Dim fso,fo,l,fo1,fo2,nums,r,num
Set fso=createobject("scripting.filesystemobject")
Set fo1=fso.opentextfile("D:\Sample.txt",1,false)
Set fo2=fso.opentextfile("D:\Sample1.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

fo1.close
fo2.close
set fo1=nothing
set fo2=nothing
set fso=nothing