如何在机器人框架中处理动态表

时间:2017-02-15 06:48:29

标签: robotframework

我有一个使用keepValue ma f = do a <- ma f a return a 标签创建的表和一组行,每行都有列col1,col2,col3,col4,col5和col6。

col1到col5有一些值,col6是编辑按钮。

根据作为参数发送的Col1到col4值,我必须单击“编辑”按钮然后执行某些操作。

我知道如何通过Selenium WebDriver来处理这个问题。

示例代码如下:

<div>

我想知道,我们可以使用机器人框架编写相同的代码。

1 个答案:

答案 0 :(得分:1)

我通过创建一个python关键字然后在机器人框架代码中使用它来解决了这个问题。

下面的

是机器人框架代码:

    @{elemnts}=    Get Webelements    ${table_rows_xpath}

    #iterate for each row
    : FOR    ${row}    IN    @{elemnts}
    \    @{columns}=    CustomLibrary.Get Webelements By Element    ${row}    ${from_parent_row_to_columns_xpath}
    \    ${status}=    Traverse through columns        @{columns}
    \    Run Keyword If    ${status}=="SUCCESS"    Click on Edit icon and Exit Loop    @{columns}


Traverse through columns
    [Arguments]    @{columns}
    ${length}    Get Length    ${verify_values}
    Set Test Variable    ${count}    0
    ${index}    Set Variable     0
    : FOR    ${col}    IN    @{columns}
    \    ${value}=    Get Text    ${col}
    \    Run Keyword If    '${value}'== '${verify_values[${index}]}'    Increment Count    ELSE    Exit For Loop
    \    ${index}=    Evaluate    ${index}+1
    \    Run Keyword If    ${length} == ${count}   Set Status SUCCESS and Exit 


Click on Edit icon and Exit Loop
    [Arguments]    @{columns}
    ${length}    Get Length    ${columns}
    ${length}=    Evaluate    ${length}-1
    Click Element    @{columns}[${length}]
    Exit For Loop    

python代码

def get_webelements_by_element(webElement, locator):
values = locator.split("=",1)

    locateBy = values[0]
    locator = values[1]

    if(locateBy =="name"):
        ele = webElement.find_elements_by_name(locator)
    elif(locateBy =="link"):
        ele = webElement.find_elements_by_link_text(locator)
    elif(locateBy =="partial link"):
        ele = webElement.find_elements_by_partial_link_text(locator)
    elif(locateBy =="tag"):
        ele = webElement.find_elements_by_tag_name(locator)
    elif(locateBy =="css"):
        ele = webElement.find_elements_by_css_selector(locator)
    elif(locateBy =="xpath"):
        ele = webElement.find_elements_by_xpath(locator)    

    return ele