if else в Robot Framework
Введение | |
Пример | |
Пример с Run Keyword If | |
Похожие статьи |
Введение
В этой статье вы можете изучить различные способы применения логических операторов в RobotFramework
Пример
Небольшой пример работы с настроенным фреймворком
*** Settings *** Documentation IF / ELSE IF / ELSE example using Robot Framework 3 and 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 *** Keywords *** Robot Framework 4: IF, ELSE IF, ELSE condition 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 *** Keywords *** Robot Framework 3: Use IF, ELSE IF, ELSE condition 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 *** Tasks *** Use if - else if - else conditions in Robot Framework Robot Framework 4: Use IF, ELSE IF, ELSE condition Robot Framework 3: Use IF, ELSE IF, ELSE condition
Пример с 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