Coding Keyword

󰃭 2024-10-02

Keyword

Keyword return value, variable

*** Test Cases ***
004_1
    ${result}    Keyword return value #1
    log to console    \n${result}

004_2 keyword return variable
    ${result}    Keyword return variable #2
    log to console    \n${result}

*** Keywords ***
Keyword return value #1
    return from keyword    test ok

Keyword return variable #2
    ${return}    set variable    test ok
    return from keyword    ${return}

!!! warning “Notice” keyword ที่ return value จำเป็นต้องมีการเอาตัวแปรมารับ response ด้วย

[Return] for return

*** Test Cases ***
004_3
    ${result}    Keyword return value #3
    log to console    \n${result}

*** Keywords ***
Keyword return value #3
    [Return]    test ok

[Return] multiple values

*** Test Cases ***
004_4
    ${result}    ${result2}    Keyword return value #4
    log to console    \n${result}    \n${result2}

*** Keywords ***
Keyword return value #4
    [Return]    test ok    test ok2

Keyword with 1 argument

*** Test Cases ***
004_5
    keyword with 1 argument    test ok

*** Keywords ***
keyword with 1 argument
    [Arguments]    ${arg1}
    log to console    ${arg1}

Keyword with 2 arguments

*** Test Cases ***
004_6
    keyword with 2 arguments    test ok    test ok2

*** Keywords ***
keyword with 2 arguments
    [Arguments]    ${arg1}    ${arg2}
    log to console    ${arg1}    ${arg2}

Keyword with Optional arguments

*** Test Cases ***
004_7
    keyword with optional arguments    test ok

*** Keywords ***
keyword with optional arguments
    [Arguments]    ${arg1}    ${arg2}=test ok2
    log to console    ${arg1}    ${arg2}

!!! warning “Notice” argument ตัวแรกจะเป็น required ตัวที่สองจะเป็น optional ได้ แต่ไม่สามารถใส่ตัวที่สองโดยไม่ใส่ตัวแรกได้

Keyword with list argument

*** Test Cases ***
004_8
    keyword with list argument    test ok    test ok2

*** Keywords ***
keyword with list argument
    [Arguments]    @{arg1}
    log to console    ${arg1}

Keyword with dictionary argument

*** Test Cases ***
004_9
    keyword with dictionary argument    name=suthiphong    surname=kong

*** Keywords ***
keyword with dictionary argument
    [Arguments]    &{arg1}
    log to console    ${arg1}

Guessing Number with return from keyword if

*** Test Cases ***
004_10
    Guessing Number    5

*** Keywords ***
Guessing Number
    [Arguments]    ${number}
    return from keyword if    ${number} == 5    test ok    test fail

Chain Keyword

*** Test Cases ***
004_11
    Keyword a    ${5}

*** Keywords ***
Keyword b 
    [Arguments]    ${input}
    log to console    ${input}
    [Return]    ${input+1}

Keyword a
    [Arguments]    ${input}
    ${result}    keyword b    ${input}
    log to console    received from keyword b ${result}

Example DONE

*** Test Cases ***
004_1
    ${result}    Keyword return value #1
    log to console    \n${result}

004_2 keyword return variable
    ${result}    Keyword return variable #2
    log to console    \n${result}

004_3 keyword return variable
    ${result}    Keyword return variable #3
    log to console    \n${result}

004_4 keyword return multiple value
    ${result1}    ${result2}    Keyword return multiple value
    log to console    \n${result1}----${result2}

004_5 keyword with 1 argument
    keyword with 1 argument    test ok

004_6 keyword with 2 argument
    keyword with 2 argument    test ok    test fail

004_7 keyword with optional argument or default value
    keyword with optional argument or default value    test ok

004_8 keyword with list argument
    @{arg}    create list     test ok    test fail
    keyword with list argument    @{arg}

004_9 keyword with dicts argument
    &{arg}    create dictionary    name=suthiphong    surname=kong
    keyword with dicts argument    &{arg}

004_10 guessing number
    ${result}    guessing number    2
    log to console    \n${result}

004_11 chain keyword
    keyword a    ${1}

*** Keywords ***
Keyword return value #1
    return from keyword    test ok

Keyword return variable #2
    ${return}    set variable    test ok
    return from keyword    ${return}

Keyword return variable #3
    ${return}    set variable    test ok
    [Return]    ${return}

Keyword return multiple value
    ${return1}    set variable    test ok
    ${return2}    set variable    test fail
    [Return]    ${return1}    ${return2}

keyword with 1 argument
    [Arguments]    ${arg1}
    log to console    \n\n\n${arg1}

keyword with 2 argument
    [Arguments]    ${arg1}    ${arg2}
    log to console    \n\n\n${arg1}----${arg2}


keyword with optional argument or default value
    [Arguments]    ${arg1}    ${arg2}=default value
    log to console    \n\n\n${arg1}----${arg2}

keyword with list argument
    [Arguments]    @{arg}
    log to console    \n\n\n log list ${arg}

keyword with dicts argument
    [Arguments]    &{arg}
    log to console    \n\n\n log dict ${arg}

guessing number
    [Arguments]    ${number}
    return from keyword if    ${number}==1    Correct!!!
    return from keyword    Guessing number again

Keyword b 
    [Arguments]    ${input}
    log to console    ${input}
    [Return]    ${input+1}

Keyword a
    [Arguments]    ${input}
    ${result}    keyword b    ${input}
    log to console    received from keyword b ${result}