How to use Listeners in robot Framework

Hello,

I’m Struggling to develop listeners in robot framework.
i’ve read the documentation-

and i also installed this library:

my main purpose is to log when the suite/ test started/ ended & when test failed / passed.
i would like to run a keyword when test passed/ or failed.

can anyone guide me how to do this ?? without running command line with – listener
Thanks,

Moshe

Hi Moshe,

That second link you gave, i’ve not used that library before but it looks like it will do what you want.

I followed the link to the home page for that library and found this section: usage

In that example, Register End Keyword listener is what I think your after, and Log is the keyword (replace it with the keyword you want) and the rest of the line are the arguments to Log.

Register End Keyword listener      Log   message form end keyword listener    level=WARN

Dave.

Hi Dave,
Thanks for your help.

I tried to use the examples from the listener usage but failed to implement it.

I would like to do some actions ( log + another action) when keyword fails ( generic for all keyword) , how can I use it in test setup/ teardown?

Is it possible to use the listeners ( let say that I have listener for when test fail or pass or skip or keyword pass / fail), in one robot file and use them as a generic action in other test cases / robot files?

Thanks
Moshe

Hi Moshe,

I figured the best thing to do was build an example for you, copy this example as moshe.robot and run it, then look at the log.html hopefully it will make everything clear.

As I see it the advantage of using the listener library would be if you wanted to do something after each keyword, otherwise I think the teardown is slightly easier, but both are workable solutions.

Dave.

*** Settings ***
Library      ListenerLibrary
Test Teardown    Teardown Keyword


*** Test Cases ***
Moshes Example Passing Test
	Register End Test Listener		Listener Keyword
	Log  This is an example of a test that passed

Moshes Example Failing Test
	Register End Test Listener		Listener Keyword
	Fail  This is an example of a test that failed


*** Keywords ***
Listener Keyword
	Log  This line is always executed, ${TEST NAME}: ${TEST STATUS}
	# https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#automatic-variables
	IF  "${TEST STATUS}" == "FAIL"
		Another Keyword
	END

Another Keyword
	Log  This keyword only gets called by the listener when the test failed.

Teardown Keyword
	Log  The teardown is called after the Listener Keyword, ${TEST NAME}: ${TEST STATUS}
	IF  "${TEST STATUS}" == "FAIL"
		Yet Another Keyword
	END

Yet Another Keyword
	Log  This keyword only gets called by the teardown when the test failed.

Another example to demonstrate this for you, save these 3 files, and then call robot Moshe0*.robot and look at log.html

Moshe_Shared.robot

*** Settings ***
Library      ListenerLibrary

*** Keywords ***
Listener Keyword
	Log  This line is always executed, ${TEST NAME}: ${TEST STATUS}
	# https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#automatic-variables
	IF  "${TEST STATUS}" == "FAIL"
		Another Keyword
	END

Another Keyword
	Log  This keyword only gets called by the listner when the test failed.

Teardown Keyword
	Log  The teardown is called after the Listener Keyword, ${TEST NAME}: ${TEST STATUS}
	IF  "${TEST STATUS}" == "FAIL"
		Yet Another Keyword
	END

Yet Another Keyword
	Log  This keyword only gets called by the teardown when the test failed.

Moshe01.robot

*** Settings ***
Test Teardown    Teardown Keyword
Resource     Moshe_Shared.robot


*** Test Cases ***
Moshes Example 1 Passing Test
	Register End Test Listener		Listener Keyword
	Log  This is an example of a test that passed

Moshes Example 1 Failing Test
	Register End Test Listener		Listener Keyword
	Fail  This is an example of a test that failed

Moshe02.robot

*** Settings ***
Test Teardown    Teardown Keyword
Resource     Moshe_Shared.robot


*** Test Cases ***
Moshes Example 2 Passing Test
	Register End Test Listener		Listener Keyword
	Log  This is an another example of a test that passed

Moshes Example 2 Failing Test
	Register End Test Listener		Listener Keyword
	Fail  This is an another example of a test that failed

1 Like

Thanks Dave,

These Examples really helped me and i hope will help others.

Appreciate your assistance as always :smiling_face:

2 Likes