Циклы в Robot Framework
Введение | |
FOR | |
Перебор списка | |
Выход из цикла | |
WHILE | |
Похожие статьи |
Введение
В этой статье вы можете изучить различные способы применения циклов в RobotFramework
FOR
Стандартный цикл по перебору целых чисел
*** Settings *** Documentation An example of the for loop *** Tasks *** Numeric For ${i}= Set Variable 0 FOR ${i} IN RANGE 51 Log To Console ${i} END
robot loop.robot
============================================================================== Loop :: An example of the for loop ============================================================================== Numeric For .0 1 2 … 49 50 Numeric For | PASS | ------------------------------------------------------------------------------ Iloop :: An example of the for loop | PASS | 1 task, 1 passed, 0 failed ============================================================================== Output: /home/andrei/src/tests/robot/src/tests/output.xml Log: /home/andrei/src/tests/robot/src/tests/log.html Report: /home/andrei/src/tests/robot/src/tests/report.html
С помощью IN RANGE можно задать начальное и конечное значение интервала
FOR ${i} IN RANGE 50 59
Перебор списка
Рассмотрим пример перебора велосипедных брендов из списка : Stels , Forward , Author , Trek
*** Settings *** Documentation Regular loop over list *** Variables *** @{BIKES}= Stels Forward Author Trek *** Tasks *** Break out of the for loop on condition FOR ${bike} IN @{BIKES} Log To Console \n${bike} END
robot loop.robot
============================================================================== Loop :: Regular loop over list. ============================================================================== Regular loop over list Stels Forward Author Trek ------------------------------------------------------------------------------ Loop :: Regular loop over list | PASS | 1 task, 1 passed, 0 failed ============================================================================== Output: /home/andrei/src/tests/robot/src/tests/output.xml Log: /home/andrei/src/tests/robot/src/tests/log.html Report: /home/andrei/src/tests/robot/src/tests/report.html
Выход из цикла
Рассмотрим тот же пример, но с дополнительным условием на выход из цикла.
*** Settings *** Documentation An example breaking out of the for loop based on some condition. *** Variables *** @{BIKES}= Stels Forward Author Trek *** Tasks *** Break out of the for loop on condition FOR ${bike} IN @{BIKES} Exit For Loop If $bike == 'Author' Log To Console \n${bike} END
robot loop.robot
============================================================================== Loop :: An example breaking out of the for loop based on some condition. ============================================================================== Break out of the for loop on condition Stels Forward Break out of the for loop on condition | PASS | ------------------------------------------------------------------------------ Loop :: An example breaking out of the for loop based on some cond... | PASS | 1 task, 1 passed, 0 failed ============================================================================== Output: /home/andrei/src/tests/robot/src/tests/output.xml Log: /home/andrei/src/tests/robot/src/tests/log.html Report: /home/andrei/src/tests/robot/src/tests/report.html
WHILE
Начиная с пятой версии Robot Framework из коробки доступен цикл WHILE
*** Settings *** Documentation An example of while loop *** Tasks *** WHILE: zero to fifty ${x}= Set Variable ${0} WHILE ${x} < 51 Log To Console ${x} ${x}= Evaluate ${x} + 1 END
robot loop.robot
============================================================================== Iloop :: An example of while loop ============================================================================== WHILE: zero to fifty .0 1 2 … 49 50 WHILE: zero to fifty | PASS | ------------------------------------------------------------------------------ Iloop :: An example of while loop | PASS | 1 task, 1 passed, 0 failed ============================================================================== Output: /home/andrei/src/tests/robot/src/tests/output.xml Log: /home/andrei/src/tests/robot/src/tests/log.html Report: /home/andrei/src/tests/robot/src/tests/report.html
Пример с конкатенацией строк
*** Settings *** Documentation An example of while loop *** Tasks *** WHILE: zero to fifty ${x}= Set Variable ${0} WHILE ${x} < 51 Log To Console ${x} ${name}= Catenate name${x} Log To Console ${name} ${x}= Evaluate ${x} + 1 END
Цикл с условием внутри
Внутри цикла можно пользоваться
условными операторами
Например, выделим чётные числа
*** Settings *** Documentation Searching for even values *** Tasks *** Look for even ${i}= Set Variable 0 FOR ${i} IN RANGE 7 Log To Console ${i} IF ${i % 2} == 0 Log To Console "i is even" Sleep 1 END END
robot ex.robot
============================================================================== Ex :: Searching for even values ============================================================================== Look for even .0 "i is even" 1 2 "i is even" 3 4 "i is even" 5 6 "i is even" Look for even | PASS | ------------------------------------------------------------------------------ Ex :: Searching for even values | PASS | 1 task, 1 passed, 0 failed ============================================================================== Output: /home/andrei/robot/output.xml Log: /home/andrei/robot/log.html Report: /home/andrei/robot/report.html