How to use variable throughout the test cases

I want to use variable throughout my test cases. For that, I have some script in Common_File.robot, and I want to use that variable in my Main.robot file without using Set Suit Variable. How can I do this? If I have following code in my Common_File.robot

*** Keywords ***
Keys for dictionary
  ${First_Dictionary}=  Create Dictionary 

  ${boxes_count}=  Get Element Count  ${coulumns}
  FOR  ${index}  IN RANGE  1  ${boxes_count}+1
     ${boxes_txt}=  Get Text  ${column}\${index}
      Set To Dictionary  ${First_Dictionary}   ${boxes_txt}  ${EMPTY}
  END 
  ${Second_Dictionary}=  Create Dictionary  ${box1}=${EMPTY}  
  ${box2}=${EMPTY}  ${box3}=${EMPTY}  ${box4}=${EMPTY}

  [RETURN]  ${First_Dictionary}  ${Second_Dictionary}

Now In Main.robot file I want to use this variables

*** Test Cases ***
Test Case 001
    Parse Xpath To Dictionary    ${xpath1}

Test Case 002
        Parse Xpath To Dictionary     ${xpath2}
*** Keywords ***
Parse Xpath To Dictionary
     [Arguments]    ${xpath}
    ${final_dictionary}=    Create Dictionary
    ${Count}=    Get Element Count    ${boxes}
    FOR    ${index}    IN RANGE    1    ${Count}+1
        ${xpath_available}=  Run Keyword And Ignore Status  Element Should Be Visible  ${xpath_with_index}
        IF  ${xpath_available}
            ${webelements}=  Get Webelements  ${xpath}
            FOR  ${element}  IN  @{webelement}
                ${element_text}=  Get Text  ${element}
            END
        END
        ${key}=  Evaluate  list(${First_Dictionary}[${index}]
        Set To Dictionary  ${final_dictionary}  ${key}=${element_text}
    END

    ${final_dictionary2}=    Create Dictionary
    ${Count2}=    Get Element Count    ${boxes2}
    FOR    ${index}    IN RANGE    1    ${Count2}+1
        ${xpath_available}=  Run Keyword And Ignore Status  Element Should Be Visible  ${xpath2}
        IF  ${xpath_available}
            ${webelements}=  Get Webelements  ${xpath2}
            FOR  ${element}  IN  @{webelement}
                ${element_text}=  Get Text  ${element}
            END
        END
        ${key}=  Evaluate  list(${Second_Dictionary}[${index}]
        Set To Dictionary  ${final_dictionary2}  ${key}=${element_text}
    END
    

For above code I’m getting error as variable ‘${First_Dictionary}’ not found and works fine with Set Suit Variable
can anyone help to solve this issue or any suggestions

Hi Leo,

having a quick look I can’t see where in Parse Xpath To Dictionary do you call Keys for dictionary?

there should be a line like:

${First_Dictionary}    ${Second_Dictionary}=    Keys for dictionary

or

@{Both_Dictionaries}=    Keys for dictionary

since Keys for dictionary is returning a list of dictionaries.

Also when you have First_Dictionary as a dictionary this line will probably still fail:

${key}=  Evaluate  list(${First_Dictionary}[${index}]

It probably should be something like:

${key}=  Evaluate  list(${First_Dictionary.keys()})[${index}]

you had 2 issues

  1. you didn’t close the round bracket ) for the cast to list
  2. you can’t use a numeric value to select a key from a dictionary, and you can’t cast a dictionary directly to a list, but python provides a keys function for a dictionary that can be cast to a list.

Dave.

Hi @damies13 thanks for the response I have tried above code by using Set Suit Variable code works fine but I dont want to use Set Suite Variable also

  1. I want to use keys of ${First_Dictionary} and ${Second_Dictionary} and make seperate Dictionary for both of them I dont know how can I use key created in common file(i.e${First_Dictionary}) ${Second_Dictionary} to Main.robot file (i.e in ${final_dictionary} ${final_dictionary2} )

Can you please show with the code for better understanding

Hi Leo,

If we look at the first part of Parse Xpath To Dictionary from your post:

*** Keywords ***
Parse Xpath To Dictionary
     [Arguments]    ${xpath}
    ${final_dictionary}=    Create Dictionary
    ${Count}=    Get Element Count    ${boxes}
    FOR    ${index}    IN RANGE    1    ${Count}+1
        ${xpath_available}=  Run Keyword And Ignore Status  Element Should Be Visible  ${xpath_with_index}
        IF  ${xpath_available}
            ${webelements}=  Get Webelements  ${xpath}
            FOR  ${element}  IN  @{webelement}
                ${element_text}=  Get Text  ${element}
            END
        END

Somewhere before this next line you should have called Keys for dictionary so that you can receive the dictionary First_Dictionary from it, but I’m not sure where (not sure exactly what you are trying to acheive), because Keys for dictionary was never called First_Dictionary doesn’t exist, don’t forget as First_Dictionary is a local variable in Keys for dictionary you need to set it to the return value from Keys for dictionary in the context of this keyword.

        ${key}=  Evaluate  list(${First_Dictionary}[${index}]
        Set To Dictionary  ${final_dictionary}  ${key}=${element_text}
    END

Dave.

@damies13 can you show with the code if possible it will be helpful to understand

Hi Leo,

Refer to my last 2 posts, in the first one I gave you the line you need to add, in the second I gave you the last line it need to be add before, as to where exactly it should be added, I’m not sure only you can answer that.

Dave.