Search code examples
robotframework

How can I pass a specific list variable to a Get and Set function?


How can I pass on a specific global list variable to the following Get and Set functions?

Assume there is a @{Files} list with multiple xxxx.txt files. Assume @{Files}[0] has dinosaur.txt

Get the File Version 
[Arguments] @{Files}[??]    # Not sure about this
Run Keyword If  File is present 
... ${time} = OperatingSystem.Get Modified Time @{Files}[]
... ${dateonly} = Split String ${time}  ${SPACE}
... ${Version} = @{dateonly}[0]
ELSE
... ${Version} = MISSING
[Return]  ${Version}

Set a dinosaur file version test case
  ${Version} = Get the File Version @{Files}[0] # Is this right ???
  Set test variable ${Version}

Solution

  • List variables have two ways to use. When referenced as ${mylist} we are referencing the underneath Python object (a variable of type List). When we use @{mylist} we are expanding all the elements from that list.

    The following code is a fully working example reproducing your original code (you only need to create a file named dinosaur.txt):

    *** Settings ***
    Library         OperatingSystem
    Library         Collections
    Library         String
    
    *** Test Cases ***
    Test Files Versions
      @{Files}=  Create List  dinosaur.txt  bird.txt  dog.txt  cat.txt
      ${Version}=  Get the File Version  ${Files[0]}
      Log  Version of file, ${Files[0]} is ${Version}
      FOR  ${file}  IN  @{Files}
        ${file_version}=  Get the File Version  ${file}
        Log  Version of file, ${file} is ${file_version}
      END
      Set Test Variable  ${Files}
      Set a dinosaur file version test case
      Log  Version of file, ${Files[0]} is ${Version}
    
    *** Keywords ***
    Get the File Version
      [Arguments]  ${File}
      ${file_is_present}=  File is present  ${File}
      IF  ${file_is_present}
          ${time}=  OperatingSystem.Get Modified Time  ${File}
          @{dateonly}=  Split String  ${time}  ${SPACE}
          ${Version}=  Set Variable  ${dateonly[0]}
      ELSE
          ${Version}=  Set Variable If  not ${file_is_present}  MISSING
      END
      [Return]  ${Version}
    
    Set a dinosaur file version test case
      ${Version}=  Get the File Version  ${Files[0]}
      Set test variable  ${Version}
    
    File is present
      [Arguments]  ${File}
      ${result}=  Run Keyword And Return Status  File Should Exist  ${File}
      Return From Keyword  ${result}