Search code examples
pythonrobotframework

Robot framework: create a keyword with optional argument. The argument's default value should come from a script


I am trying to make a keyword that takes as default argument a python script method, as seen below:

*** Settings ***
Library  SeleniumLibrary
Library  BuiltIn
Library  ./../python_scripts/folders.py

*** Keywords ***
Create subfolder inside mainfolder through right click
    [Arguments]  ${PARENT_FOLDER}=folders.get_last_folder
    press element  ${PARENT_FOLDER}
    right click and add folder
    basic folder creation process
    save folder
    assert created folder exists

The problem is that when I run this step, it outputs Element 'folders.get_last_folder' not visible after 5 seconds. What is the correct syntax in order to have ${PARENT_FOLDER}'s default value as the output of the method folders.get_last_folder()?


Solution

  • You can't run functions in Arguments section, folders.get_last_folder is considered to be a string. You need to use "Run Keyword If" to conditionally set variable, something like this:

    *** Keywords ***
    Create subfolder inside mainfolder through right click
        [Arguments]  ${PARENT_FOLDER}=folders.get_last_folder
        ${PARENT_FOLDER}=    Run Keyword If    "${PARENT_FOLDER}" == "folders.get_last_folder"    folders.get_last_folder
        ...                  ELSE              Set Variable    ${PARENT_FOLDER}
        press element  ${PARENT_FOLDER}
        right click and add folder
        basic folder creation process
        save folder
        assert created folder exists