Robot Framework provides several ways to handle exceptions, allowing you to gracefully manage errors and prevent your tests from abruptly stopping. Here's a breakdown of the common techniques:
1. Run Keyword And Expect Error
:
***Test Cases***
Verify Invalid Login
Run Keyword And Expect Error Invalid username or password Login User invalid_user wrong_password
Log Error message was expected
2. Run Keyword And Ignore Error
:
***Test Cases***
Attempt to Close Window
Run Keyword And Ignore Error Close Window
Log Window closing attempted
3. TRY / EXCEPT
Blocks:
try-except
blocks in Python.TRY
, and if any keyword within that block raises an exception, the execution jumps to the corresponding EXCEPT
block. You can have multiple EXCEPT
blocks to handle different types of exceptions or error messages.***Test Cases***
Handle Database Connection
TRY
Connect to Database
Query Database
EXCEPT Database connection failed
Log Failed to connect to the database
EXCEPT Database query error
Log Error during database query
ELSE
Log Database operations successful
FINALLY
Disconnect from Database # Always disconnect, even if there were errors
4. Assertions:
Should Be Equal ${actual_value} ${expected_value}
Element Should Be Visible login_button
5. Fatal Error
:
Fatal Error
, Robot Framework will stop the test run immediately.from robot.api.exceptions import FatalError
def my_keyword():
if some_critical_error:
raise FatalError("Critical error occurred!")