selenium 在Robot Framework中,如何在循环中编写此代码

kqhtkvqz  于 2023-01-30  发布在  其他
关注(0)|答案(2)|浏览(161)

所以我现在正在学习Robot框架,我尝试使用循环来代替重复的代码,使代码更容易维护。不幸的是,我不知道如何解决这个问题,我想把它放到一个循环中。这是一系列的问题,有下拉式的答案。我想在每次测试运行时选择一个不同的答案。我试过几个foor循环选项,但都不起作用。基本上,每个循环都应该单击drop-向下并随机选择一个答案,直到所有问题都得到回答。

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}

我尝试了类似的方法,但是它想要连接变量中的值。

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
kmbjn2e3

kmbjn2e31#

你的问题是建立变量的名字,你可以把变量作为一个列表的列表。
下面是我对您当前代码的解决方案的看法:

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

编辑--这是最初的答案。下面是一个经过验证的解决方案。

*** 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

输出:

e3bfsja2

e3bfsja22#

经过一番修修补补,我找到了一个解决办法。也许有一个更有效的方式来写这篇文章,我想知道这是什么,如果有人有任何建议。我的解决办法:

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_answer1} =                      Evaluate  random.choice(@{USER_MENU_DROP_DOWN_QUESTION_0})
${random_answer2} =                      Evaluate  random.choice(@{USER_MENU_DROP_DOWN_QUESTION_1})
${random_answer3} =                      Evaluate  random.choice(@{USER_MENU_DROP_DOWN_QUESTION_2})
${random_answer4} =                      Evaluate  random.choice(@{USER_MENU_DROP_DOWN_QUESTION_3})
${random_answer5} =                      Evaluate  random.choice(@{USER_MENU_DROP_DOWN_QUESTION_4})
@{random_answers} =     Create List     ${random_answer1}     ${random_answer2}      ${random_answer3}       ${random_answer3}       ${random_answer4}
FOR    ${question}    IN    @{random_answers}
    click element    ${USER_MENU_CLICK_NEXT_DROP_DOWN}
    Click Element    ${question}
    Sleep    1s
END

相关问题