seleniumautomated-testsrobotframeworkweb-testing

How would you write this in a loop in Robot Framework


So I'm currently learning Robot Framework and instead of using repeating code, I'm trying to utilize loops to make my code easier to maintain. Unfortunately, I do not know how to solve this one where I want to put this into a loop. This is a series of questions with drop-down answers, and I want to choose a different answer every time my test runs. I have tried a couple of foor loop options but none of them work. Essentially each loop should click on the drop-down and choose one random answer until all the questions are answered.

Check if User can change "Company" information
    Scroll Element Into View                ${USER_MENU_SCROLL_TO_COMPANY_MENU}
    sleep                                   1s
    click element                           ${USER_MENU_CLICK_COMPANY_EDIT_BUTTON}
    ${random_description1} =                 Evaluate  random.choice(@{USER_MENU_DROP_DOWN_QUESTION1})
    click element                           ${random_description1}
    sleep                                   1s
    click element                           ${USER_MENU_CLICK_DROP_DOWN_2}
    ${random_description2} =                 Evaluate  random.choice(@{USER_MENU_DROP_DOWN_QUESTION2})
    click element                           ${random_description2}
    sleep                                   1s
    click element                           ${USER_MENU_CLICK_DROP_DOWN_3}
    ${random_description3} =                 Evaluate  random.choice(@{USER_MENU_DROP_DOWN_QUESTION3})
    click element                           ${random_description3}
    sleep                                   1s
    click element                           ${USER_MENU_CLICK_DROP_DOWN_4}
    ${random_description4} =                 Evaluate  random.choice(@{USER_MENU_DROP_DOWN_QUESTION4})
    click element                           ${random_description4}
    sleep                                   1s
    click element                           ${USER_MENU_CLICK_DROP_DOWN_5}
    ${random_description5} =                 Evaluate  random.choice(@{USER_MENU_DROP_DOWN_QUESTION5})
    click element                           ${random_description5}

I tried something like this but it wants to concatenate the values in my variables.

FOR ${i} IN RANGE 1 5
    ${random_description} = Evaluate random.choice(@{USER_MENU_DROP_DOWN_QUESTION}+${i})
    click element ${USER_MENU_CLICK_DROP_DOWN_}+${i}
    click element ${random_description}
    sleep 1s
END

Solution

  • Your problem is building the name of the variables. You could have your variables as a list of lists.

    Here is my view of the solution for your current code:

    FOR ${i} IN RANGE 1 5
        ${random_description} = Evaluate random.choice(@{USER_MENU_DROP_DOWN_QUESTION${i}})
        click element ${USER_MENU_CLICK_DROP_DOWN_${i}}
        click element ${random_description}
        sleep 1s
    END
    

    EDIT -- That was the initial answer. Below is a proven solution.

    *** Test Cases ***
    Loop in menus
        @{USER_MENU_DROP_DOWN_QUESTION1}=    Create List    question11    question12    question13    question14    question15
        @{USER_MENU_DROP_DOWN_QUESTION2}=    Create List    question21    question22    question23    question24    question25
        @{USER_MENU_DROP_DOWN_QUESTION3}=    Create List    question31    question32    question33    question34    question35
        @{USER_MENU_DROP_DOWN_QUESTION4}=    Create List    question41    question42    question43    question44    question45
        @{USER_MENU_DROP_DOWN_QUESTION5}=    Create List    question51    question52    question53    question54    question55
        @{USER_MENU_CLICK_DROP_DOWN_1}=    Create List    item11    item12    item13    item14    item15
        @{USER_MENU_CLICK_DROP_DOWN_2}=    Create List    item21    item22    item23    item24    item25
        @{USER_MENU_CLICK_DROP_DOWN_3}=    Create List    item31    item32    item33    item34    item35
        @{USER_MENU_CLICK_DROP_DOWN_4}=    Create List    item41    item42    item43    item44    item45
        @{USER_MENU_CLICK_DROP_DOWN_5}=    Create List    item51    item52    item53    item54    item55
        FOR    ${i}    IN RANGE    0    5
            ${random_description}=    Evaluate    random.choice(${USER_MENU_DROP_DOWN_QUESTION${i+1}})
            Log To Console    \nclick element ${USER_MENU_CLICK_DROP_DOWN_${i+1}}[${i}]
            Log To Console    click element ${random_description}\n
            sleep    1s
        END
    

    Output: Console output