Saturday, August 8, 2015

Regular Expression with RegExp

Practically We can use RegExp Object basically in QTP for following purpose.

1. To find the regular expression from the string
2. And to replace the match value from the string

This Object have some properties and Methods.

Properties:
IgnoreCase:  By default regular expression is case sensitive, to make it case insensitive, set value as “True”
Pattern: We can define the pattern of regular expression in the pattern property. Pattern can include literal and meta characters like .,*,/d 
Global: To return or replace all matches, set Global as True. If set as False, finds the first match only

Methods:
Execute and Replace are the Methods that can be use by RegExp Object
   Execute method has following properties:
o    Item
Item.value – value of item in the collection
Firstindex – First instance of match location
o    Count – Count of instances matching the regular expression
Below code will give you an idea how we can use regular expression:

Set newRegExp = New RegExp
newRegExp.IgnoreCase = True
newRegExp.Global = True
newRegExp.Pattern = ".*a" 'This is the pattern that we have to search into the string
testString = "zeeshan" 'This is the string where we have to perform our search
Set colmatch = newRegExp.Execute(teststring)
For each match in colmatch
Print "Match Value at Fist Location--" &match.Value
print "Total Count for matches--" &colmatch.count
Next
boolMatch = newRegExp.Test(teststring)
Print boolMatch
newRegExp.pattern ="a"
strString = newRegExp.replace(teststring,"b")
Print "Replace String is --"& strstring

Output:

HOW TO WORK WITH DOM IN QTP

HTML DOM(Documnet object model) is language independent model for representing and interacting with objects in html. It defines the structure for HTML and help in traversing through objects in HTML.
This approach would be helpful when QTP is not able to identify the object.

Lets try to explain some methods that are useful in QTP when we work with DOM

1.getelementbyid
2.getelementbyname
3.getelementbytagname

Lets open the google home page and select the html code for the Search button by pressing the F12 Key than you will get the html code.







In the above code search button, tag name is input so , by using this tagname we can perform the click operation on this.

if we use to find the element with the use of tag name it will return all the objects that is having the tagname as input for that we have to use some trick in the program, how let see below code

'Code to click on the Search button of google with the use of tagname

objname ="btnG"
set btnname =  Browser("Google").Page("Google").Object.getElementsByTagName("input")
For each btn in btnname

If (btn.name =objname) Then
btn.click
Print "Button found"
else
Print "Button not found"
End If

Next

2. Similary we can use the getelementbyid and getelementbyname method for DOM