机器人框架的数据驱动场景

时间:2016-12-22 17:19:24

标签: nested-loops robotframework

我希望在变量文件中设置用户名和密码,并且作为每个用户名的测试步骤,它会前进到下一行数据。

我正在使用资源文件来驱动数据,而我的初始FOR脚本处理用户名1(eeny),然后嵌套的FOR循环一次传递所有密码。

*** Variables  ***
@{users}  eeny  meeny  miny  moe
@{pwds}  pwd  pwd1  pwd2  pwd3

*** Test Cases ***

Login Test
    :FOR    ${u}    IN    @{users}
    \   Open Browser  ${URL}
    \   set window size  1440  900
    \   set selenium speed  .5
    \   input text  id=username  ${u}
    \   :FOR  ${p}  IN  @{pwds}
    \   \  input text  id=password  ${p}
    \   \  click button  css=button.primary
    \   \  wait until page contains  css=p.logout
    \   \  capture page screenshot
    \   \  click element  css=p.logout

目前我正在尝试嵌套的FOR语句,或同时声明用户名和密码变量的FOR语句,但我不反对改变获取数据和使用多维数组或类似的策略,如果是工作得更好。

2 个答案:

答案 0 :(得分:3)

我认为您在单个测试用例和多个测试用例之间存在误解。你上面的内容将输入一个用户名,然后它会循环显示一个密码,提交和注销。只有当你碰巧在下一页上有相同的元素时,它才会尝试输入另一个密码。

我建议您在这里查看:http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#data-driven-style

另外看看一些类似的问题,你需要提供一个测试模板,担心稍后当你有两个测试用例工作时循环会是我的建议。

答案 1 :(得分:1)

数据驱动测试可以通过“通过测试模板进行数据驱动”或从Excel或csv中读取数据来实现。

'通过测试模板驱动的数据'   在这里,我们将创建一个用户定义的关键字,它只是我们想要多次执行的函数或源代码。 我们在'Test Template'部分中声明为Settings
当我们为TestCase名称提供参数时,它每次都会执行Test Template。     
这里是示例代码

*** Settings ***
Documentation     This Test Suite Deals with Data Driven Testing using Test Template Method
Test Teardown     Close All Browsers
Test Template     Validate Login with Differnt Valid Credentials

*** Variables ***
${URL}            http://ururl
${Browser}        chrome
${Title}          HomePage
${delay}          5s

*** Test Cases ***    USERNAME    PASSWORD
Test User             user1       user1

Admin User            admin       admin

*** Keywords ***
Validate Login with Differnt Valid Credentials
    [Arguments]    ${Uname}    ${Pwd}
    Open the Browser and enter the URL    ${URL}    ${Browser}
    Enter the User Name    ${Uname}
    Enter the Password    ${Pwd}
    Click on Submit button
    Verify HomePage displayed    ${Title}

Open the Browser and enter the URL
    [Arguments]    ${URL}    ${Browser}
    Open Browser    ${URL}    ${Browser}

Enter the User Name
    [Arguments]    ${UserName}
    Sleep    ${delay}
    Input Text    id=username    ${UserName}

Enter the Password
    [Arguments]    ${Password}
    Input Text    id=password    ${Password}

Click on Submit button
    Click Button    css=.btn.btn-primary

Verify HomePage displayed
    [Arguments]    ${Title}
    Sleep    ${delay}
    Title Should Be    ${Title}

'通过Excel读取数据驱动'
从Excel表读取值并多次运行,     
这里是示例代码

*** Settings ***
Documentation     CLM Registration Test Case
Test Teardown     Close All Browsers
Library           Selenium2Library
Library           Collections
Library           ExcelLibrary
Library           String

*** Variables ***
${delay}          2s
${excelName}      LoginTestData.xls
${rowCount}       ${EMPTY}
${cellCount}      ${EMPTY}
${URL}            http://Ur test Test URL
${Browser}        chrome

*** Test Cases ***
ReadFromExcelSheet
    Open Excel Sheet    ${excelName}
    @{sheetNames}    Get Sheet Names
    ${sheetName}    Set Variable    @{sheetNames}[0]
    ${rowCount}    Get Row Count    ${sheetName}
    ${cellCount}    Get Column Count    ${sheetName}
    #for loop to read all the rows in excel
    : FOR    ${rindex}    IN RANGE    1    ${rowCount}
    \    @{rowValues}    Create List
    \    @{rowValues}    Get Values    ${sheetName}    ${rindex}    ${cellCount}
    \    Log to console    row values are for index ${rindex} : @{rowValues}
    \    Sleep    ${delay}
    \    Open Browser    ${url} ${Browser}
    \    Enter User Name    @{rowValues}[0]
    \    Enter Password     @{rowValues}[1]
    \    Click on Submit Button

*** Keywords ***
Open Excel Sheet
    [Arguments]    ${excelName}
    Open Excel    ${excelName}    useTempDir=False

Get Values
    [Arguments]    ${sName}    ${row}    ${cCount}
    Log to console    user is in Get Values function
    @{rValues}    Create List
    : FOR    ${cindex}    IN RANGE    0    ${cCount}
    \    Log to console    get the data from ${sName}[${cindex}][${row}]
    \    ${cellValue}    Read Cell Data By Coordinates    ${sName}    ${cindex}    ${row}
    \    Insert Into List    ${rValues}    ${cindex}    ${cellValue}
    [Return]    @{rValues}


Open The Browser
       [Arguments]    ${url}    ${Browser}
       Open Browser   ${url}    ${Browser}

Enter User Name    
       [Arguments]    username
       Input Text    id=username    username


Enter Password     
       [Arguments]   password
       Input Password    id=password    password

Click on Submit Button
       Click Button    css=.btn.btn-primary