FileLibrary | Ranorex Automation Helpers
Help CenterUser GuideRanorex Studio expert

FileLibrary

During a test run, it’s often useful or necessary to run checks on files or write data to them. The FileLibrary collection contains all the user code methods that make this possible.

Here, we explain how each of them works. To use them, simply ⇢ add the individual methods to the desired recording modules and configure them as described on this page.

On this page

    AppendStringToExistingFile

    This method opens a file and appends a string to the end of the file. If the file doesn’t exist, it will be created automatically.

    The following arguments are available:

    String text

    • The string you want to append to the file.
    • Can be a variable.

    String path

    • The path to the file you want to append the string to. Can be absolute or relative and must include file name and extension. The base path is the output folder of the solution.
    • If you only specify the file name and extension, i.e. don’t provide a path, the method will look for the file in the output folder of the solution.
    • If the specified file doesn’t exist, the method automatically creates the file and then appends the string to it.

    Boolean addNewLine

    • true = appends the string in a new line at the end of the file.
    • false = appends the string to the end of the last line in the file.

    The method is also logged to the report:

    CheckFilesExist

    This method checks whether one or more files exist. It behaves like a Validation action, i.e. results in success or failure of the containing module.

    The following arguments are available:

    String path

    • The path to the folder that contains the file(s) whose existence you want to check. Can be absolute or relative. The base path is the output folder of the solution.
    • The relative path “./” will search for the files in the output folder.

    String pattern

    • You can either enter a specific file name and extension to find a specific file or you can specify a search pattern, which will then find all files that match it. E.g.: “DemoApp.*” will check for any files named DemoApp, regardless of file ending.

    Int32 expectedCount

    • How many files that match the string pattern are expected to exist in the target folder. This is the validation part of the method.
    • The method compares this number against the number of files it actually finds using the search pattern.
    • If the numbers match, it returns a success, if they don’t, it returns a failure.

    Results are logged to the report:

    DeleteFiles

    This method deletes one or more files.

    The following arguments are available:

    String path

    • The path to the folder that contains the file(s) you want to delete. Can be absolute or relative. The base path is the output folder of the solution. The relative path “./” will search for the files in the output folder.

    String pattern

    • You can either enter a specific file name and extension to find a specific file to delete or you can specify a search pattern, which will then delete all files that match it. E.g.: “DemoApp.*” will check for any files named DemoApp, regardless of file ending.

    If the method can’t find the file(s) to delete, it will not result in a failure of the module. Only a warning will be logged to the report.

    myLogFile.text deleted successfully in the output folder

    Warning that myLo.txt could not be found in the output folder and therefore wasn’t deleted

    ValidateFileContainsText

    This method checks whether a file contains a specific string. It is available with and without an argument to specify the file encoding. It behaves like a Validation action, i.e. results in success or failure of the containing module.

    The following arguments are available:

    String filePath

    • The path to the file you want to check. Can be absolute or relative and must include file name and extension. The base path is the output folder of the solution.
    • If you only specify the file name and extension, i.e. don’t provide a path, the method will look for the file in the output folder of the solution.
    • If the path is wrong or the file can’t be found, the containing module won’t fail, but an error will be logged to the report.

    String text

    • The string to look for in the specified file.
    • Can be a variable.

    String fileEncoding

    • The encoding of the specified file.
    • If left empty, uses UTF-8 encoding.

    ValidateFilesBinaryEqual

    This method compares whether the content of two binary files is the same. It behaves like a Validation action, i.e. results in success or failure of the containing module.

    String filePath1 and filePath2

    • The paths to the two files you want to compare. Can be absolute or relative and must include file names and extensions. The base path is the output folder of the solution.
    • If you only specify the file names and extensions, i.e. don’t provide paths, the method will look for the files in the output folder of the solution.
    • If the path is wrong or the file(s) can’t be found, the containing module won’t fail, but an error will be logged to the report.

    The results are logged to the report:

    Successful comparison, i.e. both files match

    Failure, i.e. the files don’t match

    ValidateFilesTextEqual

    This method compares whether the content of two text files is the same. It is available with and without an argument to normalize the line endings in the files for better comparison. It behaves like a Validation action, i.e. results in success or failure of the containing module.

    String filePath1 and filePath2

    • The paths to the two files you want to compare. Can be absolute or relative and must include file names and extensions. The base path is the output folder of the solution.
    • If you only specify the file names and extensions, i.e. don’t provide paths, the method will look for the files in the output folder of the solution.
    • If the path is wrong or the file(s) can’t be found, the containing module won’t fail, but an error will be logged to the report.

    Boolean normalizeLineEndings

    • true = Normalizes line endings to make comparison of files with different encodings possible. Does not make changes to the original files.

    The results are also logged to the report:

    Successful comparison, i.e. both files match

    Failure, i.e. the files don’t match

    WaitForFile

    This method pauses the test run for a specific time and, while paused, checks whether one or more files exist, i.e. the method waits for one or more files to exist. Depending on whether the file exists within the waiting period or not, the containing module succeeds or fails.

    String path

    • The path to the folder that contains the file(s) whose existence you want to check. Can be absolute or relative. The base path is the output folder of the solution.
    • The relative path “./” will search for the files in the output folder.

    String pattern

    • You can either enter a specific file name and extension to find a specific file or you can specify a search pattern, which will then find all files that match it. E.g.: “DemoApp.*” will check for any files named DemoApp, regardless of file ending.

    Int32 duration

    • Waiting time in milliseconds.
    • This is how long the test run will pause to wait for the file(s) to exist.

    The results are also logged to the report:

    Success

    Failure, i.e. the files did not exist within the waiting period

    WriteToFile

    This method creates a file and writes a string to it. It’s different from AppendStringToExistingFile, which can only append a string to an already existing file.

    String text

    • The string to write to the new file.
    • Can be a variable.

    String filenamePrefix

    • The path where you want to  write the file. Can be absolute or relative and must include the file name and extension. The base path is the output folder of the solution.
    • Prefix for the file name of the new file.
    • If left empty, the file name will only consist of the obligatory timestamp (<filename>_YYYYMMDD_HHmmSS).

    String fileExtension

    • File extension for the new file.
    • If left empty, the file will not be of any file type.
    Note icon

    Note

    If the method is called more than once per second, the last call will overwrite all preceding ones. The method only creates separate new files if at least one second has passed between calls.