# ecoding=utf-8

import socket
import time

##ROBOT_LISTENER_API_VERSION = 2

class RobotListener2(object):

    ROBOT_LIBRARY_SCOPE = 'GLOBAL'
    ROBOT_LISTENER_API_VERSION = 2
    
    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, name, attrs):
        self._send_socket("Starting Suite : " + str(name) + str(attrs["doc"]) + str(attrs["tests"]) +str(attrs["totaltests"])+ str(attrs['starttime']))

    def start_test(self, name, attrs):
        self._send_socket("Starting test : " +  str(name) + str(attrs["doc"]) + str(attrs["tags"]) + str(attrs['starttime']))


    def end_test(self, name, attrs):
        if attrs['status'] == 'FAIL':
            self._send_socket('Test "%s" failed: %s  time:%s' % (name, attrs['message'],attrs['endtime']))
        else:
            self._send_socket('Test "%s" PASS: %s time:%s' % (name, attrs['message'], attrs['endtime']))
    
    def end_suite(self, name, attrs):
        if attrs['status'] == 'FAIL':
            self._send_socket('Ending suite:  "%s" failed: %s time:%s' % (name, attrs['message'],attrs['endtime']))
        else:
            self._send_socket('Ending suite:  "%s" PASS: %s time:%s' % (name, attrs['message'], attrs['endtime']))

    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 close(self):
        self._send_socket('All tests executed---close')
        self._close()

    def message(self, message):
        self._send_socket(str(str(message['level']) +" "+ message['timestamp']) +" "+ str(message['message']))

    def log_message(self, message):
        self._send_socket(str(str(message['level']) +" "+ message['timestamp']) +" "+ str(message['message']))

  

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