
Events and Listeners in Robot Framework
An event listener is a procedure or function in a computer program that waits for an event to occur. Example of events are before changing text, after clicking an element, before URL is loaded, etc. Creating effective logging and test script maintenance are the main conventions of event listener in automated testing. Collecting these details through standard logging is an alternative but explicit triggering would be required. Reducing the lines of code and make programmatic interface cleaner can be extra benefit of event listener.
While executing the automation scripts Selenium WebDriver track the numerous events that happen. By design, the listener will react to an event by calling the event’s handler. In Selenium WebDriver EventFiringWebDriver
, class throws an event and an interface named EventListener
catches that event. Through EventFiringWebDriver
, the user can have control to transform the default behavior of the driver. For example, a test failure event activates the automatic screen capture method defined in the EventListener
and starts logging screenshots.
SeleniumLibrary 4.0 has enabled importing EventFiringWebDriver
listener class as a parameter while importing the SeleniumLibrary. Current implementation supports only EventFiringWebDriver listener class written in Python.
*** Settings ***
Library SeleniumLibrary event_firing_webdriver=${CURDIR}/RobotFrameworkListener.py
Suite Teardown Close Browser*** Variables ***
${URL} http://www.google.com
${SEARCH} q***Keywords***
Open ${URL} in The Browser ${browser_name}
Create Webdriver ${browser_name} executable_path=/usr/local/bin/chromedriver
Go To ${URL}Find ${locator} Element And Enter ${text}
Input Text ${locator} ${text}*** Test Cases ***Test Case For Event Firing And Listener
Open ${URL} in The Browser Chrome
Find ${SEARCH} Element And Enter Event Firing
The sample script
- Launch a chrome browser.
- Navigate to the specified URL.
- Enter text in text box.
- Close the browser opened.
from robot.api import logger
from selenium.webdriver.support.events import AbstractEventListenerclass RobotFrameworkListener(AbstractEventListener):
def before_navigate_to(self, url, driver):
logger.info("URL before navigation: '%s'." % url) def after_navigate_to(self, url, driver):
logger.info("URL after navigation: '%s'." % driver.current_url) def before_change_value_of(self, element, driver):
dict= element.get_property('attributes')
element_value = dict[2]['value']
logger.info("Before clearing/entering the text field '%s'." % element_value) def after_change_value_of(self, element, driver):
dict= element.get_property('attributes')
element_value = dict[2]['value']
logger.info("After clearing/entering the text field '%s'." % element_value)
EventFiringWebDriver
listener class contains the following events listener methods implementation.
before_navigate_to ()
As the name suggest this method captures the URL before navigation.after_navigate_to ()
After navigation, the URL changes due to redirection so this method would be handy to seizure that. In the example, initial URL is http://www.google.com but after the navigation, the URL turn out to be https://www.google.com/?gws_rd=ssl.before_change_value_of ()
after_change_value_of ()
These events transpire when the value of a WebElement modifies either by theInput Test
orClear Element Text
keyword. Invocation of the preceding methods happen before or after the WebDriver changes the value of the WebElement. Capturing the value of the element is possible through the WebElement parameter. In the example,Input Test
keyword calls this method twice. This is because theInput Text
internally calls theClear Element Text
keyword. By passing theclear
parameter as false, this will stop.
If an exception arises during the event, these methods fail to invoke. In that case, the on_exception ()
method is invoked and the throwable object and the WebDriver object are sent to that method as parameters. An example is throwing an exception while failing to identify a WebElement. This would trigger the event notifying the on_exception ()
method.
The listener class import logger
module for test libraries logging purposes from the robot.api
package. This package exposes the public APIs of Robot Framework. It is possible to log messages using levels with the log level specific trace ()
, debug ()
, info ()
, warn ()
, error ()
functions. Default option is not to log the trace and debug messages but through command line options, it changeable.Warning and error is automatically wrote to console and to the Test Execution Errors section in log file.
The listener class also imports AbstractWebDriverEventListener
class provided by the WebDriver library. This class creates a dummy implementation such that listener class does not have to contain all the methods, only the ones required. Hence, implementation contains only four methods.

event_firing_webdriver
parameter is not used.