Friday 20 June 2014

Regular Expressions use in QTP/UFT



qtp regular expression examples, qtp regular expression for numbers, qtp regular expression syntax


How to use Regular Expressions in QTP\UFT

  Regular Expression can be used to find the text strings and objects with varying values.

·         Regular Expression will be helpful when the expected value of any object or string is regularly changing but in fixed pattern.
·         These are strings which defines the search phrases on the basis of special character provided in the expression.
·         Scenarios where Regular Expression can be utilized:

      1.      Defining the property values of an object
      2.      Defining the expected values for checkpoints
      3.      Defining pop-up window conditions in recover scenario 

 Points about Regular Expression:

v  It can be created only for strings only.
v  Period (.), hyphen (-), asterisk (*), caret (^), brackets ([ ]), parentheses (()), dollar sign ($), vertical line (|), plus sign (+), question mark (?), and backslash (\) are special characters used to create RE.
v  Any of the above mentioned special characters is preceded by a backslash (\), QTP treats it as a literal character.

         Various Regular Expression Pattern used in QTP: 

Alpha Numeric Character:
1.      Matching Any Alpha Numeric Character Including the Underscore ( \w )
2.      Matching Any Non-Alpha Numeric Character (\W) will match any special character other than underscore. Please note case of W in this case.

Digit Character: 
1.      Matches a digit character (\d) matches a digit value.
2.      Matching a non-digit character (\D) matches a non digit value

    Single Character:
1.      Matching Any Single Character (.) e.g. def. Will match ‘def’ followed by any            character.
2.      Matching Any Single Character in a List ( [xy] ) e.g. [de] will match either d or e
3.      Matching Any Single Character Not in a List ( [^xy] ) e.g. 4[^56] will match all values between 41 to 49 except 45 and 46.
4.      Matching Any Single Character within a Range ( [x-y] ) e.g. : 2[1-3] will match 21,22, and 23.


Specific Characters:
1.      Matching Zero or More Specific Characters ( * ) This matches zero or more occurrences of the preceding character. e.g de* will match dee, deeee, d and so on. Similarly d.* will match d, ds, deee, and so on, since preceding character here is “.”.
2.      Matching One or More Specific Characters ( + ) Only difference from * is it will match for minimum one character. e.g se+t will match seat,set but not st as in above case.
3.      Matching Zero or One Specific Character ( ? ) A question mark (?) instructs QTP to match zero or one occurrences of the preceding character. For example: ze?s matches zes and zs, but nothing else
4.      Matching One of Several Regular Expressions ( | )  e.g date|day will match either of date or day. If we write da(t|e)ay, it will match datay or daeay.


Line:
1.      Matching the Beginning of a Line ( ^ ) This will match only if match is found at beginning of line.
2.      Matching the End of a Line ( $ )  This will match only if match is found at end of line.

Boundary:
1.      Matching a word at boundary(\b) e.g end\b will match testend but not in gendit.

Regular Expression Examples:
 There are 3 properties and 3 methods in the Regular Expression.

Properties:

1.      Pattern
2.      Global
3.      Ignorecase

Methods:
                                                                                        
1.      Execute( )
2.      Test()
3.      Replace()

Properties syntax:

1) Pattern:-  Property is used to store expected expression for matching.
    Syntax:-                                                                      
    set r=new regexp
    r.pattern=”\w”
    r.global=true/false

2) Global:- Property 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

3) Ignorecase:- Property 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:
1)  Execute( ):- Method is used to apply given pattern on given text and find matched values in text.
     Ex:-                  
     set rg=new regexp
     rg.pattern=”[0-5]+”
     rg.global=true
     st=”mno123def 34 zsdfg45gh”
     set x=rg.execute(st)
     for each y in x
          print(y.value)
     next

2) Test:-Method is used to apply given pattern on given text for matching. If pattern was matched with 
text, then this method returns true.
     Ex:-  
     set r=new regexp
     r.pattern=”[0-6]+”
     r.global=true
     x=”633544”
     if r.test(x) then
       print(“Data is matched”)
    else
      print(“Mismatch in data”)
    end if

3)  Replace( ):- Method is used to replace matched text with other text.
     Ex:-    
     set r=new regexp
     r.pattern=”[0-5]+”
     r.global=true
     x=”This is QTP machine 5”
     r.replace(x,”I”)
     print(x)

Tuesday 25 February 2014

UFT/QTP: GetRowWithCellText method

getrowwithcelltext example, getrowwithcelltext in qtp, getrowwithcelltext method in qtp


GetRowWithCellText method - find the row number on a webtable with specified cell text

Syntax

object.GetRowWithCellText(Text,[Column],[StartFromRow])

·          Column  - Optional - A Variant value. The column number where the cell is located. The value can either be the column name or index. Index values begin with 1.

·      StartFromRow - Optional. A Long value. The number of the row in which to start the search. The first row in the table is numbered 1. 

·           In case the method does not find any cell with specified text, the method returns -1 value.

In below mentioned example I will select the row where Confirmation Number is "789"

Select
Flight Name
Ticket Number
Source
Destination
Date
IA
123
Chennai
New Jersy
3-Mar-14
BA
456
New Jersey
Los Angeles
4-Mar-14
JA
789
Los Angels
Singapoer
5-Mar-14
               

--Find the row number  where "789" text is present using GetRowWithCellText method

GtRowN = Browser("XYZ").Page("OBC").WebTable("Ticket Summary").GetRowWithCellText("789")

--check text find in webtable or not

     If  GtRowN >0 Then
          -- coulmn index  where  checkbox  is present
          --here select coulmn name index value is 1
          CoulmnIndex = 1         
  
-- create the checkbox object  after getting the rownumber and select the row by clicking the checkbox

 set objCheckbox = Browser("XYZ").Page("OBC").WebTable("Ticket Summary").ChildItem(GtRowN,CoulmnIndex,"WebCheckBox",0)
      
 objCheckbox.set "ON"

    else
         msgbox "Did not find the value test fail"

     End If

Monday 27 January 2014

UFT- How to connect to SQL without DSN?

Set oDbCon=createobject("ADODB.connection")

oDbCon.open="Provider=SQLOLEDB;Data Source=computerName; Trusted_Connection=Yes;Initial Catalog=database_name;User ID=username;Password=password;"

set oData=oDbCon.execute("select * from table_name")

oFields=oData.fields.count

For oFld=0 to oFields-1

 fldName=oData.fields(oFld).name
   
 oval=""
   
  While not oData.eof
   oval=oval&oData(fldName)&vbnewline
   oData.movenext
  Wend
   
 msgbox oval,vbokonly,fldName
 oData.movefirst
   
Next
  

Tuesday 7 January 2014

UFT/QTP- Difference between ChildObjects, ChildItem and Getcelldata in QTP





childitem method syntax in qtp, childobjects and child items in qtp, childobjects syntax in qtp, getcelldata method in qtp, getcelldata qtp example


ChildObjects method is to access total child objects from an object using description object. In other words, it returns the collection of child objects contained within the object.

Syntax:- object.ChildObjects ([Description])

eg: for the count of No of links present in a page we can use Child objects method

set odesc = Description.Create()
odesc("Class Name").Value = "oLinks"
set oLinks=Browser("title:=.*").Page("title:=.*").ChildObjects("odesc")
msgbox oLinks.count
  
ChildItem method is to access child objects from a web table cell in web table object without using description object. In other words, it returns a test object from the cell by type and index.

Syntax:-object.ChildItem (Row, Column, MicClass, Index)

EG:

set a = Browser("A").Page("B").WebTable("C").WebTable("Item").ChildItem(2,2,"WebElement",0)
b = a.GetROProperty("value")
MsgBox b

Getcelldata method is to retrieve data from a web table cell in web table object.

Syntax:-object.GetCellData (Row, Column)

EG:

Dim i,j
For i=1 to Row count
  For j=1 to column count

     msgbox browser("Yahoo! Mail: The best").Page("Yahoo! Mail: The best").WebTable("Yahoo! ID:").GetCellData (i,j)

  Next
Next

UFT/QTP- Read Excel using ADODB connection

qtp adodb connection excel, qtp excel database, Read Excel using ADODB connection




Const adOpenStatic=3
Const adLockOptimistic=3
Const adCmdText= &H001

Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")

Sqlquery="Select  UserName,Password  FROM [SheetName$] where UserName='Vadivel.sekar'"

objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source="&"Excel Path" & ";" & _
"Extended Properties=""Excel 8.0;HDR=Yes;"";"

objRecordset.Open Sqlquery, objConnection, adOpenStatic, adLockOptimistic, adCmdText

Do Until objRecordset.EOF         

msgbox objRecordset.Fields.Item("UserName")
msgbox objRecordset.Fields.Item("Password")
objRecordset.MoveNext                                                
                                               

Loop

Note: e.g. Excel Path=D:\Login Info\Sample.xls