Home > Net >  How to reconstruct multiple variables into one with parameter?
How to reconstruct multiple variables into one with parameter?

Time:11-10

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[@])[1]
xpath=(//input[@])[2]
xpath=(//input[@])[3]

current script:

*** Settings ***
Library    Browser

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

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

CodePudding user response:

You can refer to this section "Variable number of arguments with user keywords".

My solution:

You can use this keyword with at least one or more parameters.

*** Test Cases ***
Test
    Name That You Want    Test1
    Name That You Want    Test1    Test2    Test3

*** Keywords ***
Name That You Want
    [Arguments]    ${checkbox}    @{checkbox_list}
    Click    ${checkbox}
    FOR    ${xpath}    IN    @{checkbox_list}
        Click    ${xpath}
    END

CodePudding user response:

You can use the Evaluate function to do this.

*** Settings ***

Library    Browser
Library    SeleniumLibrary

*** Variables ***

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

*** Test Cases ***

Click Checkbox 

*** Keywords ***
Click Checkbox  
    # For Example, From 0 to 9 looping it, you can modify it as you want
    FOR     ${value}    IN RANGE    10
        ${final xpath}    Evaluate    "${Checkbox}".format("${value}")
        Click Element    ${final xpath}

Final xpath will have the below output for each loop respectively

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

Or You can use below method

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