Forum Discussion

littlebear's avatar
littlebear
Contributor
31 days ago

File System Testing

Hello,

I am new to doing any testing outside of browser based testing, but I have been asked to make a test that works exclusively within a Windows file system. The test would involve opening the file system, opening a file within it, checking the contents or properties of the file, and then leaving and closing the system.

Could someone please point me to TestComplete's documentation for this kind of test, if there is any?

Thank you,

  • use aqFile object and aqFileSystem. They work great. here are the links and some start functions if you are scripting in Javascript. If not javascript just ask chat GPT to convert the below functions into whatever script language you are using

    https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqfilesystem/index.html

    https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqfile/index.html

    function GetFileSize(directory, fileName)
    {
      var files = aqFileSystem.FindFiles(directory, fileName);
      
      // 1. find file
      if(files !== null && files.length !== 0)
      {
        var size = files.Item(0).Size;
        
        // 3. return 'number' value
        return size;
      }
      else
      {
        var msg = `Could not find the file, '${fileName}', in directory, '${directory}'.`;
        Log.Error(msg); 
      }
    }
    
    
    function VerifyFileSize(directory, fileName, expectedFileSize, fileSizeTolerance)
    {
      var actualFileSize = GetFileSize(directory, fileName);
      if(Math.abs(expectedFileSize - actualFileSize) >  fileSizeTolerance)
      {
        var difference = Math.abs(expectedFileSize - actualFileSize);
      
        var msg = `The actual file size is not close enough to the expected file size` + 
                  `| actual: '${actualFileSize}' | expected: '${expectedFileSize}' | ` + 
                  `size difference: '${difference}' | desired tolerance: '${fileSizeTolerance}'.` + 
                  `Directory: '${directory}' | fileName: '${fileName}'` ;
        Log.Error(msg);
      }
      else
      {
        Log.Message('File size compare complete. File sizes matched.')
      }
    }
    
    
    function DeleteFileIfExists(directory, fileName, checkIfSuccess = false)
    {
      // 1. Delete file if exists
      var fullPath = StrHelper.ConcatFileNameAndPath(directory, fileName);
      if(aqFileSystem.Exists(fullPath))
      {
        aqFile.Delete(fullPath);
      }
      
      // 2. If we need to check to see if file was deleted, check now ...
      if(checkIfSuccess === true)
      {
        // Check to see if file was deleted?
        fileWasDeleted = IsDeleted(fullPath);
        if(fileWasDeleted === false)
        {
          Log.Warning(`File, '${fullPath}', was not deleted`);
          return false;
        }
        else
        {
          return true;
        }
      }
      
      return true;
    }
    
    
    function VerifyFileExistance(fileNameAndPath, maxWaitTimeSec)
    {
      var fileExists = aqFileSystem.Exists(fileNameAndPath);
      
      // 2. Wait until file exists
      var crntWaitSecond = 0;
      while(!fileExists && crntWaitSecond <= maxWaitTimeSec)
      {
        Delay(1000);
        crntWaitSecond++;
        fileExists = aqFileSystem.Exists(fileNameAndPath);
      }
      
      // 3. Fail if file not found
      if(!fileExists)
      {
        var msg = `Could not find the specified file, ${fileNameAndPath}.`;
        Log.Error(msg);
      }  
    }
    
    
    function IsDeleted(fileNamepath, maxWaitTimeMiliSec = 2000)
    {
      var fileExists = aqFileSystem.Exists(fileNamepath);
      
      // Wait until file or folder is deleted
      var crntWaitSecondMs = 0;
      while(fileExists && crntWaitSecondMs <= maxWaitTimeMiliSec)
      {
        Delay(200);
        crntWaitSecondMs = crntWaitSecondMs + 200;
        fileExists = aqFileSystem.Exists(fileNamepath);
      }
      
      return !fileExists;
    }
    
    
    function ReadFileIntoString(fileNameAndPath)
    {
      VerifyFileExistance(fileNameAndPath);
      
      var fileString = aqFile.ReadWholeTextFile(fileNameAndPath, aqFile.ctUTF8);
      
      return fileString;
    }
    
    function GetFileTimeStamp(baseWorkingDirectory, caseName, fileName)
    {
      var fileNamePath = StrHelper.Concat_BaseDirectory_FileFolder_and_FileName(baseWorkingDirectory, caseName, fileName);
      VerifyFileExistance(fileNamePath, 4);
      var fileTimeStamp = aqFile.GetLastWriteTime(fileNamePath);
      
      return fileTimeStamp;
    }
    
    
    function GetFileTimeStampString(baseWorkingDirectory, caseName, fileName)
    {
      var timeStamp = GetFileTimeStamp(baseWorkingDirectory, caseName, fileName);
      var timeStampString = aqConvert.DateTimeToStr(timeStamp);
      
      return timeStampString;
    }
    
    
    
    // ************* the Below code put into a file called 'StringHelper' *************
    
    
    function Concat_BaseDirectory_FileFolder_and_FileName(baseDirectory, fileFolder, fileName)
    {
      // base directory
      baseDirectory = baseDirectory.trim();
      var lastChar = baseDirectory[baseDirectory.length - 1]; 
      if(lastChar !== "\\")
      {
        baseDirectory = baseDirectory + "\\";
      }  
      
      // file folder
      fileFolder = fileFolder.trim();
      var folderFirstChar = fileFolder[0];
      var folderLastChar = fileFolder[fileFolder.length - 1];
      if(folderFirstChar === "\\" || folderFirstChar === "/")
      {
        fileFolder = fileFolder.substring(1, fileFolder.length);
      }
      if(folderLastChar === "\\" || folderLastChar === "/")
      {
        fileFolder = fileFolder.substring(0, fileFolder.length-1);
      }
      
      var fileNameAndPath = baseDirectory + fileFolder + "\\" + fileName.trim();
      
      return fileNameAndPath;
    }
    
    
    function Concat_BaseDirectory_and_FileFolder(baseDirectory, fileFolder)
    {
      if(fileFolder === "")
      {
        Log.Error("The variable, 'v_TestName', cannot be empty. Please fill in and re-run.")
      }
    
      // base directory
      baseDirectory = baseDirectory.trim();
      var lastChar = baseDirectory[baseDirectory.length - 1]; 
      if(lastChar !== "\\")
      {
        baseDirectory = baseDirectory + "\\";
      }  
      
      // file folder
      fileFolder = fileFolder.trim();
      var folderFirstChar = fileFolder[0];
      var folderLastChar = fileFolder[fileFolder.length - 1];
      if(folderFirstChar === "\\" || folderFirstChar === "/")
      {
        fileFolder = fileFolder.substring(1, fileFolder.length);
      }
      if(folderLastChar === "\\" || folderLastChar === "/")
      {
        fileFolder = fileFolder.substring(0, fileFolder.length-1);
      }
      
      var fullPath = baseDirectory + fileFolder;
      
      return fullPath;
    }
    
    
    function ConcatFileNameAndPath(directory, fileName)
    {
      let fileNameAndPath = directory;
      let lastChar = directory[directory.length - 1];  
      if(lastChar !== "\\")
      {
        fileNameAndPath = directory + "\\" + fileName;
      }
      else
      {
        fileNameAndPath = directory + fileName;
      }  
      
      return fileNameAndPath;
    }
    

     

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    If you do an internet search for "testcomplete file" you will come across various results, one of them pointing to aqFile object. There will be similar objects which relates to files within the documentation.

  • torus's avatar
    torus
    Contributor

    use aqFile object and aqFileSystem. They work great. here are the links and some start functions if you are scripting in Javascript. If not javascript just ask chat GPT to convert the below functions into whatever script language you are using

    https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqfilesystem/index.html

    https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqfile/index.html

    function GetFileSize(directory, fileName)
    {
      var files = aqFileSystem.FindFiles(directory, fileName);
      
      // 1. find file
      if(files !== null && files.length !== 0)
      {
        var size = files.Item(0).Size;
        
        // 3. return 'number' value
        return size;
      }
      else
      {
        var msg = `Could not find the file, '${fileName}', in directory, '${directory}'.`;
        Log.Error(msg); 
      }
    }
    
    
    function VerifyFileSize(directory, fileName, expectedFileSize, fileSizeTolerance)
    {
      var actualFileSize = GetFileSize(directory, fileName);
      if(Math.abs(expectedFileSize - actualFileSize) >  fileSizeTolerance)
      {
        var difference = Math.abs(expectedFileSize - actualFileSize);
      
        var msg = `The actual file size is not close enough to the expected file size` + 
                  `| actual: '${actualFileSize}' | expected: '${expectedFileSize}' | ` + 
                  `size difference: '${difference}' | desired tolerance: '${fileSizeTolerance}'.` + 
                  `Directory: '${directory}' | fileName: '${fileName}'` ;
        Log.Error(msg);
      }
      else
      {
        Log.Message('File size compare complete. File sizes matched.')
      }
    }
    
    
    function DeleteFileIfExists(directory, fileName, checkIfSuccess = false)
    {
      // 1. Delete file if exists
      var fullPath = StrHelper.ConcatFileNameAndPath(directory, fileName);
      if(aqFileSystem.Exists(fullPath))
      {
        aqFile.Delete(fullPath);
      }
      
      // 2. If we need to check to see if file was deleted, check now ...
      if(checkIfSuccess === true)
      {
        // Check to see if file was deleted?
        fileWasDeleted = IsDeleted(fullPath);
        if(fileWasDeleted === false)
        {
          Log.Warning(`File, '${fullPath}', was not deleted`);
          return false;
        }
        else
        {
          return true;
        }
      }
      
      return true;
    }
    
    
    function VerifyFileExistance(fileNameAndPath, maxWaitTimeSec)
    {
      var fileExists = aqFileSystem.Exists(fileNameAndPath);
      
      // 2. Wait until file exists
      var crntWaitSecond = 0;
      while(!fileExists && crntWaitSecond <= maxWaitTimeSec)
      {
        Delay(1000);
        crntWaitSecond++;
        fileExists = aqFileSystem.Exists(fileNameAndPath);
      }
      
      // 3. Fail if file not found
      if(!fileExists)
      {
        var msg = `Could not find the specified file, ${fileNameAndPath}.`;
        Log.Error(msg);
      }  
    }
    
    
    function IsDeleted(fileNamepath, maxWaitTimeMiliSec = 2000)
    {
      var fileExists = aqFileSystem.Exists(fileNamepath);
      
      // Wait until file or folder is deleted
      var crntWaitSecondMs = 0;
      while(fileExists && crntWaitSecondMs <= maxWaitTimeMiliSec)
      {
        Delay(200);
        crntWaitSecondMs = crntWaitSecondMs + 200;
        fileExists = aqFileSystem.Exists(fileNamepath);
      }
      
      return !fileExists;
    }
    
    
    function ReadFileIntoString(fileNameAndPath)
    {
      VerifyFileExistance(fileNameAndPath);
      
      var fileString = aqFile.ReadWholeTextFile(fileNameAndPath, aqFile.ctUTF8);
      
      return fileString;
    }
    
    function GetFileTimeStamp(baseWorkingDirectory, caseName, fileName)
    {
      var fileNamePath = StrHelper.Concat_BaseDirectory_FileFolder_and_FileName(baseWorkingDirectory, caseName, fileName);
      VerifyFileExistance(fileNamePath, 4);
      var fileTimeStamp = aqFile.GetLastWriteTime(fileNamePath);
      
      return fileTimeStamp;
    }
    
    
    function GetFileTimeStampString(baseWorkingDirectory, caseName, fileName)
    {
      var timeStamp = GetFileTimeStamp(baseWorkingDirectory, caseName, fileName);
      var timeStampString = aqConvert.DateTimeToStr(timeStamp);
      
      return timeStampString;
    }
    
    
    
    // ************* the Below code put into a file called 'StringHelper' *************
    
    
    function Concat_BaseDirectory_FileFolder_and_FileName(baseDirectory, fileFolder, fileName)
    {
      // base directory
      baseDirectory = baseDirectory.trim();
      var lastChar = baseDirectory[baseDirectory.length - 1]; 
      if(lastChar !== "\\")
      {
        baseDirectory = baseDirectory + "\\";
      }  
      
      // file folder
      fileFolder = fileFolder.trim();
      var folderFirstChar = fileFolder[0];
      var folderLastChar = fileFolder[fileFolder.length - 1];
      if(folderFirstChar === "\\" || folderFirstChar === "/")
      {
        fileFolder = fileFolder.substring(1, fileFolder.length);
      }
      if(folderLastChar === "\\" || folderLastChar === "/")
      {
        fileFolder = fileFolder.substring(0, fileFolder.length-1);
      }
      
      var fileNameAndPath = baseDirectory + fileFolder + "\\" + fileName.trim();
      
      return fileNameAndPath;
    }
    
    
    function Concat_BaseDirectory_and_FileFolder(baseDirectory, fileFolder)
    {
      if(fileFolder === "")
      {
        Log.Error("The variable, 'v_TestName', cannot be empty. Please fill in and re-run.")
      }
    
      // base directory
      baseDirectory = baseDirectory.trim();
      var lastChar = baseDirectory[baseDirectory.length - 1]; 
      if(lastChar !== "\\")
      {
        baseDirectory = baseDirectory + "\\";
      }  
      
      // file folder
      fileFolder = fileFolder.trim();
      var folderFirstChar = fileFolder[0];
      var folderLastChar = fileFolder[fileFolder.length - 1];
      if(folderFirstChar === "\\" || folderFirstChar === "/")
      {
        fileFolder = fileFolder.substring(1, fileFolder.length);
      }
      if(folderLastChar === "\\" || folderLastChar === "/")
      {
        fileFolder = fileFolder.substring(0, fileFolder.length-1);
      }
      
      var fullPath = baseDirectory + fileFolder;
      
      return fullPath;
    }
    
    
    function ConcatFileNameAndPath(directory, fileName)
    {
      let fileNameAndPath = directory;
      let lastChar = directory[directory.length - 1];  
      if(lastChar !== "\\")
      {
        fileNameAndPath = directory + "\\" + fileName;
      }
      else
      {
        fileNameAndPath = directory + fileName;
      }  
      
      return fileNameAndPath;
    }
    

     

  • Hello,

    First you need to have TestComplete desktop license than you use recorder as you do with browser testing.

     

  • jr348's avatar
    jr348
    Contributor

    Another way of opening files from the browser is using the file://   url eg file://C:/Windows/ .  You can click-navigate the directory and file structure,  however to run tests on the opened file you may need the desktop module.  Most browsers can open text, CSV, PDFs, Excel so it  may not be needed depending on the file.