GetChildElementByAttrRegEx (QTP, TestComplete, XML, VBScript)

Posted by Albert Gareev on Apr 15, 2014 | Categories: Source codeXML Data

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


Returns child XML node with specified attribute matching specified regex.


'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)
      boolRC = Regex_Test(LCase(sValue), sAttrRegex)
    End If
    If boolRC Then
      Set GetChildElementByAttrRegEx = objNode
      Exit Function
    End If


  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

  • Leave a Reply

    * Required
    ** Your Email is never shared

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.