1
votes

I designed a robot framework testing library, a specific method defined as below:

import json
from robot.api import logger

...

@staticmethod
def select_list_first_element_info(select_list, select_key):
    if select_list and isinstance(select_list, list) and select_key:
        data_dict = select_list[0]
        json_dict = json.loads(data_dict)
        if select_key in json_dict.keys():
            return json_dict.get(select_key)
        else:
            logger.error(r"%s cannot be found in keys of dict variable json_dict." % select_key, html=True)
            return ""
    else:
        logger.error(r"select_list is not valid list or select_key is null.", html=True)
        return ""

But, I/O error occurred regardless of html=True or not.

20180302 01:18:55.723 - INFO - +--- START KW: lhvapi.Select List First Element Info [ ${dc_list} | ${key_dcid} ]
20180302 01:18:55.724 - ERROR - select_list is not valid list or select_key is null.
20180302 01:18:55.727 - FAIL - OSError: [Errno 5] Input/output error
20180302 01:18:55.727 - DEBUG - Traceback (most recent call last):
  File "/home/autorunner/virtual_env/lib/python3.6/site-packages/lhvapi/LhvRequests.py", line 162, in select_list_first_element_info
    logger.error(r"select_list is not valid list or select_key is null.", html=True)
  File "/home/autorunner/virtual_env/lib/python3.6/site-packages/robot/api/logger.py", line 131, in error
    write(msg, 'ERROR', html)
  File "/home/autorunner/virtual_env/lib/python3.6/site-packages/robot/api/logger.py", line 88, in write
    librarylogger.write(msg, level, html)
  File "/home/autorunner/virtual_env/lib/python3.6/site-packages/robot/output/librarylogger.py", line 44, in write
    LOGGER.log_message(Message(msg, level, html))
  File "/home/autorunner/virtual_env/lib/python3.6/site-packages/robot/output/logger.py", line 154, in _log_message
    self.message(msg)
  File "/home/autorunner/virtual_env/lib/python3.6/site-packages/robot/output/logger.py", line 141, in message
    logger.message(msg)
  File "/home/autorunner/virtual_env/lib/python3.6/site-packages/robot/output/console/quiet.py", line 28, in message
    self._stderr.error(msg.message, msg.level)
  File "/home/autorunner/virtual_env/lib/python3.6/site-packages/robot/output/console/highlighting.py", line 74, in error
    self.write(' ] %s\n' % message)
  File "/home/autorunner/virtual_env/lib/python3.6/site-packages/robot/output/console/highlighting.py", line 51, in write
    self.stream.write(console_encode(text, stream=self.stream))

OS: CentOS Linux release 7.4.1708 (Core)

Python: v3.6.3

Robot Framework: v3.0.2

2

2 Answers

0
votes

I did not reproduce your error, on Fedora Core 26. This is your modified code (removed the staticmethod annotation):

import json
from robot.api import logger


def select_list_first_element_info(select_list, select_key):
    """Example of select_list_first_element_info documentation.
    """

    if select_list and isinstance(select_list, list) and select_key:
        data_dict = select_list[0]
        json_dict = json.loads(data_dict)
        if select_key in json_dict.keys():
            return json_dict.get(select_key)
        else:
            logger.error(r"<b>%s</b> cannot be found in keys of dict variable <i>json_dict</i>." % select_key, html=True)
            return ""
    else:
        logger.error(r"<i>select_list</i> is not valid list or <i>select_key</i> is <b>null</b>.", html=True)
        return ""


if __name__ == "__main__":
    myjson = ['{"customer_user_id": "toto","password_user_id": "tutu","vpws_name": "VPWS-POC-002"}']

    print("value: ")
    print(select_list_first_element_info(myjson, "customer_user_id"))

And this is a sample test file:

*** Settings ***
Library           lhvapi.py

*** Test Cases ***
test keyword
    @{mydata}=    Create List    {"userid": "hisname","pass_userid": "secret","other_name": "Name"}
    ${myres}=    Select List First Element Info    ${mydata}    pass_userid
    Log To Console    ${myres}
    ${myres}=    Select List First Element Info    ${mydata}    username
    ${myres}=    Select List First Element Info    ${mydata}    ${None}
    ${myvar}=    Convert To String    ${mydata}
    ${myres}=    Select List First Element Info    ${myvar}    other_name

Then executed in virtualenv:

(venv) [helio@localhost Robot]$ pip install robotframework
Collecting robotframework
Installing collected packages: robotframework
Successfully installed robotframework-3.0.2
(venv) [helio@localhost Robot]$ robot test_lhvapi.robot 
==============================================================================
Test Lhvapi                                                                   
==============================================================================
test keyword                                                          ..secret
[ ERROR ] <b>username</b> cannot be found in keys of dict variable <i>json_dict</i>.
[ ERROR ] <i>select_list</i> is not valid list or <i>select_key</i> is <b>null</b>.
[ ERROR ] <i>select_list</i> is not valid list or <i>select_key</i> is <b>null</b>.
test keyword                                                          | PASS |
------------------------------------------------------------------------------
Test Lhvapi                                                           | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Output:  /home/helio/Test/Robot/output.xml
Log:     /home/helio/Test/Robot/log.html
Report:  /home/helio/Test/Robot/report.html
(venv) [helio@localhost Robot]$ python --version
Python 3.6.4
0
votes

Root course is stderr is redirected while robot attempt to logs. https://github.com/robotframework/robotframework/issues/2784