Sayali Waghmare
Sayali Waghmare

Reputation: 112

How can I access value of nested lists in robot framework

I want to access values of a nested list [ICMPDU,[0,1,2]] in robot framework. I want to access the element 2 in the list in robot framework. Below is my code.Below code gives me output as 'Yes'[2]. Any suggestions?

*** Settings ***
Library           SeleniumLibrary
Library           Collections

*** Variables ***
@{ICMPDU_Val}     'Yes'    1    2
@{ICMPDU}         ICMPDU    @{ICMPDU_Val}

*** Test Cases ***
Network_web_page
  Log To Console    @{ICMPDU}[1][2]

Upvotes: 2

Views: 14049

Answers (2)

Bryan Oakley
Bryan Oakley

Reputation: 386220

The first problem is that you are not creating a list inside of a list.

Consider this line:

@{ICMPDU}         ICMPDU    @{ICMPDU_Val}

This will create a list composed of four values: ICMPDU, Yes, 1, and 2. That is because when @ is used on the right hand side of a keyword, the list is expanded and each element becomes an argument to the keyword.

From the robot framework users guide (emphasis added):

When a variable is used as a scalar like ${EXAMPLE}, its value will be used as-is. If a variable value is a list or list-like, it is also possible to use as a list variable like @{EXAMPLE}. In this case individual list items are passed in as arguments separately.

If you want the list to be the second element of the list, you must use $ instead of @ when defining the variable:

@{ICMPDU}         ICMPDU    ${ICMPDU_Val}

The second problem is that the syntax for accessing array elements only works outside the braces if you have a single index. If you need somethime more complex, such as [1][2] you need to use extended variable syntax and move the indexes inside of the curly braces.

For example, ${ICMPDU[1][2]}

Upvotes: 4

GPT14
GPT14

Reputation: 819

Using the Collections Library:

*** Test Cases ***
Network_web_page
    ${li}=    Get From List    ${ICMPDU}    1
    ${res}=    Get From List    ${li}    2

Gives the output:

${li} = [u"'Yes'", u'1', u'2']

${res} = 2

and the variables as :

*** Variables ***
@{ICMPDU_Val}     'Yes'    1    2
@{ICMPDU}         ICMPDU    ${ICMPDU_Val}

Upvotes: 1

Related Questions