4

Is there any option in Robot framework to set the value of a variable "TRUE" or "FALSE" depending upon a condition ?

I want to run a keyword only if a condition is TRUE, i.e. a keyword should run only if a particular ID exists in present screen.

Here is my test script -

*** Settings ***
Library  Selenium2Library
Test Setup  Open Browser  http://xyz.com  chrome
Test Teardown  Close All Browsers

*** Variables ***
${IdExist}  None

*** Test Cases ***
Test1
  Open Application and check page content

*** Keywords ***
Open application and check page content
  Maximize Browser Window
  Sleep  2s
  Click Element  id=Start
  Sleep  2s
  ${IdExist} = Page Should Contain Element  id=hamburger-icon
  Run Keyword if   "${IdExist}" == "PASS"  Click Button

Click Button
  Click Element  id=hamburger-icon   

Is this a Correct way of writing a test script to set the value for a variable?

${IdExist} = Page Should Contain Element id=hamburger-icon

I found this particular piece of code in a website ! But, while running the above test case, the value of the variable is not getting set as TRUE / PASS value, even though the particular icon exists in present screen (The keyword Click Button is not at all triggering!)

Is there any other effective way to handle this particular scenario?

Am I missing something? Please help me to solve this issue.

2

The syntax is cumbersome, but there are a couple things you can do.

If you don't care about the message returned by the first keyword, you can use Run keyword and return status. This is perhaps the easiest solution, since the keyword will return either True or False.

${status}=    Run keyword and return status
...    Page should contain element    id=hamburger-icon
Run keyword if  ${status}   Click button

If you want the status of a keyword and also what it returns, you can call Run Keyword and Ignore Error, which returns two values: "PASS" or "FAIL", and the return value or error message from the keyword. You can then use the "PASS" or "FAIL" result in your if statement.

${status}    ${result}=    Run keyword and ignore error
...    Page Should Contain Element  id=hamburger-icon
Run keyword if    "${status}" == "FAIL"
...    Click button

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.