preload

GetXMLElementProperty (QTP, TestComplete, VBScript)

Posted by Albert Gareev on Aug 12, 2008 | Categories: Source codeXML Data

Parent page: Service Functions – MSXMLDOM (QTP, TestComplete, VBScript)

Description

Dispatcher type function. Reads child node by name: attribute, text, comment, CData

See Test Code section for examples.

Implementation

Public Function GetXMLElementProperty(ByRef objXMLElement, ByVal sProperty, ByVal intIndex, ByRef sPropertyValue)
  Dim sAttrName

 If objXMLElement is Nothing Then
  GetXMLElementProperty = False
  Exit Function
 End If
 
  If intIndex = "" Then intIndex = -1
  If Not isNumeric(intIndex) Then
  GetXMLElementProperty = False
  Exit Function
 End If
 
  If Left(UCase(sProperty), 6) = "#ATTR:" Then
    sAttrName = Mid(sProperty, 7)
    sProperty = "#ATTR:"
  Else
    sProperty = UCase(sProperty)
  End If
 
  Select Case sProperty
    Case "INNERTEXT"
      sPropertyValue = objXMLElement.Text
    Case "#ATTR:"
      sPropertyValue = p_GetXMLElementAttrText(objXMLElement, sAttrName)
    Case "#TEXT"
      If intIndex = -1 Then
        sPropertyValue = p_GetXMLElementText(objXMLElement)
      Else
        sPropertyValue = p_GetXMLElementTextByIndex(objXMLElement, intIndex)
      End If
    Case "#COMMENT"
      If intIndex = -1 Then
        sPropertyValue = p_GetXMLElementComment(objXMLElement)
      Else
        sPropertyValue = p_GetXMLElementCommentByIndex(objXMLElement, intIndex)
      End If
    Case "#CDATA"
      If intIndex = -1 Then
        sPropertyValue = p_GetXMLElementCData(objXMLElement)
      Else
        sPropertyValue = p_GetXMLElementCDataByIndex(objXMLElement, intIndex)
      End If
    Case Else
      sPropertyValue = ""
    GetXMLElementProperty = False
    Exit Function
  End Select
 
  GetXMLElementProperty = True

End Function

Test Code

  Set objXMLDoc = CreateXMLDOM("test")
  If objXMLDoc is Nothing Then
    Log.Error "CreateXMLDOM failed"
  End If
  Set objXMLParent = CreateChildElementByName(objXMLDoc.DocumentElement, "case", "test case")
  If objXMLParent is Nothing Then
    Log.Error "CreateChildElementByName failed"
  End If
  Set objXMLNode = CreateChildElementByName(objXMLParent, "step", "test step")
  If objXMLParent is Nothing Then
    Log.Error "CreateChildElementByName failed"
  End If
  If objXMLNode.BaseName <> "step" Then
    Log.Error "CreateChildElementByName failed"
  End If
  If objXMLNode.Text <> "test step" Then
    Log.Error "CreateChildElementByName failed"
  End If
                                                      
  Set objXMLNode = ChildElementByName(objXMLParent, "case") 
  If Not (objXMLNode is Nothing) Then
    Log.Error "ChildElementByName failed"
  End If
  Set objXMLNode = ChildElementByName(objXMLParent, "step") 
  If objXMLNode is Nothing Then
    Log.Error "ChildElementByName failed"
  End If
  If objXMLNode.BaseName <> "step" Then
    Log.Error "ChildElementByName failed"
  End If
  If objXMLNode.Text <> "test step" Then
    Log.Error "ChildElementByName failed"
  End If
 
  Log.Message("XMLDOM XML Node Property Objects")
 
  boolRC = GetXMLElementProperty(objXMLNode, "innertext", "", sValue)
  boolRC = boolRC AND (sValue = "test step") 
  If Not boolRC Then
    Log.Error "GetXMLElementProperty failed"
  End If
  boolRC = GetXMLElementProperty(objXMLNode, "#text", "", sValue)
  boolRC = boolRC AND (sValue = "test step") 
  If Not boolRC Then
    Log.Error "GetXMLElementProperty failed"
  End If
  boolRC = GetXMLElementProperty(objXMLNode, "#text", -1, sValue)
  boolRC = boolRC AND (sValue = "test step") 
  If Not boolRC Then
    Log.Error "GetXMLElementProperty failed"
  End If
 
  boolRC = SetXMLElementProperty(objXMLNode, "innertext", -1, "step instructions")
  If Not boolRC Then
    Log.Error "SetXMLElementProperty failed"
  End If
  boolRC = GetXMLElementProperty(objXMLNode, "#text", -1, sValue)
  boolRC = boolRC AND (sValue = "step instructions") 
  If Not boolRC Then
    Log.Error "GetXMLElementProperty failed"
  End If

  boolRC = SetXMLElementProperty(objXMLNode, "#Attr:id", "", "123-456")
  If Not boolRC Then
    Log.Error "SetXMLElementProperty failed"
  End If
  boolRC = GetXMLElementProperty(objXMLNode, "#Attr:ID", "", sValue)
  boolRC = boolRC AND (sValue = "123-456") 
  If boolRC Then
    Log.Error "GetXMLElementProperty failed"
  End If
  boolRC = GetXMLElementProperty(objXMLNode, "#Attr:id", "", sValue)
  boolRC = boolRC AND (sValue = "123-456") 
  If Not boolRC Then
    Log.Error "GetXMLElementProperty failed"
  End If
  boolRC = SetXMLElementProperty(objXMLNode, "#Attr:Id", "", "AXBN")
  If Not boolRC Then
    Log.Error "SetXMLElementProperty failed"
  End If
  boolRC = GetXMLElementProperty(objXMLNode, "#Attr:Id", "", sValue)
  boolRC = boolRC AND (sValue = "AXBN") 
  If Not boolRC Then
    Log.Error "GetXMLElementProperty failed"
  End If

  boolRC = SetXMLElementProperty(objXMLNode, "#text", 0, "entry1")
  If Not boolRC Then
    Log.Error "SetXMLElementProperty failed"
  End If
  boolRC = SetXMLElementProperty(objXMLNode, "#text", 1, "entry2")
  If Not boolRC Then
    Log.Error "SetXMLElementProperty failed"
  End If
  boolRC = GetXMLElementProperty(objXMLNode, "#text", -1, sValue)
  boolRC = boolRC AND (sValue = "entry1entry2") 
  If Not boolRC Then
    Log.Error "GetXMLElementProperty failed"
  End If
  boolRC = SetXMLElementProperty(objXMLNode, "#text", 5, "entry5loop")
  If Not boolRC Then
    Log.Error "SetXMLElementProperty failed"
  End If
  boolRC = GetXMLElementProperty(objXMLNode, "#text", -1, sValue)
  boolRC = boolRC AND (sValue = "entry1entry2entry5loopentry5loopentry5loopentry5loop") 
  If Not boolRC Then
    Log.Error "GetXMLElementProperty failed"
  End If
  boolRC = SetXMLElementProperty(objXMLNode, "#text", "", "textentry")
  If Not boolRC Then
    Log.Error "SetXMLElementProperty failed"
  End If
  boolRC = GetXMLElementProperty(objXMLNode, "#text", 0, sValue)
  boolRC = boolRC AND (sValue = "textentry") 
  If Not boolRC Then
    Log.Error "GetXMLElementProperty failed"
  End If
  boolRC = GetXMLElementProperty(objXMLNode, "#text", -1, sValue)
  boolRC = boolRC AND (sValue = "textentry") 
  If Not boolRC Then
    Log.Error "GetXMLElementProperty failed"
  End If

  sValue = ChildElementValueByName(objXMLParent, "step")
  If (sValue <> "textentry") Then
    Log.Error "ChildElementValueByName failed"
  End If
 
  boolRC = SetXMLElementProperty(objXMLNode, "#comment", 0, "comment1")
  If Not boolRC Then
    Log.Error "SetXMLElementProperty failed"
  End If
  boolRC = SetXMLElementProperty(objXMLNode, "#comment", 2, "comment2")
  If Not boolRC Then
    Log.Error "SetXMLElementProperty failed"
  End If
  boolRC = GetXMLElementProperty(objXMLNode, "#comment", "", sValue)
  boolRC = boolRC AND (sValue = "comment1comment2comment2") 
  If Not boolRC Then
    Log.Error "GetXMLElementProperty failed"
  End If

  boolRC = SetXMLElementProperty(objXMLNode, "#cdata", 0, "cd-section1")
  If Not boolRC Then
    Log.Error "SetXMLElementProperty failed"
  End If
  boolRC = SetXMLElementProperty(objXMLNode, "#cdata", 2, "cd-section2")
  If Not boolRC Then
    Log.Error "SetXMLElementProperty failed"
  End If
  boolRC = GetXMLElementProperty(objXMLNode, "#cdata", "", sValue)
  boolRC = boolRC AND (sValue = "cd-section1cd-section2cd-section2") 
  If Not boolRC Then
    Log.Error "GetXMLElementProperty failed"
  End If

  Set objXMLNode = SetChildElementByAttr(objXMLParent, "step", "alpha", "System Step")
  If objXMLNode is Nothing Then
    Log.Error "SetChildElementByAttr failed"
  End If
  boolRC = GetXMLElementProperty(objXMLNode, "#text", -1, sValue)
  boolRC = boolRC AND (sValue = "System Step") 
  If Not boolRC Then
    Log.Error "SetChildElementByAttr failed"
  End If

  sValue = GetChildElementValueByAttr(objXMLParent, "step", "alpha")
  If sValue <> "System Step" Then
    Log.Error "GetChildElementValueByAttr failed"
  End If


Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported
This work by Albert Gareev is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported.