The FileLibrary collection contains user code methods that allow you to run checks on files or write data to them.
FileLibrary methods
To use these methods, add them to your recording module. For more information, see Using the User Code Library.
AppendStringToExistingFile
This method opens a file and appends a string to the end of the file. If the file doesn’t exist, the method creates it automatically.
Arguments:
- String text: The string you want to add to the file. It can be a variable.
-
String path: The file path, including the name and extension.
- It can be absolute or relative. The application uses the solution's output folder as the base path for relative entries.
- If you only specify the file name and extension, the method looks 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: Set to true to start the string on a new line; set to false to append it directly to the end of the existing last line.
The application logs the method to the report:
CheckFilesExist
This method validates whether one or more files exist in a folder. It functions like a standard validation and reports a success or failure to the log.
Arguments:
- String path: The folder path to search. Can be absolute or relative. The base path is the output folder of the solution. Use ./ to target the solution's output folder.
- String pattern: The file name or search pattern to locate files. For example, if you use the search pattern DemoApp.*, the method checks for any files named DemoApp, regardless of the file extension.
- Int32 expectedCount: The exact number of files that must match the pattern for the validation to succeed.
Success in the report:
Failure in the report:
DeleteFiles
This method removes one or more files from a specified directory based on a search pattern.
Arguments:
- String path: The folder path containing the files you want to delete.The base path is the output folder of the solution. The relative path “./” searches for the files in the output folder.
- String pattern: The file name or search pattern to locate files to remove. For example, if you use the search pattern DemoApp.*, the method removes any files named DemoApp, regardless of the file extension.
If the method can’t find any files to delete, it does not fail the module. The report displays a warning.
- 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 if a specific file contains a defined text string.It is available with and without an argument to specify the file encoding. It functions like a standard validation and reports a success or failure to the log.
Arguments:
-
String filePath: The path to the file you want to check.
- Can be absolute or relative and must include the file name and extension. The base path is the output folder of the solution.
- If you only specify the file name and extension and don’t provide a path, the method looks 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 does not fail, but an error is logged to the report.
- String text: The string the method searches for within the file. Can be a variable
- String fileEncoding: The encoding of the specified file. If empty, the method uses UTF-8 encoding.
ValidateFilesBinaryEqual
This method performs a binary comparison between two files to ensure they are identical at the byte level. It functions like a standard validation and reports a success or failure to the log.
Arguments:
-
String filePath1 and String filePath2: The paths to the two files you are comparing.
- 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 and don’t provide paths, the method looks 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 does not fail, but an error is logged to the report.
Results in the report:
- Successful comparison, for example, both files match.
- Failure,for example, the files do not match.
ValidateFilesTextEqual
This method compares the text content of two files. It is available with and without an argument to normalize the line endings in the files for better comparison. It functions like a standard validation and reports a success or failure to the log.
Arguments:
-
String filePath1 and String filePath2: The paths to the two files.
- 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 and don’t provide paths, the method looks 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 does not fail, but an error is logged to the report.
- Boolean normalizeLineEndings: Set to true to normalize line endings to make comparison of files with different encodings possible. Do not make changes to the original files.
Results in the report:
- Successful comparison, for example, both files match.
- Failure, for example, the files do not match.
WaitForFile
This method repeatedly checks for a file's existence until it appears or the timeout expires. Depending on whether the file exists within the waiting period or not, the containing module succeeds or fails.
Arguments:
-
String path: The folder path to monitor.
- Can be absolute or relative. The base path is the output folder of the solution.
- The relative path “./” searches for the files in the output folder.
- String pattern: The file name or pattern to look for. For example, if you use the search pattern DemoApp.*, the method searches for any files named DemoApp, regardless of the file extension.
- Int32 duration: The total search timeout in milliseconds.
WriteToFile
This method creates a file in the output folder and writes a string to it. It’s different from AppendStringToExistingFile, which can only append a string to an already existing file.
Arguments:
- String text: The content you want to write to the new file. Can be a variable.
- String filenamePrefix: Prefix for the file name of the new file. If empty, the file name only consists of the obligatory timestamp (<filename>_YYYYMMDD_HHmmSS).
String fileExtension: File extension for the new file.If empty, the file is not of any file type.