preload

SetXMLElementProperty (QTP, TestComplete, VBScript)

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

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

Description

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

See Test Code section for examples.

Implementation

Public Function SetXMLElementProperty(ByRef objXMLElement, ByVal sProperty, ByVal intIndex, ByVal sPropertyValue)
  Dim boolRC
  Dim sAttrName
 
  If objXMLElement is Nothing Then
  SetXMLElementProperty = False
  Exit Function
 End If
 
  If intIndex = "" Then intIndex = -1
  If Not isNumeric(intIndex) Then
  SetXMLElementProperty = 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"
      objXMLElement.Text = sPropertyValue
      boolRC = True
    Case "#ATTR:"
      boolRC = p_SetXMLElementAttrText(objXMLElement, sAttrName, sPropertyValue)
    Case "#TEXT"
      If intIndex = -1 Then
        boolRC = p_SetXMLElementText(objXMLElement, sPropertyValue)
      Else
        boolRC = p_SetXMLElementTextByIndex(objXMLElement, intIndex, sPropertyValue)
      End If
    Case "#COMMENT"
      If intIndex = -1 Then
        boolRC = p_SetXMLElementComment(objXMLElement, sPropertyValue)
      Else
        boolRC = p_SetXMLElementCommentByIndex(objXMLElement, intIndex, sPropertyValue)
      End If
    Case "#CDATA"
      If intIndex = -1 Then
        boolRC = p_SetXMLElementCData(objXMLElement, sPropertyValue)
      Else
        boolRC = p_SetXMLElementCDataByIndex(objXMLElement, intIndex, sPropertyValue)
      End If
    Case Else
    boolRC = False
  End Select
 
  SetXMLElementProperty = boolRC

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.