Checking files with Robot Framework

With Robot Framework you are able to access file system. This could be used as reading log files, creating new files with random data and for example deleting files. This short sample shows one way to use Robot, much more can be found in OperatingSystem documentation

Test Suite

With this test suite, we are going to check for file, then adding new file and for last one deleting the created file. Suite uses one resource file and in that file is located much more detailed logic. One could also add documentation and logging to make this example much more professional and readable for other developers.

*** Settings ***
Resource    Resources/resources.txt

*** Test Cases ***

Check for CV
    Check for file   C:\\some\\path\\to\\Documents\\CV\\cv_2016.docx
    
Check how to write a file
    Check how to write a file     C:\\some\\path\\to\\Documents\\RobotFramework_test    test_1.txt    This is a sample text    UTF-8

Check removing file
    Check how to remove a file     C:\\some\\path\\to\\Documents\\RobotFramework_test    test_1.txt

Show in three test cases we are going to use customaided keywords. This is not necessary if you would like to add all keywords in each test case. However here is also showed explanation how to use arguments in custom keywords.

Then the magical resources in Resources folder

*** Settings ***
Library    OperatingSystem

*** Keywords ***
Check for file    
    [Arguments]    ${path}
    File Should Exist    ${path}
    
Check how to write a file
    [Arguments]    ${path}   ${fileName}    ${text}    ${encoding}
    File Should Not Exist    ${path}${/}${fileName}  
    Create File    ${path}${/}${fileName}    content=${text}     encoding=${encoding}
    File Should Exist    ${path}${/}${fileName}
    ${FileContent}=  Get file    ${path}${/}${fileName}   encoding=${encoding}
    Should Be Equal  ${text}   ${FileContent}
    
Check how to remove a file
    [Arguments]    ${path}   ${fileName}
    File Should Exist    ${path}${/}${fileName}
    Remove File    ${path}${/}${fileName}
    File Should Not Exist    ${path}${/}${fileName}

In keywords are more tailored logic, what we are going to do in each test case.

  1. Check for file
    1. Check if the file exist in argumetn path
  2. Check how to write a file
    1. There should not be a file in that path
    2. Create a file with certain path, content and encoding
    3. Check that the file is created
    4. Capture created file content in a variable
    5. Check that content added to file is the same
  3. Check how to remove a file
    1. File shoul exist in certain path
    2. Remove file from the path
    3. File should no longer exist in given path

So as we can see, Robot is quite good worker also in this kind of job processing. This kind of functionality could also be used with selenium test where first user makes something at the GUI and then check for the results at log files.