0

I want to do string concatenation with every listed value in robot framework

I use ride, like:

${list} | Create List | a | b | c |

I want get the new list like:

${list1} | a•optional | b•optional | c•optional

how do I do this?

SiHa
  • 7,830
  • 13
  • 34
  • 43
bean
  • 21
  • 1
  • 6

2 Answers2

1

You can use a custom keyword to achieve that you can either pass in a list object to the keyword as an argument or you can create it in python what ever suits you

ListHelper.py

def Catanate_Items_In_List(myList = []):
  string="•optional"
  output = ["{}{}".format(i,string) for i in myList]
  return output

And then In robot you can do:

*** Settings ***
Library  ListHelper.py


*** Test Cases ****
Catanate to Every string In a List
   ${list}  Create List  a  b  c 
   ${list1} =   Catanate Items In List   ${list}
   log  ${list1}

Results :

${list1} = ['a•optional', 'b•optional', 'c•optional']
AutoTester213
  • 2,714
  • 2
  • 24
  • 48
1

You can use the Evaluate keyword with a one-liner Python expression, Appending the same string to a list of strings in Python.

*** Test Cases ***
Catenate
    ${list}=    Create List   a   b   c
    Log Many    @{list}
    ${new list}=    Catenate List Items    ${list}    •optional
    Log Many    @{new list}

*** Keywords ***
Catenate List Items
    [Arguments]    ${list}    ${catenate with}
    ${new list}=    Evaluate    [s + '${catenate with}' for s in ${list}]
    [return]    ${new list}

Or you can create another custom keyword that utilize Robot Framework instead of Python. For example:

  1. Pass the original list and the string that should be appended to a custom keyword.
  2. Make a copy.
  3. Iterate through the two lists and update the values in the new one.

*** Settings ***
Library    Collections

*** Test Cases ***
Catenate
    ${list}=    Create List   a   b   c
    Log Many    @{list}
    ${new list}=    Catenate List Items    ${list}    •optional
    Log Many    @{new list}

*** Keywords ***
Catenate List Items
    [Arguments]    ${list}    ${catenate with}
    ${new list}=    Copy List    ${list}
    ${list length}=    Get Length    ${new list}
    FOR    ${i}    IN RANGE    ${list length}
        # last parameter will be the new, catenated value
        Set List Value    ${new list}    ${i}    ${list[${i}]}${catenate with}
    END
    [return]    ${new list}

You can use the Extended Variable Syntax to access the list item value at a specific index. ${list[${i}]} this effectively means that you get the item at position ${i} from ${list}. Concatenation can be done simply by passing the list item and the suffix string together, without any spaces as a new list value, ${list[${i}]}${catenate with}. The first approach is definitely more convenient.

Bence Kaulics
  • 7,066
  • 7
  • 33
  • 63