# ecoding=utf-8

import socket
import time

class RobotListener3(object):

    ROBOT_LIBRARY_SCOPE = 'GLOBAL'
    ROBOT_LISTENER_API_VERSION = 3
    
    ## robot --listener Listener:50007:false
    def __init__(self):
        self.sock = socket.socket()
        self._connect()

    def _close(self):
        self.sock.close()

    def _connect(self):
        self.conn = self.sock.connect(("127.0.0.1", 50007))
        self._send_socket(b"connecting  ........ OK")

    def start_suite(self, data, result):
        self._send_socket("Starting Suite : " + str(data) + str(result))

    def start_test(self, data, result):
##        tags = ''.join(attrs['tags'])
        self._send_socket("Starting test : " +  str(data) + str(result))

    def start_keyword(self, name, attrs):
        self._send_socket('Executing start keyword %s with arguments %s' % (name, attrs['args']))

    def end_keyword(self, name, attrs):
        self._send_socket('Executing end keyword %s with arguments %s' % (name, attrs['args']))

    def end_test(self, 	data, result):
        if not result.passed:
            self._send_socket('Test "%s" failed: %s' % (result.name, result.message))
        else:
            self._send_socket('Test "%s" PASS: %s' % (result.name, result.message))
    
    def end_suite(self, data, result):
        if not result.passed:
            self._send_socket('Ending suite "%s" failed: %s' % (result.name, result.message))
        else:
            self._send_socket('Ending suite "%s" PASS: %s' % (result.name, result.message))

 
    def _send_socket(self, msg):
        str(msg).replace(u'\xa0', u' ')
        data = ('%s' % msg).encode("gbk")
        
        try:
            self.sock.sendall(data)
        except Exception:
            self._close()
            self._connect()

    def log_file(self, path):
        self._send_socket('log_file available at %s' % path)

    def report_file(self, path):
        self._send_socket('report_file available at %s' % path)

    def xunit_file(self, path):
        self._send_socket('xunit_file available at %s' % path)

    def debug_file(self, path):
        self._send_socket('debug_file available at %s' % path)

    def close(self):
        self._send_socket('All tests executed---close')
        self._close()

    def message(self, message):
        self._send_socket(str(message))

    def log_message(self, message):
        self._send_socket(str(message))

  

posted on 2021-12-23 11:26  Old-Kang  阅读(50)  评论(0编辑  收藏  举报