Showing posts with label XMLUtil. Show all posts
Showing posts with label XMLUtil. Show all posts

Wednesday, December 5, 2012

How to compare two XML files using QTP

There are many ways you can compare two XML Files.

Here in this i demonstrated using XMLUtil.


'***************************************************************************************************************************************************************************************
'Objective              :  Compare two XML Files
'Functionality        : The below script compares two XML Files
'                                   s_FilePath1, s_FilePath1 These two variables holds the XML Files respctively
'                                   s_DiffFilePath will holds the difference between these two XML Files
'Author                    : Udaya Anem
'Scripted on       : 4th Sept
'Updated by         :
'***************************************************************************************************************************************************************************************

Dim s_FilePath1,s_FilePath2
Dim o_XML1,o_XML2,o_XMLDiff
Dim s_DiffFilePath
Dim b_RetVal

s_FilePath1="E:\Programming Samples\QTP Samples\Cars.xml" 'This is XML File Path 1
s_FilePath2="E:\Programming Samples\QTP Samples\Cars1.xml" 'This is XML File Path 2
s_DiffFilePath= "E:\Diff.xml"

'Set o_XML1=XMLUtil.CreateXML()
'o_XML1.loadfile(s_FilePath1)
'
'Set o_XML2=XMLUtil.CreateXML()
'o_XML2.loadfile(s_FilePath2)

Set o_XML1=XMLUtil.CreateXMLFromFile(s_FilePath1)
Set o_XML2=XMLUtil.CreateXMLFromFile(s_FilePath2)

'The Compare method from XMLData compares XML Files
b_RetVal=o_XML1.Compare(o_XML2,o_XMLDiff)

If b_RetVal=1 Then
    reporter.ReportEvent micPass,"XML Comparison","XML Comparison is Successful"
else
    reporter.ReportEvent micFail,"XML Comparison","XML Comparison is Failed"
    s_DiffFilePath=Environment("TestDir")&"\XML Diff File.xml"
    o_XMLDiff.savefile s_DiffFilePath
End If

Set o_XML1=nothing
Set o_XML2=nothing

XMLUtil - Part 2

I request people watch XMLUtil - Part 1, before watching this video.

http://qtpftvideos.blogspot.in/2012/12/how-to-work-with-xml-files-part-1.html

In this video i demonstrated more about how to update an XML File.
How to update XML Elements.
How to add XML attributes to an XML Element.
How to update XML attribute values.
How to add child and sub-child elements.


'***************************************************************************************************************************************************************************************
'Objective              :  Cover basic XML Operations
'Functionality        : The below script  covers the updating off XML Files
'                                  
'Author                    : Udaya Anem
'Scripted on       : 4th Dec
'Updated by         :
'***************************************************************************************************************************************************************************************
Dim o_XML
Dim s_XMLFilePath

s_XMLFilePath="E:\Programming Samples\QTP Samples\Cars.xml"
s_UpdatedXMLFilePath="E:\Programming Samples\QTP Samples\Cars2.xml"

'Set o_XML=XMLUtil.CreateXMLFromFile(s_XMLFilePath)
Set o_XML=XMLUtil.CreateXMLFromFile(s_XMLFilePath)

'The below steps update specified XML Element value
Set o_ChildPath=o_XML.ChildElementsByPath("/Cars/Make/Model/Name")
print o_ChildPath.Item(1).Value
o_ChildPath.Item(1).SetValue("Swift")
o_XML.SaveFile s_UpdatedXMLFilePath
print o_ChildPath.Item(1).Value

'The below script adds an attribute to the XML Element
Set o_ChildPath=o_XML.ChildElementsByPath("/Cars/Make/Model/Name")
o_ChildPath.Item(1).AddAttribute "Version","Petrol"
o_XML.SaveFile s_UpdatedXMLFilePath
''
''
set o_XMLAttributes=o_ChildPath.Item(1).Attributes
print o_XMLAttributes.Item(1).Value

'to update an attribute also we have to use AddAttribute
o_ChildPath.Item(1).AddAttribute "Version","Diesel"
o_XML.SaveFile s_UpdatedXMLFilePath

set o_RootElement=o_XML.GetRootElement
o_RootElement.AddChildElementByName "Make",""
Set o_ChildPath=o_XML.ChildElementsByPath("/Cars/Make")
o_ChildPath.Item(o_ChildPath.Count).AddAttribute "name","Audi"
o_ChildPath.Item(o_ChildPath.Count).AddChildElementByName "Model",""
Set o_ChildPat=o_XML.ChildElementsByPath("/Cars/Make/Model")
o_ChildPat.Item(o_ChildPat.Count).AddChildElementByName "Name","A4"
o_ChildPat.Item(o_ChildPat.Count).AddChildElementByName "Release","2002"
o_XML.SaveFile s_UpdatedXMLFilePath

Set o_ChildPath=nothing
Set o_XML=nothing

Monday, December 3, 2012

How to work with XML Files - Part 1

Here in this video i demonstrated how to traverse an XML file using "XMLUtil" utility object.
Following is the code i used in the above examples:
'***************************************************************************************************************************************************************************************
'Objective              :  Cover basic XML Operations
'Functionality        : The below script  covers the updating off XML Files
'                                  
'Author                    : Udaya Anem
'Scripted on       : 18th Sept
'Updated by         :
'***************************************************************************************************************************************************************************************
Dim o_XML
Dim s_XMLFilePath

s_XMLFilePath="E:\Programming Samples\QTP Samples\Cars.xml"
s_UpdatedXMLFilePath="E:\Programming Samples\QTP Samples\Cars2.xml"

'Set o_XML=XMLUtil.CreateXMLFromFile(s_XMLFilePath)
Set o_XML=XMLUtil.CreateXMLFromFile(s_XMLFilePath)

'The below steps update specified XML Element value
'Set o_ChildPath=o_XML.ChildElementsByPath("/Cars/Make/Model/Name")
'print o_ChildPath.Item(1).Value
'o_ChildPath.Item(1).SetValue("Alto")
'o_XML.SaveFile s_UpdatedXMLFilePath
'print o_ChildPath.Item(1).Value

'The below script adds an attribute to the XML Element
Set o_ChildPath=o_XML.ChildElementsByPath("/Cars/Make/Model/Name")
o_ChildPath.Item(1).AddAttribute "Version","Petrol"
o_XML.SaveFile s_UpdatedXMLFilePath
'
'
'set o_XMLAttributes=o_ChildPath.Item(1).Attributes
'print o_XMLAttributes.Item(1).Value

'to update an attribute also we have to use AddAttribute
o_ChildPath.Item(1).AddAttribute "Version","Diesel"
o_XML.SaveFile s_UpdatedXMLFilePath

Set o_ChildPath=nothing
Set o_XML=nothing


Following is the sample XML File:
<Cars>
  <Make name="Maruthi">
    <Model>
      <Name>Wagnor</Name>
      <Release>2000</Release>
    </Model>
  </Make>
  <Make name="Hundai">
    <Model>
      <Name>i10</Name>
      <Release>2003</Release>
    </Model>
  </Make>
  <Make name="Maruthi">
    <Model>
      <Name>Swift</Name>
      <Release>2005</Release>
    </Model>
  </Make>
</Cars>