robotframework 中的if語句---- run keyword if


一. 簡介

對比於python中的if關鍵字,robotframework中是用run keyword if關鍵字。

python中使用 if...elif...else 語句結構,而在robotframework中如下:

run keyword if    判斷條件    其他關鍵字
...    ELSE IF    判斷條件    其他關鍵字
...    ELSE    其他關鍵字

注意:ELSE IF,  ELSE一定要全大寫!

二. 使用示例

1. 數字對比

*** Variables ***
${num}    5

*** Test Cases ***
Test_001
    log to console    num=${num}
    run keyword if    ${num}>3    log to console    first success
    ...    ELSE    log to console    first fail

    run keyword if    ${num}>${3}    log to console    second success
    ...    ELSE    log to console    second fail

    run keyword if    ${num}<0    log to console    third success
    ...    ELSE IF    ${num}==5    log to console    It's five
    ...    ELSE    log to console    third fail

對比數字,直接寫數字值(3)或變量形式(${3})均可。

執行結果:

2. 字符串對比

在自動化中,經常會比較獲取到的頁面的值和預期值是否相同。

若變量為字符串,則變量和被比較值都要加單引號‘’或雙引號“”。

*** Variables ***
${str2}    abc
*** Test Cases ***
Test_001
    log to console    str=${str2}

    run keyword if    '${str2}'=='abc'   log to console    success
    ...    ELSE    log to console    fail

執行結果:

3. 布爾值對比

進行真假判斷時,run keyword if 后面可以直接跟變量名即可,如

run keyword if    ${var}      其他關鍵字

true和false大小寫均可,如${TRUE},${true}。

*** Variables ***
${str2}    abc
${str3}    ${true}
*** Test Cases ***
Test_001
    run keyword if    ${str2}   log to console    first success
    ...    ELSE    log to console    first fail

    run keyword if    ${str3}==${TRUE}   log to console    second success
    ...    ELSE    log to console    second fail

    run keyword if    ${str3}==${true}   log to console    third success
    ...    ELSE    log to console    third fail

    ${str6}    set variable    ${false}
    run keyword if    ${str6}==${false}   log to console    fourth success
    ...    ELSE    log to console    fourth fail

執行結果:

對於

run keyword if    ${var}==${false}    其他關鍵字

這種情況,也可以直接用

run keyword unless    ${var}    其他關鍵字

來代替。

run keyword unless 關鍵字,是當條件為假時,執行后面的關鍵字。

4. 判斷列表是否包含某元素

在判斷語句中可以使用not in和in來判斷某個元素是否在列表中。

*** Variables ***
${num}    5
@{num_list}     1    3    5    #這里的值被判定為字符串
@{num_list2}     ${1}    3    ${5}    #${5}是數字
*** Test Cases ***
Test_001
    run keyword if    ${num} not in ${num_list}    log to console    first success
    ...    ELSE    log to console    first fail

    run keyword if    '${num}' in ${num_list}    log to console    second success
    ...    ELSE    log to console    second fail

    run keyword if    ${num} in @{num_list2}    log to console    third success
    ...    ELSE    log to console    third fail

執行結果:

5. 判斷列表相等

*** Variables ***
@{num_list2}     1    3    5
@{num_list}     1    3    5

*** Test Cases ***
Test_001
    run keyword if    ${num_list} == ${num_list2}    log to console    first success
    ...    ELSE    log to console    first fail

    run keyword if    @{num_list} == @{num_list2}    log to console    second success
    ...    ELSE    log to console    second fail

執行結果:

6. 與run keywords結合使用

run keyword if    條件語句    run keywords    其他關鍵字
    ...    AND    其他關鍵字
    ...    AND    其他關鍵字

表示條件為真時,才執行run keywords后面的關鍵字。

7. 與set variable結合使用

*** Test Cases ***
Test_001
    ${num}    set variable    0
    ${result}    run keyword if    ${num}<0    set variable     ${${num}+1}
    ...  ELSE    set variable     ${${num}+2}
    log to console    ${result}

注意:${${num}+2} 這種形式,獲得的是數字相加的計算結果,為2;

如果寫成${num}+2這種形式,獲得的是兩個字符鏈接結果,為0+2.

執行結果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM