Forum Discussion

edgarh88's avatar
edgarh88
New Contributor
3 years ago
Solved

How to Validate recently downloaded output Excel Files

Hi all, 

 

I´m currently working in some scenarios which requires to input some information into an Excel file, upload this file into the system, wait for the system to process such file and after that download the output Excel file and run some validations on this file. 

 

Is there any possible way to directly run validations on the recently downloaded Excel file without having Test Complete previously directed to the path of this file?

The file name increases by one number at the end after each download.  

  • Hello edgarh88 - 

     

    This could be accomplished by using a variable in the path for the excel file - are you currently using keyword tests or scripts in TestComplete?

     

    Could you show me where you have the path and i could explain how best to do this?

     

    Thanks, 


    Emma

  • Hi edgarh88, @

     

    To expand upon Emma's (ebarbera) suggestion, you could get the most recently created file from a directory.  This function will allow you to pass in the directory and file extension and will return the most recent created file path.  I added a test function below to show how to pass in your parameters.  I hope this helps.

     

        function getMostRecentCreatedFile(directoryPath, searchPattern)

        {

          var newestFilePath = "";

          var previousCreationTime = "1/1/1900";

          try

          {

            var files = aqFileSystem.FindFiles(directoryPath, searchPattern, false);

            if (files != null)

            {         

              while(files.HasNext())

              {

                var file = files.Next();

                var creationTime = aqFile.GetCreationTime(file.Path);

                if (aqDateTime.Compare(creationTime, previousCreationTime) == 1)

                {

                  previousCreationTime = creationTime;

                  newestFilePath = file.Path;

                }

              }

            }

          }

          catch(e)

          {

            Log.Error("Exception in getMostRecentCreatedFile", e.descrpition);

          }

          //

          return newestFilePath;

        }

     

     

        function testGetFile()

        {

          var filePath = getMostRecentCreatedFile("C:\\Users\\herrerae\\Downloads", "*.xlsx");

        }

5 Replies

  • Hello edgarh88 - 

     

    This could be accomplished by using a variable in the path for the excel file - are you currently using keyword tests or scripts in TestComplete?

     

    Could you show me where you have the path and i could explain how best to do this?

     

    Thanks, 


    Emma

    • edgarh88's avatar
      edgarh88
      New Contributor

      Hi ebarbera, 

       

      I´m currently using keyword tests and yes I think a Script would be the best approach for this validations.

       

      The file will go to my downloads folder but I can change it to any specific folder if it needed. 

       

      Here´s an example of my actual path:

      C:\Users\herrerae\Downloads\Error_MANAGE_CREATE_PARTY (47) (5).xlsx

      • ebarbera's avatar
        ebarbera
        Staff

        Hi edgarh88 - 

         

        What is the scripting language in your current project?

         

        This example is in javascript: 

         

        function FilePath()
        {
        Project.Variables.UniqueUserNum = ++Project.Variables.FileNum
        Project.Variables.FilePath = "C:\Users\herrerae\Downloads\Error_MANAGE_CREATE_PARTY (47) (" + Project.Variables.FileNum + ").xlsx"

        }

         

        But i would suggest writing a short script like this and running it as part of a Keyword test - you can do this by dragging a "Run Script Routine" operation into your test.

         

        You will have to add the respective values to the project variables the way this is set up.

         

        Then you will have to change the path like this:

        Let me know if you have any questions!

         

        Thanks, 

        Emma

         

         

  • Hi edgarh88, @

     

    To expand upon Emma's (ebarbera) suggestion, you could get the most recently created file from a directory.  This function will allow you to pass in the directory and file extension and will return the most recent created file path.  I added a test function below to show how to pass in your parameters.  I hope this helps.

     

        function getMostRecentCreatedFile(directoryPath, searchPattern)

        {

          var newestFilePath = "";

          var previousCreationTime = "1/1/1900";

          try

          {

            var files = aqFileSystem.FindFiles(directoryPath, searchPattern, false);

            if (files != null)

            {         

              while(files.HasNext())

              {

                var file = files.Next();

                var creationTime = aqFile.GetCreationTime(file.Path);

                if (aqDateTime.Compare(creationTime, previousCreationTime) == 1)

                {

                  previousCreationTime = creationTime;

                  newestFilePath = file.Path;

                }

              }

            }

          }

          catch(e)

          {

            Log.Error("Exception in getMostRecentCreatedFile", e.descrpition);

          }

          //

          return newestFilePath;

        }

     

     

        function testGetFile()

        {

          var filePath = getMostRecentCreatedFile("C:\\Users\\herrerae\\Downloads", "*.xlsx");

        }

    • edgarh88's avatar
      edgarh88
      New Contributor

      Thank you Chris, I have implemented this solution and it works great for my Test cases. 

       

      I appreciate your help!