GetChildElementByAttrRegEx (QTP, TestComplete, XML, VBScript)
Parent page: Service Functions – MSXMLDOM (QTP, TestComplete, VBScript)
Description
Returns child XML node with specified attribute matching specified regex.
Implementation
'Returns FIRST occurrence
Public Function GetChildElementByAttrRegEx(ByRef objXMLParent, ByVal sTagName, ByVal sAttrName, ByVal sAttrRegex, ByVal objParameter)
	Dim objColl, objNode
  Dim Iter, sValue
  Dim boolMatchCase, boolRC
	If objXMLParent is Nothing Then
		Set GetChildElementByAttrRegEx = Nothing
		Exit Function
	End If
	If TypeName(objParameter) <>  "Dictionary" Then
		Set objParameter = CreateObject("Scripting.Dictionary")
	End If
  boolMatchCase = InitBool(Trim(objParameter.Item("p.matchcase")), False)
  If Not boolMatchCase Then
    sAttrRegex = LCase(sAttrRegex)
  End If
	Set objColl = objXMLParent.SelectNodes("./"&sTagName)
  For Iter = 0 To objColl.Length-1
		Set objNode = objColl.Item(Iter)
		Call GetXMLElementProperty(objNode, "#ATTR:" & sAttrName, -1, sValue)
    If boolMatchCase Then
      boolRC = Regex_Test(sValue, sAttrRegex)
    Else
      boolRC = Regex_Test(LCase(sValue), sAttrRegex)
    End If
    If boolRC Then
      Set GetChildElementByAttrRegEx = objNode
      Exit Function
    End If
  Next
  Set GetChildElementByAttrRegEx = Nothing
End Function
Unit Tests
'Unit tests use other functions from the parent library
'Reporting example is done in TestComplete syntax
  Set objXMLNode = SetChildElementByAttr(objXMLParent, "step", "alpha", "System Step 1")
  Set objXMLNode = SetChildElementByAttr(objXMLParent, "step", "betta", "System Step 2")
  Set objXMLNode = SetChildElementByAttr(objXMLParent, "step", "gamma", "System Step 3")
  Set objXMLNode = GetChildElementByAttrRegEx(objXMLParent, "step", "name", "tt", Nothing)
  If objXMLNode is Nothing Then
    Log.Error "GetChildElementByAttrRegEx failed"
  End If
  boolRC = GetXMLElementProperty(objXMLNode, "#text", -1, sValue)
  boolRC = boolRC AND (sValue = "System Step 2")
  If Not boolRC Then
    Log.Error "GetChildElementByAttrRegEx failed"
  End If
 

