Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Robotframework Listener throws "Cannot access execution context" error

In order to support an alternative logging format I've started the development of a custom Robotframework Listener. Using the examples in the guide I've been able to replicate the simple PythonListner example. This example can be succesfully run using:

python.exe -m robot.run --listener C:\temp\tiny.py -s Test02.Test C:\temp\Test02

The by the listener class generated file contains the different events that have occurred and the functionality is working as it should.

When adding the following line to the __init__ method of the class:

BuiltIn().get_variable_value('${SUITE SOURCE}')

The following response is received:

 failed: Creating instance failed: RobotNotRunningError: Cannot access execution context
Traceback (most recent call last):
  File "C:\temp\tiny.py", line 9, in __init__
    print  repr(BuiltIn().get_variables())
  File "C:\Python27\lib\site-packages\robot\libraries\BuiltIn.py", line 940, in get_variables
    return utils.NormalizedDict(self._variables.current, ignore='_')
  File "C:\Python27\lib\site-packages\robot\libraries\BuiltIn.py", line 2669, in _variables
    return self._namespace.variables
  File "C:\Python27\lib\site-packages\robot\libraries\BuiltIn.py", line 2661, in _namespace
    return self._context.namespace
  File "C:\Python27\lib\site-packages\robot\libraries\BuiltIn.py", line 2656, in _context
    raise RobotNotRunningError('Cannot access execution context')
==============================================================================
Test02
==============================================================================
Test02.Test
==============================================================================
Process Data File                                                     | PASS |
------------------------------------------------------------------------------
Test02.Test                                                           | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Test02                                                                | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Output:  C:\temp\output.xml
Log:     C:\temp\log.html
Report:  C:\temp\report.html

The test passes but the file is not generated. This result has been replicated on Python 2.7 and Robotframework 3.0 and 2.8.5. It is odd and I need some help to further analyse or (ideally) solve this problem.

Since the Python class is called by Robotframework I'm unsure how to continue debugging this, so feel free to make suggestions there as well. In any case I'm stuck and could use your help.

like image 264
A. Kootstra Avatar asked Dec 21 '16 15:12

A. Kootstra


1 Answers

You are trying to reference BuiltIn() before the test starts. That is what is meant by the error Cannot access execution context: the test hasn't started so there is no execution context. Simply put, you cannot access methods of the BuiltIn library before the test runs.

You will have to wait to call that statement until after the test has started running.

For example:

class listener:
    ROBOT_LISTENER_API_VERSION = 2

    def start_suite(self, name, attrs):
        self.suite_source = BuiltIn().get_variable_value('${SUITE SOURCE}')

However, if all you need is the source of the current suite, that information is available in the listener method start_suite.

For example:

class MyListener:
    ROBOT_LISTENER_API_VERSION = 2

    def start_suite(self, name, attrs):
        self.suite_source = attrs["source"]
like image 58
Bryan Oakley Avatar answered Nov 13 '22 09:11

Bryan Oakley