Search code examples
cssseleniumxpathrobotframework

How to reconstruct multiple variables into one with parameter?


The web page has a table with 11 check boxes.

I want to have the flexibility to let tester just input 1 or 2 or 3 to select specific checkbox.

How do I build the keyword with just ONE variable? so the tester do not need to input many lines of element variable.

the elements:

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

current script:

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

expect script:

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

outer html of first row in table

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

Solution

  • Keyword implements below.

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

    Output:

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