Tuesday, February 23, 2010

Generating Random Data

Do you want to generate random data for your test? Try the below methods.
Method #1:

Set objRandm = CreateObject("Scriptlet.TypeLib")
strGUID = objRandm.Guid
Msgbox strGUID

Method #2:

Set oGuid=DotNetFactory.CreateInstance("System.Guid","mscorlib")
Set oConvert=DotNetFactory.CreateInstance("System.Convert","mscorlib")

temp= oConvert.ToBase64String(oGuid.NewGuid().ToByteArray())
temp=Left(temp,22)
temp=Replace(temp,"/", "_")
strRandm=Replace(temp,"+", "-")
Msgbox strRandm
This method is as same as previous one, but length of the string is shorter.
Method #3:

Msgbox hex(( ( (timer+rnd(1)) *100) + int(rnd(1)*16)*&hf0000 ) mod &h100000 )
The above code generates semi-unique random strings.

Getting Attached Functional Libraries' And Data Table's Path

Do you want to get the attached functional libraries' and data table's path? Try the below snippets.

Getting Functional Libraries List:
Msgbox Setting("TestFuncLibFilesList")
Getting Data Table's Path:
Msgbox Setting("DataTablePath")

Wednesday, February 17, 2010

Accessing Web Objects Using DOM Methods

ElementFromPoint Method
x=Browser("name:=Google").Page("title:=Google").WebEdit("name:=q").GetROProperty("x")
y=Browser("name:=Google").Page("title:=Google").WebEdit("name:=q").GetROProperty("y")
Browser("name:=Google").Page("title:=Google").Object.elementFromPoint(x,y).Value="QTP" 'Sets value in web edit using coordinates

GetElementsByTagName Method
Set obj= Browser("name:=Google").Page("title:=Google").Object.getElementsByTagName("INPUT")
inCount=obj.Length-1
For i=0 to inCount
If obj(i).Name="q" and obj(i).Type="text" Then
Browser("name:=Google").Page("title:=Google").Object.getElementsByTagName("INPUT")(i).Value="QTP" 'Sets value in web edit using tag name.
End If
Next

GetElementsByName Method
Set obj= Browser("name:=Google").Page("title:=Google").Object.getElementsByTagName("INPUT")
inCount=obj.Length-1
For i=0 to inCount
If obj(i).Name="q" and obj(i).Type="text" Then
Browser("name:=Google").Page("title:=Google").Object.getElementsByName(obj(i).Name)(0).Value="QTP" 'Sets value in web edit using element's name.
End If
Next

GetElementByID Method
Browser("name:=Google").Page("title:=Google").Object.getElementByID("XXXX").Value="QTP" 'Sets value in web edit using element's ID

Verifying Child Object Existence Using Contains Method
Set objWebEdit=Browser("name:=Google").Page("title:=Google").WebEdit("name:=q").Object
msgbox Browser("name:=Google").Page("title:=Google").webTable("index:=0").Object.contains(objWebEdit)'If the webtable contains the specified child object, then Contains method will return True.

Friday, February 5, 2010

XML DOM and Xpath Snippets

In this article I am going to explain about the XML DOM methods and Xpath queries.

The Example XML Document Used For This Article



#1: Finding Elements by Attributes
If you want to access the name of the employee two, write attribute filter condition in your xpath query.
/Employees/Employee[@id='002']

Code:
gStrOrPath="C:\ Test.xml"'Path of your XML
Set gObjXmlOR = CreateObject( "Microsoft.XMLDOM")
gObjXmlOR.Async = "False"
gObjXmlOR.Load(gStrOrPath)
StrXQuery="/Employees/Employee[@id='002']" ' Attribute filter condition in your Xpath query
Msgbox gObjXmlOR.documentElement.selectSingleNode(StrXQuery).Text


The msgbox displays the child element’s text (I.e. Peter).

#2: Finding Elements with Text
Here I have written Xpath query to find the employee Peter’s gender using his name.
/Employees/Employee/name[.='Peter']

Code:
StrXQuery ="/Employees/Employee/name[.='Peter']" ' Text filter condition
Msgbox gObjXmlOR.documentElement.selectSingleNode(StrXQuery). Attributes.getNamedItem("gender").Text


The msgbox displays Sam’s gender as in the XML.

#3: Checking an Element has child or not
To find an element has child or not, then use XML Dom hasChildNodes method.
As per the example XML document, I am finding the employee node has child or not.

StrXQuery="/Employees/Employee[@id='002']
Msgbox gObjXmlOR.documentElement.selectSingleNode(StrXQuery).hasChildNodes


#4: Checking Errors in XML

Set gObjXmlOR = CreateObject("Microsoft.XMLDOM")
gObjXmlOR.async = False
gObjXmlOR.load("Test.xml")
If gObjXmlOR.parseError.errorCode <> 0 Then
MsgBox("Parse Error line " & gObjXmlOR.parseError.line & ", character " & _
gObjXmlOR.parseError.linePos & vbCrLf & gObjXmlOR.parseError.srcText)

End If