如何将多个变量重构为一个带参数的变量?

How to reconstruct multiple variables into one with parameter?

该网页有一个 table,其中有 11 个复选框。

我希望能够灵活地让测试人员只需在 select 特定复选框中输入 1 或 2 或 3。

如何仅使用 ONE variable 构建关键字?所以测试人员不需要输入很多行元素变量。

元素:

xpath=(//input[@class="ant-checkbox-input"])[1]
xpath=(//input[@class="ant-checkbox-input"])[2]
xpath=(//input[@class="ant-checkbox-input"])[3]

当前脚本:

*** Settings ***
Library    Browser

*** Variables ***
${Checkbox-1}   xpath=(//input[@class="ant-checkbox-input"])[1]
${Checkbox-2}   xpath=(//input[@class="ant-checkbox-input"])[2]
${Checkbox-2}   xpath=(//input[@class="ant-checkbox-input"])[3]

*** Test Cases ***
   Click    ${Checkbox-1}
   Click    ${Checkbox-2}
   Click    ${Checkbox-3}

期待脚本:

    *** Settings ***
    Library    Browser
    
    *** Variables ***
    ${Checkbox-1}   xpath=(//input[@class="ant-checkbox-input"])[1]
    ${Checkbox-2}   xpath=(//input[@class="ant-checkbox-input"])[2]
    ${Checkbox-2}   xpath=(//input[@class="ant-checkbox-input"])[3]
    ${CustomCheckbox}   I don't know how to construct
    *** Test Cases ***
    User select checkbox 1 and 2
      [Arguments]    ${not sure}  
       Click    ${CustomCheckbox}   1     2

table

第一行的外层 html
<tr data-row-key="1" class="ant-table-row ant-table-row-level-0">
    <td class="ant-table-cell ant-table-selection-column">
        <label class="ant-checkbox-wrapper">
            <span class="ant-checkbox">
                <input type="checkbox" class="ant-checkbox-input" value="">
                    <span class="ant-checkbox-inner"/>
                </span>
            </label>
        </td>
        <td class="ant-table-cell">1</td>
        <td class="ant-table-cell">101-39s5mb.mp4</td>
    </tr>

关键字在下面实现。

*** Variables ***
${checkbox_xpath}    xpath=(//input[@class="ant-checkbox-input"])

*** Test Cases ***
Test
    Log To Console    \nClick 1
    Name That You Want    1
    Log To Console    Click 2 and 3
    Name That You Want    2    3

*** Keywords ***
Name That You Want
    [Arguments]    @{index}
    
    FOR    ${key}    IN    @{index}
        Log To Console    ${checkbox_xpath}\[${key}\]
    END

输出:

==============================================================================
Main
==============================================================================
Test
Click 1
.xpath=(//input[@class="ant-checkbox-input"])[1]
.Click 2 and 3
.xpath=(//input[@class="ant-checkbox-input"])[2]
xpath=(//input[@class="ant-checkbox-input"])[3]
Test                                                                  | PASS |
------------------------------------------------------------------------------
Main                                                                  | PASS |
1 test, 1 passed, 0 failed
==============================================================================

您可以使用评估函数来执行此操作。

*** Settings ***

Library    Browser
Library    SeleniumLibrary

*** Variables ***

${Checkbox}   xpath=(//input[@class='ant-checkbox-input'])[{0}]

*** Test Cases ***
Test
    Click Checkbox    1  3  9

*** Keywords ***
Click Checkbox 
    [Arguments]  @{Values}
    FOR     ${value}    IN    @{Values}
        ${final xpath}    Evaluate    "${Checkbox}".format("${value}")
        Click Element    ${final xpath}
    END

根据您提供的值,最终的 xpath 将具有以下输出

(//输入[@class='ant-checkbox-input'])[1], (//输入[@class='ant-checkbox-input'])[3]....(//输入[@class='ant-checkbox-input'])[9]

或者你可以使用下面的方法

*** Settings ***
Library    Browser
Library    SeleniumLibrary

*** Variables ***

${Checkbox}   xpath=//input[@class='ant-checkbox-input']

*** Test Cases ***

Click Checkbox 

*** Keywords ***

Click Checkbox 
     # If you dont know how many elements it has,then you can get all the elements and click one by one.
     ${list of elements}  Get Webelements  ${Checkbox}
     For    ${element}   in  ${list of elements}
        Click Element    ${element}