1#!/usr/bin/env python
2
3import json
4import traceback
5
6import websocket
7
8SERVER = 'ws://127.0.0.1:8642'
9AGENT = 'py-websockets-client'
10
11
12ws = websocket.create_connection(SERVER + "/getCaseCount")
13count = json.loads(ws.recv())
14ws.close()
15
16
17for case in range(1, count+1):
18    url = SERVER + '/runCase?case={0}&agent={1}'.format(case, AGENT)
19    status = websocket.STATUS_NORMAL
20    try:
21        ws = websocket.create_connection(url)
22        while True:
23            opcode, msg = ws.recv_data()
24            if opcode == websocket.ABNF.OPCODE_TEXT:
25                msg.decode("utf-8")
26            if opcode  in (websocket.ABNF.OPCODE_TEXT, websocket.ABNF.OPCODE_BINARY):
27                ws.send(msg, opcode)
28    except UnicodeDecodeError:
29        # this case is ok.
30        status = websocket.STATUS_PROTOCOL_ERROR
31    except websocket.WebSocketProtocolException:
32        status = websocket.STATUS_PROTOCOL_ERROR
33    except websocket.WebSocketPayloadException:
34        status = websocket.STATUS_INVALID_PAYLOAD
35    except Exception as e:
36        # status = websocket.STATUS_PROTOCOL_ERROR
37        print(traceback.format_exc())
38    finally:
39        ws.close(status)
40
41print("Ran {} test cases.".format(case))
42url = SERVER + '/updateReports?agent={0}'.format(AGENT)
43ws = websocket.create_connection(url)
44