def simulate_mouse_move(self, coords, relative=False): """ Simulate a mouse movement. @coords the x and y position of the mouse @relative if False, coords are an absolute position, otherwise a relative displacement #returns True if successfull, False otherwise """ request = protocol_pb2.Request() request.type = protocol_pb2.Request.SIMULATE_USER if relative: request.user.type = protocol_pb2.UserEvent.MOUSE_MOVE_REL else: request.user.type = protocol_pb2.UserEvent.MOUSE_MOVE_ABS request.user.xpos = coords[0] request.user.ypos = coords[1] response = self.send(request) if not response or response.error != protocol_pb2.Response.NO_ERROR: logging.error('[Client] failed to simulate a mouse move event') return False else: return True
def take_screenshot(self, name, win_id=0): """ Take a screenshot and save it to the file with the given name. @name file name where to save the screenshot @win_id identifier of the window from where to take the screenshot #returns True if successfull, False otherwise The file is saved locally, on the client side, in PNG format. If window ID is zero, then the screenshot is taken on the whole screen. Otherwise it is taken from the specified window ID (which is X11 specific) """ logging.info('[Client] taking a screenshot of the whole screen') request = protocol_pb2.Request() request.type = protocol_pb2.Request.TAKE_SCREENSHOT request.id = win_id response = self.send(request) if not response or response.error != protocol_pb2.Response.NO_ERROR: logging.error('[Client] failed to take screenshot') return False # got the screenshot, save it to a file with open(name, 'wb') as image: image.write(response.image) return True
def pack_request(info, request_id, folder, name, offset, size, block_hash, temporary): header = pack_header(const.MSG_REQUEST, info.compression) request = protocol_pb2.Request() request.id = request_id request.folder = folder request.name = name request.offset = offset request.size = size request.hash = block_hash #request.temporary = temporary message_length = struct.pack('>I',len(request.SerializeToString())) return header + message_length + request.SerializeToString()
def fetch_object_tree(self): """ Request the server to send the application current list of Qt objects. #returns the list of objects, empty in case of error """ request = protocol_pb2.Request() request.type = protocol_pb2.Request.FETCH_OBJECT_TREE response = self.send(request) if not response or response.error != protocol_pb2.Response.NO_ERROR: logging.error('[Client] failed to retrieve the object tree') return [] else: return response.objects
def start_recording_user(self): """ Start to record all of the user mouse and keyboard events #returns True if successfull, False otherwise """ request = protocol_pb2.Request() request.type = protocol_pb2.Request.RECORD_USER request.start = True response = self.send(request) if not response or response.error != protocol_pb2.Response.NO_ERROR: logging.error('[Client] failed to record the user events') return False else: return True
def kill_app(self): """ Force Application Under Test to close. #returns True if successfull, False otherwise """ logging.info('[Client] killing the remote application') request = protocol_pb2.Request() request.type = protocol_pb2.Request.KILL_APP response = self.send(request) if not response or response.error != protocol_pb2.Response.NO_ERROR: logging.error('[Client] failed to kill the remote application') return False else: return True
def fetch_object(self, obj): """ Request the server to send all of the properties from a Qt object. @obj the identifier of the object #returns list of object properties, empty in case of error """ request = protocol_pb2.Request() request.type = protocol_pb2.Request.FETCH_OBJECT request.id = obj response = self.send(request) if not response or response.error != protocol_pb2.Response.NO_ERROR: logging.error('[Client] failed to retrieve the object properties') return [] else: return response.properties
def server(port, secret, out=None): logger.info("Starting server ...") server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) if out: out = pycommons.open_file(output, 'wb') generic_logging.add_file_handler(out, logger) else: out = sys.stdout try: server_socket.bind(('', port)) except socket.error as e: logger.error('Bind failed! :' + e[1]) sys.exit(-1) server_socket.listen(10) while 1: sock, addr = server_socket.accept() # print str(addr) length = struct.unpack('>Q', common.sock_read(sock, 8))[0] logger.info("Request length: %d" % (length)) msg_buf = common.sock_read(sock, length) request = protocol_pb2.Request() request.ParseFromString(msg_buf) if request.secret != common.sha256(secret): response = protocol_pb2.Response() response.type = protocol_pb2.Response.GENERIC response.status = protocol_pb2.ERROR response.error = "Invalid secret" send_response(sock, response) sock.close() continue response = protocol_pb2.Response() if request.type == protocol_pb2.Request.KEY_REQUEST: handle_key_request(sock, request.keyRequest, response) send_response(sock, response) sock.close()
def stop_recording_user(self): """ Stop recording the user mouse and keyboard events. #returns the list of events recorded so far, None in case of error """ request = protocol_pb2.Request() request.type = protocol_pb2.Request.RECORD_USER request.start = False response = self.send(request) if not response or response.error != protocol_pb2.Response.NO_ERROR: logging.error('[Client] failed to record the user events') return None else: result = [] for event in response.events: result.append(event) return result
def client(host, port, secret, command, **kwargs): try: client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client_socket.connect((host, port)) except Exception: logger.critical('Could not connect to server!') logger.critical(traceback.format_exc()) return request = protocol_pb2.Request() request.secret = common.sha256(secret) if command == 'key': key_request(request, **kwargs) msg = request.SerializeToString() length = struct.pack('>Q', len(msg)) logger.debug("Request length: %d" % (len(msg))) client_socket.sendall(length + msg) logger.info("Request sent") handle_response(client_socket, **kwargs)
def simulate_mouse_button(self, button, press): """ Simulate a mouse button press or release @button the mouse button to press/release @press the state of the button #returns True if successfull, False otherwise """ request = protocol_pb2.Request() request.type = protocol_pb2.Request.SIMULATE_USER request.user.type = protocol_pb2.UserEvent.MOUSE_BUTTON request.user.press = press request.user.button = button response = self.send(request) if not response or response.error != protocol_pb2.Response.NO_ERROR: logging.error('[Client] failed to simulate a mouse button event') return False else: return True
def simulate_keyboard(self, key, press): """ Simulate a key press or release @key the key to press/release @press the state of the key #returns True if successfull, False otherwise """ request = protocol_pb2.Request() request.type = protocol_pb2.Request.SIMULATE_USER request.user.type = protocol_pb2.UserEvent.KEYBOARD request.user.press = press request.user.key = key response = self.send(request) if not response or response.error != protocol_pb2.Response.NO_ERROR: logging.error('[Client] failed to simulate a keyboard event') return False else: return True
def set_object_property(self, obj, name, value): """ Modify the specified property on the given object. @obj the identifier of the object @name name of the property to modify @value new value, as a bytearray, of the property #returns True if successfull, False otherwise """ request = protocol_pb2.Request() request.type = protocol_pb2.Request.WRITE_PROPERTY request.id = obj request.property.name = name request.property.value = value request.property.writable = True response = self.send(request) if not response or response.error != protocol_pb2.Response.NO_ERROR: logging.error('[Client] failed to set the object property') return False else: return True