Forum Discussion

Kateryna's avatar
Kateryna
Contributor
12 years ago

Count keyword tests in script

Hello, does anybody know how to count the number of keyword tests in suite from the one script?
  • OK then...



    I used the following code to get the list of ManualTests. Basically, the code scans the current project file for the items that end with '.tcMT' which is the extension of the files with Manual Tests.



    I believe that you can adopt this to iterate through all project files within your test suite and search not for Manual but for Keyword tests.


    '-------------------------------------------------------------------------------



    Function GetManualTestsCollection

      Const cProcName = "GetManualTestsCollection"

      Dim cProcNameMsgPrefix

      cProcNameMsgPrefix = cUnitNameMsgPrefix & cProcName & "(): "



      Const cExt = ".tcMT"



      Dim Doc

      Dim ManualTestCollection

      Dim Nodes

      Dim Node

      Dim strManTCName

      Dim i



      Set ManualTestCollection = CreateObject("Scripting.Dictionary")

      Set GetManualTestsCollection = ManualTestCollection



      If ("" = BuiltIn.GetCOMServerPath("Msxml2.DOMDocument.4.0")) Then InstallMSXML



      ' Create COM object

      Set Doc = Sys.OleObject("Msxml2.DOMDocument.4.0")

      Doc.async = False



      ' Load data from the current project file

      Call Doc.load(Project.FileName)



      ' Report an error, if, for instance, the markup or file structure is invalid

      If (Doc.parseError.errorCode <> 0) Then

        s = "Reason:" & vbTab & Doc.parseError.reason & vbCrLf & _

            "Line:"   & vbTab & CStr(Doc.parseError.line) & vbCrLf & _

            "Pos:"    & vbTab & CStr(Doc.parseError.linePos) & vbCrLf & _

            "Source:" & vbTab & Doc.parseError.srcText

        ' Post an error to the log and exit

        Call Log.Error(cProcNameMsgPrefix & "Error when parsing the project file.", _

            Project.FileName & vbCrLf & s)

        Set Doc = Nothing

        Exit Function

      End If



      ' Use an XPath expression to obtain the list of Manual tests nodes

    '/Nodes/Node[@name="root"]/Node[@name="child list"]/Node/Node[@name="item data"]/Prp[@name="storage"]

    '/Nodes/Node//*/Prp[@name="storage"]

    '//Prp[@name="storage"]

    '  Set Nodes = Doc.selectNodes("//Prp[@name=""storage""]")

    '  Set Nodes = Doc.selectNodes("//Node[contains(@name,"".tcmt"")]")

      ' Case insensitive search for the 'name' nodes that contain the cExt ('.tcMT') extension

      ' Based on:

      ' http://stackoverflow.com/questions/614797/xpath-find-a-node-that-has-a-given-attribute-whose-value-contains-a-string

      ' http://www.dotnetspider.com/resources/470-Doing-case-InSensitve-comparisons-using-XPa-X.aspx

      'Set Nodes = Doc.selectNodes("//Node[contains(translate(@name, ""abcdefghijklmnopqrstuvwxyz"", ""ABCDEFGHIJKLMNOPQRSTUVWXYZ""),translate(""" & cExt & """, ""abcdefghijklmnopqrstuvwxyz"",""ABCDEFGHIJKLMNOPQRSTUVWXYZ""))]")



      ' Return 'name' nodes that ends on cExt value (case insensitive compare)

      ' Case insensitive search for the 'name' nodes that end on the cExt ('.tcMT') extension

      ' Based on:

      ' http://stackoverflow.com/questions/614797/xpath-find-a-node-that-has-a-given-attribute-whose-value-contains-a-string

      ' http://www.dotnetspider.com/resources/470-Doing-case-InSensitve-comparisons-using-XPa-X.aspx

      ' http://bytes.com/topic/xml/answers/726552-xpath-query-ends

      Set Nodes = Doc.selectNodes("//Node[substring(translate(@name, ""abcdefghijklmnopqrstuvwxyz"", ""ABCDEFGHIJKLMNOPQRSTUVWXYZ""), string-length(@name) - string-length(""" & cExt & """) + 1, string-length(@name)) = translate(""" & cExt & """, ""abcdefghijklmnopqrstuvwxyz"",""ABCDEFGHIJKLMNOPQRSTUVWXYZ"")]")



      ' Process the nodes

      For Each Node In Nodes

        For i = 0 To Node.attributes.length-1

    '      If ("value" = Node.attributes(i).name) Then

          If ("name" = Node.attributes(i).name) Then

            strManTCName = Node.attributes(i).text 'e.g. ManualTests\ManualTest1\ManualTest1.tcMT

    '        If (0 = aqString.Compare(aqFileSystem.GetFileExtension(cExt), _

    '            aqFileSystem.GetFileExtension(strManTCName), False)) Then ' case insensitive compare

              ' get file name (without path and ext.)

              strManTCName = Utilities.ChangeFileExt(aqFileSystem.GetFileName(strManTCName), "")

              If ("" <> strManTCName) Then ManualTestCollection.Add strManTCName, Eval(strManTCName)

    '        End If

          End If

        Next

      Next



      Set Doc = Nothing

    End Function

    '-------------------------------------------------------------------------------

     


  • AlexKaras's avatar
    AlexKaras
    Champion Level 3
    Hi Kateryna,



    Try if this works for you (DelphiScript):

    oKeywordTestsList := aqObject.GetProperties(KeywordTests);

    iKeywordTestsCount := oKeywordTestsList.Count;

    Log.Message(aqString.Format('# of Keyword tests ; %i', iKeywordTestsCount));

  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)

    Hi Kateryna,


     


    Another idea came to my mind - count the number of *.tcKDTest files in your project suite folder (if all your files are located in one location):


    var foundFiles = aqFileSystem.FindFiles("<Path to the project suite>", "*.tcKDTest", true);


      if (foundFiles != null)


      {


        Log.Message("The number of files is " + foundFiles.Count);


      }


     


    This way is simpler than Alexei's suggestion. However, the way he proposed is more general :)


     

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3
    OK then...



    I used the following code to get the list of ManualTests. Basically, the code scans the current project file for the items that end with '.tcMT' which is the extension of the files with Manual Tests.



    I believe that you can adopt this to iterate through all project files within your test suite and search not for Manual but for Keyword tests.


    '-------------------------------------------------------------------------------



    Function GetManualTestsCollection

      Const cProcName = "GetManualTestsCollection"

      Dim cProcNameMsgPrefix

      cProcNameMsgPrefix = cUnitNameMsgPrefix & cProcName & "(): "



      Const cExt = ".tcMT"



      Dim Doc

      Dim ManualTestCollection

      Dim Nodes

      Dim Node

      Dim strManTCName

      Dim i



      Set ManualTestCollection = CreateObject("Scripting.Dictionary")

      Set GetManualTestsCollection = ManualTestCollection



      If ("" = BuiltIn.GetCOMServerPath("Msxml2.DOMDocument.4.0")) Then InstallMSXML



      ' Create COM object

      Set Doc = Sys.OleObject("Msxml2.DOMDocument.4.0")

      Doc.async = False



      ' Load data from the current project file

      Call Doc.load(Project.FileName)



      ' Report an error, if, for instance, the markup or file structure is invalid

      If (Doc.parseError.errorCode <> 0) Then

        s = "Reason:" & vbTab & Doc.parseError.reason & vbCrLf & _

            "Line:"   & vbTab & CStr(Doc.parseError.line) & vbCrLf & _

            "Pos:"    & vbTab & CStr(Doc.parseError.linePos) & vbCrLf & _

            "Source:" & vbTab & Doc.parseError.srcText

        ' Post an error to the log and exit

        Call Log.Error(cProcNameMsgPrefix & "Error when parsing the project file.", _

            Project.FileName & vbCrLf & s)

        Set Doc = Nothing

        Exit Function

      End If



      ' Use an XPath expression to obtain the list of Manual tests nodes

    '/Nodes/Node[@name="root"]/Node[@name="child list"]/Node/Node[@name="item data"]/Prp[@name="storage"]

    '/Nodes/Node//*/Prp[@name="storage"]

    '//Prp[@name="storage"]

    '  Set Nodes = Doc.selectNodes("//Prp[@name=""storage""]")

    '  Set Nodes = Doc.selectNodes("//Node[contains(@name,"".tcmt"")]")

      ' Case insensitive search for the 'name' nodes that contain the cExt ('.tcMT') extension

      ' Based on:

      ' http://stackoverflow.com/questions/614797/xpath-find-a-node-that-has-a-given-attribute-whose-value-contains-a-string

      ' http://www.dotnetspider.com/resources/470-Doing-case-InSensitve-comparisons-using-XPa-X.aspx

      'Set Nodes = Doc.selectNodes("//Node[contains(translate(@name, ""abcdefghijklmnopqrstuvwxyz"", ""ABCDEFGHIJKLMNOPQRSTUVWXYZ""),translate(""" & cExt & """, ""abcdefghijklmnopqrstuvwxyz"",""ABCDEFGHIJKLMNOPQRSTUVWXYZ""))]")



      ' Return 'name' nodes that ends on cExt value (case insensitive compare)

      ' Case insensitive search for the 'name' nodes that end on the cExt ('.tcMT') extension

      ' Based on:

      ' http://stackoverflow.com/questions/614797/xpath-find-a-node-that-has-a-given-attribute-whose-value-contains-a-string

      ' http://www.dotnetspider.com/resources/470-Doing-case-InSensitve-comparisons-using-XPa-X.aspx

      ' http://bytes.com/topic/xml/answers/726552-xpath-query-ends

      Set Nodes = Doc.selectNodes("//Node[substring(translate(@name, ""abcdefghijklmnopqrstuvwxyz"", ""ABCDEFGHIJKLMNOPQRSTUVWXYZ""), string-length(@name) - string-length(""" & cExt & """) + 1, string-length(@name)) = translate(""" & cExt & """, ""abcdefghijklmnopqrstuvwxyz"",""ABCDEFGHIJKLMNOPQRSTUVWXYZ"")]")



      ' Process the nodes

      For Each Node In Nodes

        For i = 0 To Node.attributes.length-1

    '      If ("value" = Node.attributes(i).name) Then

          If ("name" = Node.attributes(i).name) Then

            strManTCName = Node.attributes(i).text 'e.g. ManualTests\ManualTest1\ManualTest1.tcMT

    '        If (0 = aqString.Compare(aqFileSystem.GetFileExtension(cExt), _

    '            aqFileSystem.GetFileExtension(strManTCName), False)) Then ' case insensitive compare

              ' get file name (without path and ext.)

              strManTCName = Utilities.ChangeFileExt(aqFileSystem.GetFileName(strManTCName), "")

              If ("" <> strManTCName) Then ManualTestCollection.Add strManTCName, Eval(strManTCName)

    '        End If

          End If

        Next

      Next



      Set Doc = Nothing

    End Function

    '-------------------------------------------------------------------------------

     


  • That's not actually what I was looking for. This script will give the number of keyword tests in current project. But I need the number of keyword tests in all projects. I have a lot of projects in one suite and it will take a long time to write a script in each project. So I'm searching for some sort of general solution...
  • Thanks a lot! I did the same VS project. Thank you so much for your help!