if else в Robot Framework
| Введение | |
| Пример | |
| Сравнение переменной со строкой | |
| Сравнение c bool | |
| Run Keyword If | |
| Похожие статьи |
Введение
В этой статье вы можете изучить различные способы применения логических операторов в RobotFramework
Пример
*** Settings *** *** Test Cases *** Demo IF 1 == 1 Log To Console "One equals one" END
python -m robot .\demo.robot
==================================================================== Demo ==================================================================== Demo "One equals one" Demo | PASS | -------------------------------------------------------------------- Demo | PASS | 1 test, 1 passed, 0 failed ====================================================================
Небольшой пример для Robot Framework версии 4 и выше
*** Settings *** Documentation IF / ELSE IF / ELSE example using Robot Framework 4+. ... Generate a random number. ... Do if-else logic based on the generated number. ... Stop (pass) when the condition is met. *** Variables *** ${MAX_TRIES}= ${50} ${NUMBER_TO_PASS_ON}= 7 *** Test Cases *** IF ELSE Demo FOR ${i} IN RANGE ${MAX_TRIES} ${random}= Evaluate random.randint(0, 10) IF ${random} == ${NUMBER_TO_PASS_ON} Pass Execution "${random} == ${NUMBER_TO_PASS_ON}" ELSE IF ${random} > ${NUMBER_TO_PASS_ON} Log To Console Too high. ELSE Log To Console Too low. END END
python -m robot .\demo.robot
============================================================================== Demo :: IF / ELSE IF / ELSE example using Robot Framework 4+. Generate a ra... ============================================================================== IF ELSE Demo Too high. Too high. Too low. Too high. Too low. Too low. IF ELSE Demo | PASS | "7 == 7" ------------------------------------------------------------------------------ Demo :: IF / ELSE IF / ELSE example using Robot Framework 4+. Gene... | PASS | 1 test, 1 passed, 0 failed ==============================================================================
Пример для Robot Framework версии 3
*** Settings *** Documentation IF / ELSE IF / ELSE example using Robot Framework 3. ... Generate a random number. ... Do if-else logic based on the generated number. ... Stop (pass) when the condition is met. *** Variables *** ${MAX_TRIES}= ${50} ${NUMBER_TO_PASS_ON}= 7 *** Test Cases *** IF ELSE Demo In Robot Framework 3 FOR ${i} IN RANGE ${MAX_TRIES} ${random}= Evaluate random.randint(0, 10) ${is_expected}= Evaluate ${random} == ${NUMBER_TO_PASS_ON} Run Keyword Unless ... ${is_expected} ... Log To Console Condition not met. Run Keyword If ... ${is_expected} ... Pass Execution "${random} == ${NUMBER_TO_PASS_ON}" ... ELSE IF ... ${random} > ${NUMBER_TO_PASS_ON} ... Log To Console Too high. ... ELSE ... Log To Console Too low. END
Сравнение переменной со строкой
При сравнении переменных со строками нужно пользоваться кавычками
*** Settings *** *** Test Cases *** IF ELSE With Variables Compare With ABC ABC Compare With ABC www.devhops.ru *** Keywords *** Compare With ABC [Arguments] ${text} IF "${text}" == "ABC" Log To Console OK ELSE Log To Console Not ABC END
Либо
… *** Keywords *** Compare With ABC [Arguments] ${text} IF $text == "ABC" …
python -m robot .\demo.robot
============================================================================== Demo ============================================================================== IF ELSE With Variables OK .Not ABC IF ELSE With Variables | PASS | ------------------------------------------------------------------------------ Demo | PASS | 1 test, 1 passed, 0 failed ==============================================================================
Если не взять в кавычки ABC то будет ошибка
*** Keywords *** Compare With ABC [Arguments] ${text} IF "${text}" == ABC
============================================================================== IF ELSE With Variables | FAIL | Invalid IF condition: Evaluating expression '"ABC" == ABC' failed: NameError: name 'ABC' is not defined nor importable as module Variables in the original expression '"${text}" == ABC' were resolved before the expression was evaluated. Try using '$text == ABC' syntax to avoid that. See Evaluating Expressions appendix in Robot Framework User Guide for more details. ------------------------------------------------------------------------------ Demo | FAIL | 1 test, 0 passed, 1 failed ==============================================================================
Если не взять в кавычки ${text} то ошибка будет такой
*** Keywords *** Compare With ABC [Arguments] ${text} IF ${text} == "ABC"
============================================================================== IF ELSE With Variables | FAIL | Invalid IF condition: Evaluating expression 'ABC == "ABC"' failed: NameError: name 'ABC' is not defined nor importable as module Variables in the original expression '${text} == "ABC"' were resolved before the expression was evaluated. Try using '$text == "ABC"' syntax to avoid that. See Evaluating Expressions appendix in Robot Framework User Guide for more details. ------------------------------------------------------------------------------ Demo | FAIL | 1 test, 0 passed, 1 failed ==============================================================================
Сравнение переменной с bool
При сравнении с ${TRUE} и ${FALSE} строки нужно заключать в кавычки
*** Settings *** *** Test Cases *** IF ELSE With Bool Compare With Bool ${TRUE} Compare With Bool ${FALSE} Compare With Bool "www.heihei.ru" Compare With Bool ${0} Compare With Bool ${1} Compare With Bool ${2} Compare With Bool ${NONE} *** Keywords *** Compare With Bool [Arguments] ${val} IF ${val} == ${TRUE} Log To Console This is True ELSE IF ${val} == ${FALSE} Log To Console This is False ELSE Log To Console This is neither True nor False END
============================================================================== IF ELSE With Bool This is True .This is False .This is neither True nor False .This is False .This is True .This is neither True nor False .This is neither True nor False IF ELSE With Bool | PASS | ------------------------------------------------------------------------------ Demo | PASS | 1 test, 1 passed, 0 failed ==============================================================================
Пример с Run Keyword If
В данном примере используется RFBrowser на основе Playwright
*** Settings *** Documentation Example that opens single page Library Browser ... enable_playwright_debug=${True} ... auto_closing_level=TEST ... retry_assertions_for=0:00:03 *** Variables *** ${url} https://eth1.ru *** Keywords *** Start Chromium Browser New Browser browser=chromium headless=False slowMo=1 New Context viewport={'width': 1920, 'height': 1080} ignoreHTTPSErrors=True *** Test Cases *** Starting a browser with a page Start Chromium Browser New Page https://eth1.ru ${sample_title} = Set Variable eth1.ru ${actual_title} = Get Title # With Run Keyword If Run Keyword If "${actual_title}" == "${sample_title}" Log To Console "1: Actual title is equal to sample title" # With IF IF "${actual_title}" == "${sample_title}" Log To Console "2: Actual title is equal to sample title" END IF "${actual_title}" != None Log To Console "3: Title exists" END IF "${actual_title}" != "heihei.ru" Log To Console "4: visited url is not heihei.ru" END Close Browser
robot if.robot
======================================================================== If :: Example that opens single page ======================================================================== Starting a browser with a page ...eth1.ru .."1: Actual title is equal to sample title" ."2: Actual title is equal to sample title" ."3: Title exists" ."4: visited url is not heihei.ru" Starting a browser with a page | PASS | ------------------------------------------------------------------------ If :: Example that opens single page | PASS | 1 test, 1 passed, 0 failed ======================================================================== Output: /home/andrei/python/robot/rfbrowser/output.xml Log: /home/andrei/python/robot/rfbrowser/log.html Report: /home/andrei/python/robot/rfbrowser/report.html
Автор статьи: Андрей Олегович