def get_screen(self, path, log_level=logging.INFO): """ Save screen of mobile device. :param path: Path to image that will be saved. :param log_level: Log level. """ # Ensure folder to save the screen exists File.delete(path) Folder.create(folder=os.path.dirname(path)) if self.type is DeviceType.EMU or self.type is DeviceType.ANDROID: Adb.get_screen(device_id=self.id, file_path=path) if self.type is DeviceType.SIM: Simctl.get_screen(sim_id=self.id, file_path=path) if self.type is DeviceType.IOS: IDevice.get_screen(device_id=self.id, file_path=path) image_saved = False if File.exists(path): size = os.path.getsize(path) if size > 10: image_saved = True if image_saved: message = "Image of {0} saved at {1}".format(self.id, path) Log.log(level=log_level, msg=message) else: message = "Failed to save image of {0} saved at {1}".format( self.id, path) Log.error(message) raise Exception(message)
def kill(proc_name, proc_cmdline=None): if Settings.HOST_OS is OSType.WINDOWS: proc_name += ".exe" result = False for proc in psutil.process_iter(): name = "" cmdline = "" try: name = str(proc.name()) cmdline = str(proc.cmdline()) except Exception: continue if proc_name == name: if Settings.HOST_OS == OSType.WINDOWS: cmdline = cmdline.replace('\\\\', '\\') if (proc_cmdline is None) or (proc_cmdline is not None and proc_cmdline in cmdline): try: proc.kill() Log.log(level=logging.DEBUG, msg="Process {0} has been killed.".format( proc_name)) result = True except psutil.NoSuchProcess: continue return result
def activate_workspace(self, WS_CLASS): ws_to_activate = self.workspaces[WS_CLASS.get_id()] if ws_to_activate != self.curr_workspace: if None != self.curr_workspace: self.curr_workspace.deactivate() self.curr_workspace = ws_to_activate self.curr_workspace.activate() self.curr_workspace.tkraise() Log.log("Activated workspace " + self.curr_workspace.get_id())
def run(): Log.log_application_name = "Test" Log.log("Starting...") window = Window() window.add_workspace(WsStart) window.add_workspace(WsTest) window.activate_workspace(WsStart) window.run()
def kill_pid(pid): try: proc = psutil.Process(pid) proc.terminate() Log.log(level=logging.DEBUG, msg="Process has been killed: {0}{1}".format( os.linesep, proc.cmdline())) except Exception: pass
def __init__(self, *args, **kwargs): Log.log("Window init") Tk.__init__(self, *args, **kwargs) self.ws_controller = WorkspaceController() self.btn_frame = Frame(self) self.ws_frame = Frame(self) self.btn_frame.grid(row=0, column=0) self.ws_frame.grid(row=0, column=1) test_label = ttk.Label(self.ws_frame, text="This works?!") test_label.grid(row=0, column=0)
def kill_by_commandline(cmdline): result = False for proc in psutil.process_iter(): cmd = "" try: cmd = str(proc.cmdline()) except Exception: continue if cmdline in cmd: try: proc.kill() Log.log(level=logging.DEBUG, msg="Process {0} has been killed.".format(cmdline)) result = True except psutil.NoSuchProcess: continue return result
def save_screen(path, log_level=logging.DEBUG): """ Save screen of host machine. :param path: Path where screen will be saved. :param log_level: Log level of the command. """ Log.log(level=log_level, msg='Save current host screen at {0}'.format(path)) if Settings.HOST_OS is OSType.LINUX: os.system("import -window root {0}".format(path)) else: try: from PIL import ImageGrab image = ImageGrab.grab() image.save(path) except IOError: Log.error('Failed to take screen of host OS') if Settings.HOST_OS is OSType.OSX: Log.info('Retry...') run(cmd='screencapture ' + path)
def __init__(self): Log.log("WorkspaceController init") self.workspaces = {} self.curr_workspace = None
def run(cmd, cwd=Settings.TEST_RUN_HOME, wait=True, timeout=600, fail_safe=False, register=True, log_level=logging.DEBUG): # Init result values time_string = datetime.now().strftime('%Y_%m_%d_%H_%M_%S_%f') log_file = os.path.join(Settings.TEST_OUT_LOGS, 'command_{0}.txt'.format(time_string)) complete = False duration = None output = '' # Ensure logs folder exists dir_path = os.path.dirname(os.path.realpath(log_file)) Folder.create(dir_path) # Command settings if not wait: # Redirect output to file File.write(path=log_file, text=cmd + os.linesep + '====>' + os.linesep) cmd = cmd + ' >> ' + log_file + ' 2>&1 &' # Log command that will be executed: Log.log(level=log_level, msg='Execute command: ' + cmd) Log.log(level=logging.DEBUG, msg='CWD: ' + cwd) # Execute command: if wait: start = time.time() with open(log_file, mode='w') as log: if Settings.HOST_OS == OSType.WINDOWS: process = subprocess.Popen(cmd, cwd=cwd, shell=True, stdout=log, stderr=log) else: process = subprocess.Popen(cmd, cwd=cwd, shell=True, stdout=subprocess.PIPE, stderr=log) # Wait until command complete try: process.wait(timeout=timeout) complete = True out, err = process.communicate() if out is not None: if Settings.PYTHON_VERSION < 3: output = str(out.decode('utf8').encode('utf8')).strip() else: output = out.decode("utf-8").strip() except subprocess.TimeoutExpired: process.kill() if fail_safe: Log.error('Command "{0}" timeout after {1} seconds.'.format( cmd, timeout)) else: raise # Append stderr to output stderr = File.read(path=log_file) if stderr: output = output + os.linesep + File.read(path=log_file) # noinspection PyBroadException try: File.delete(path=log_file) except Exception: Log.debug('Failed to clean log file: {0}'.format(log_file)) log_file = None end = time.time() duration = end - start else: process = psutil.Popen(cmd, cwd=cwd, shell=True, stdin=None, stdout=None, stderr=None, close_fds=True) # Get result pid = process.pid exit_code = process.returncode # Log output of the process if wait: Log.log(level=log_level, msg='OUTPUT: ' + os.linesep + output + os.linesep) else: Log.log(level=log_level, msg='OUTPUT REDIRECTED: ' + log_file + os.linesep) # Construct result result = ProcessInfo(cmd=cmd, pid=pid, exit_code=exit_code, output=output, log_file=log_file, complete=complete, duration=duration) # Register in TestContext if psutil.pid_exists(result.pid) and register: TestContext.STARTED_PROCESSES.append(result) # Return the result return result
def __init__(self, parent_frame, ws_controller): Log.log("WsStart init called") WorkspaceBase.__init__(self, parent_frame, ws_controller) ws_header = ttk.Label(self, text = "This is the start workspace") ws_header.grid(row = 0, column = 0)
def destroy(self): Log.log("Destroy called")
def deactivate(self): Log.log("Deactivate called")
def pause(self): Log.log("Pause called")
def activate(self): Log.log("Activate called")