【问题标题】:How to Clear or Remove the values from List in Robot Framework如何在 Robot Framework 中清除或删除列表中的值
【发布时间】:2022-01-03 16:57:54
【问题描述】:

我写了一个测试用例,它会从 excel 表中读取登录详细信息,并为每对详细信息登录和注销。

我正在从 Excel 中读取数据,每行详细信息都传递给列表并将其作为输入发送到另一个关键字/函数以进行登录。

注销后,我只想删除列表中的值,它显示为成功,但在下次运行时它也显示旧值。

我搜索了,我没有得到任何关键字来清除所有列表值。

有人可以帮我解决这个问题吗?代码如下:

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

   *** Variables ***
    ${delay}          5s
    ${excelName}      LoginTestData.xls
    @{rowValues}
    ${rowCount}       ${EMPTY}
    ${cellCount}      ${EMPTY}

    *** Test Cases ***
    Get Data from Excel
        Open Excel Sheet    ${excelName}
        @{sheetNames}    Get Sheet Names
        ${sheetName}    Set Variable    @{sheetNames}[0]
        ${rowCount}    Get Row Count    ${sheetName}
        ${cellCount}    Get Column Count    ${sheetName}
        : FOR    ${rindex}    IN RANGE    1    ${rowCount}
        \    @{rowValues}    Get Values    ${sheetName}    ${rindex}    ${cellCount}
        \    Log to console    row values are for index ${rindex} : @{rowValues}
        \    Login to the CLM    @{rowValues}
        \    Log    cell count is : ${cellCount}
        \    Change Language to English
        \    Sleep    ${delay}
        \    Logout from CLM
        \    Remove Values    ${rowValues}    ${cellCount}

    *** 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
        : 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    ${rowValues}    ${cindex}    ${cellValue}
        [Return]    @{rowValues}

    Login to the CLM
        [Arguments]    @{rowValues}
        Open Browser    http://172.20.24.74/clm-ui/#/login/    chrome
        Maximize Browser Window
        Sleep    ${delay}
        Input Text    id=username    @{rowValues}[0]
        Input Password    id=password    @{rowValues}[1]
        Click Button    css=.btn.btn-primary

    Remove Values
        [Arguments]    ${rowValues}    ${cellCount}
        Log to console    rowvalues are : ${rowValues} and cell Count: ${cellCount}
        : FOR    ${index}    IN RANGE    0    ${cellCount}
        \    ${value}=    Remove from List    ${rowValues}    -1
        \    Log to console    value removed from list is : ${value}

    Change Language to English
        Sleep    ${delay}
        Wait Until Element Is Visible    xpath=//*[@id='top-navbar']/ul[2]/li/a/span[2]    30s
        Click Element    xpath=//*[@id='top-navbar']/ul[2]/li/a/span[2]
        Click Element    xpath=//*[@id='top-navbar']//a[contains(text(),'English')]

    Logout from CLM
        Sleep    ${delay}
        Click Element    xpath=//*[@id='top-navbar']/ul[2]/li/a/span[2]
        Click Link    link=Logout

在输出中它显示为删除值,但下一次运行,它也显示旧值

输出如下:

===========================================================================================================
ReadExcel :: CLM Registration Test Case                                                                    
===========================================================================================================
Get Data from Excel                                                                                user is in Get Values function
get the data from LoginDetails[0][1]
get the data from LoginDetails[1][1]
row values are for index 1 : [u'akurasa', u'Srija210$']
rowvalues are : [u'akurasa', u'Srija210$'] and cell Count: 2
value removed from list is : Srija210$
value removed from list is : akurasa
user is in Get Values function
get the data from LoginDetails[0][2]
get the data from LoginDetails[1][2]
row values are for index 2 : [u'clmui', u'tecnotree', u'akurasa', u'Srija210$']
rowvalues are : [u'clmui', u'tecnotree', u'akurasa', u'Srija210$'] and cell Count: 2
value removed from list is : Srija210$
value removed from list is : akurasa
| PASS |
-----------------------------------------------------------------------------------------------------------

【问题讨论】:

    标签: robotframework


    【解决方案1】:

    尝试使用关键字“创建列表”作为 FOR 循环的最后一步,而不是“删除值”用户定义的关键字。此外,如果每行的列数不同,请将变量 ${cellCount} 移动到循环内

        Get Data from Excel
        Open Excel Sheet    ${excelName}
        @{sheetNames}    Get Sheet Names
        ${sheetName}    Set Variable    @{sheetNames}[0]
        ${rowCount}    Get Row Count    ${sheetName}
    
        : FOR    ${rindex}    IN RANGE    1    ${rowCount}
        \    ${cellCount}    Get Column Count    ${sheetName}
        \    @{rowValues}    Get Values    ${sheetName}    ${rindex}    ${cellCount}
        \    Log to console    row values are for index ${rindex} : @{rowValues}
        \    Login to the CLM    @{rowValues}
        \    Log    cell count is : ${cellCount}
        \    Change Language to English
        \    Sleep    ${delay}
        \    Logout from CLM
        \    @{rowValues}    Create List
    

    “@{rowValues} 创建列表”:此命令将清除列表中的值

    【讨论】:

    • 嗨 Rakesh,对不起,我没听明白,我必须在“创建列表”用户定义的关键字中实现什么功能。
    • “创建列表”是内置库中可用的库关键字。您无需为它编写任何脚本。只需将上述脚本复制并粘贴到测试用例部分即可。尝试并更新。
    【解决方案2】:

    在我的情况下,我想要清除的列表是全局的,所以我无法使用 Create List 清除它。在这种情况下,一个

    :FOR     ${elem}    IN    @{list}
        Remove values from list    ${list}    ${elem}
    

    帮助。

    【讨论】:

      猜你喜欢
      • 2019-01-07
      • 1970-01-01
      • 1970-01-01
      • 2021-03-31
      • 1970-01-01
      • 2015-09-04
      • 1970-01-01
      • 1970-01-01
      • 2019-12-04
      相关资源
      最近更新 更多