Home > other >  IF ELSE in robot framework [Keyword as a condition]
IF ELSE in robot framework [Keyword as a condition]

Time:12-06

I just can't figure out how to map a keyword as a condition.

    @keyword("Is the Closed Message Page Present")
    def check_closedMsg_page(self):
        result = self.CLOSED_TEXT.is_displayed
        self.LOG(f"It returns {self.CLOSED_TEXT.is_displayed}")
        return result

The above function returns a bool value either True or False.

"Is the Closed Message Page Present" is a keyword which I want to make condition. If the condition is true then it should execute the below two keywords else skip it.

    IF  Is the Closed Message Page Present = True
        Then Login      username        password
        And Close Browsers
    END

I tried following:

IF  Is the Closed Message Page Present == 'True'
        Then Login      username        password
        And Close Browsers
    END
IF  'Is the Closed Message Page Present' == 'True'
        Then Login      username        password
        And Close Browsers
    END
Is the Closed Message Page Present
IF  True
        Then Login      username        password
        And Close Browsers
    END

I am expecting the keyword (Is the Closed Message Page Present) to be condition which needs to be true to execute the other two statements or keywords.

CodePudding user response:

I'm still new to the framework, but the only simple method I found is to store the keyword return value to a local variable and use that in the IF statement.

*** Settings ***
Library  SeleniumLibrary
Library  ../stackoverflow.py

*** Test Cases ***
robot Example

  ${value}  Is the Closed Message Page Present
  IF  ${value}
        Login      username        password
        Close Browser
  END

*** Keywords ***
Login
  [Arguments]    ${username}  ${password} 
  log  'Logs in to system'

stackoverflow.py returns a random True/False value

import random
from robot.api.deco import keyword

class stackoverflow:
    @keyword("Is the Closed Message Page Present")
    def check_closedMsg_page(self):
        return random.choice([True, False])

There is an exhaustive list of conditional expressions that you could further use at https://robocorp.com/docs/languages-and-frameworks/robot-framework/conditional-execution

  • Related