# How to use Listeners in robot Framework

**URL:** <https://forum.robotframework.org/t/how-to-use-listeners-in-robot-framework/3727>\
**Category:** Robot Framework\
**Created:** [3 May 2022 12:04 UTC](https://forum.robotframework.org/t/how-to-use-listeners-in-robot-framework/3727 "2022-05-03T12:04:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Moshed](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@Moshed](https://forum.robotframework.org/u/Moshed)\
**Post date:** [3 May 2022 12:04 UTC](https://forum.robotframework.org/t/how-to-use-listeners-in-robot-framework/3727/1 "2022-05-03T12:04:17Z")

</div>

Hello,

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

> <https://github.com/robotframework/robotframework/blob/master/doc/userguide/src/ExtendingRobotFramework/ListenerInterface.rst>

and i also installed this library:

> **[robotframework-listenerlibrary](https://pypi.org/project/robotframework-listenerlibrary/)**
>
> Robot Framework Listener 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

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [7 May 2022 06:23 UTC](https://forum.robotframework.org/t/how-to-use-listeners-in-robot-framework/3727/2 "2022-05-07T06:23:00Z")

</div>

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](https://github.com/sebastianciupinski/robotframework-listenerlibrary#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.

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

```

Dave.

---

<div class="post-metadata">

**Author:** ![Moshed](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@Moshed](https://forum.robotframework.org/u/Moshed)\
**Post date:** [7 May 2022 13:18 UTC](https://forum.robotframework.org/t/how-to-use-listeners-in-robot-framework/3727/3 "2022-05-07T13:18:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [8 May 2022 05:27 UTC](https://forum.robotframework.org/t/how-to-use-listeners-in-robot-framework/3727/4 "2022-05-08T05:27:50Z")

</div>

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.

```python
***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.

```

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [8 May 2022 05:43 UTC](https://forum.robotframework.org/t/how-to-use-listeners-in-robot-framework/3727/5 "2022-05-08T05:43:25Z")

</div>

> [@Moshed](#):
>
> 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?

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_

```python
***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_

```python
***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_

```python
***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

```

---

<div class="post-metadata">

**Author:** ![Moshed](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@Moshed](https://forum.robotframework.org/u/Moshed)\
**Post date:** [8 May 2022 05:58 UTC](https://forum.robotframework.org/t/how-to-use-listeners-in-robot-framework/3727/6 "2022-05-08T05:58:07Z")

</div>

Thanks Dave,

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

Appreciate your assistance as always ☺
