def list_dir(self, relative_path): """列出某个目录下的文件 :type relative_path:string :param relative_path: ,资源文件目录相对路径,相对于setting下的资源目录的路径,支持多级目录 :return:返回一个包含资源目录下所有文件或者文件下的绝对路径列表 """ result = [] relative_path = self._adjust_path(relative_path) if relative_path.startswith(os.sep): relative_path = relative_path[1:] for it in self._resources_dirs: dir_path = self._adjust_path(os.path.join(it, relative_path)) dir_path = smart_text(dir_path) if os.path.isdir(dir_path): result.append(dir_path) if len(result) > 1: logger.error("找到多个目录:") for item in result: logger.error("%s" % item) raise Exception("存在多个%s目录" % relative_path) if len(result) < 1: raise Exception("%s目录不存在" % relative_path) paths = [] for path in os.listdir(result[0]): paths.append(os.path.join(result[0], path)) return paths
def post_test(self): '''清理测试用例 ''' self._run_test_complete = True if hasattr(self, '_record_thread_status_dict' ) and self._record_thread_status_dict: timeout = 30 time0 = time.time() while time.time() - time0 < timeout: for key in self._record_thread_status_dict: if not self._record_thread_status_dict[key]: break else: break time.sleep(0.1) else: qta_logger.warn('wait for record screen thread exit timeout') self._save_logcat() self._save_qt4a_log() if hasattr(self, '_logcat_debug_level_list'): for device in Device.device_list: device_name = '%s' % device.device_id if device_name not in self._logcat_debug_level_list: msg = '设备:%s中的logcat没有debug级别的日志,请检查手机日志级别设置 ' % device_name if self._check_log_called: qta_logger.error(msg) else: qta_logger.warn(msg) if hasattr(settings, 'QT4A_DEVICE_HOSTS'): for device in Device.device_list: self.log_info('恢复设备: %s hosts环境' % device.device_id) device.modify_hosts()
def screenshot(self, image_path=None): '''截屏,返回元组:截屏是否成功,图片保存路径 :param image_path: 截屏图片的存放路径 :type image_path: str :rtype: tuple (boolean, str) ''' try: base64_img = None from http.client import IncompleteRead for _ in range(3): try: base64_img = self._driver.device.capture_screen() if base64_img: break except IncompleteRead: time.sleep(2) # 等待两秒后再次尝试 continue if not image_path: image_path = os.path.join( QT4i_LOGS_PATH, "p%s_%s.png" % (os.getpid(), uuid.uuid1())) with open(os.path.abspath(image_path), "wb") as fd: if PY2: fd.write(base64.decodestring(base64_img)) else: fd.write(base64.decodebytes(base64_img.encode('utf-8'))) return os.path.isfile(image_path), image_path except: logger.error('screenshot failed: %s' % traceback.format_exc()) return False, ""
def run_test(self): url = "http://localhost/rpc" list = [0, 1, 2] res = send(url, list) if res.status_code != 200: logger.error("服务错误") dict_json = json.loads(res.content.decode()) pass
def execute(self, args): """执行过程 """ if os.path.isfile(__file__): logger.error("should run in egg mode") sys.exit(1) qtaf_path = os.path.dirname(os.path.abspath(__file__)) project.update_project_qtaf(args.proj_path, qtaf_path)
def screenshot(self, image_path=None): '''截屏,返回元组:截屏是否成功,图片保存路径 :param image_path: 截屏图片的存放路径 :type image_path: str :rtype: tuple (boolean, str) ''' try: base64_img = self._driver.device.capture_screen() if not image_path: image_path = os.path.join( QT4i_LOGS_PATH, "p%s_%s.png" % (os.getpid(), uuid.uuid1())) with open(os.path.realpath(image_path), "wb") as fd: fd.write(base64.decodestring(base64_img)) return (os.path.isfile(image_path), image_path) except: logger.error('screenshot failed: %s' % traceback.format_exc()) return (False, "")
def parse_args(self, args): """解析参数 """ if len(args) < 1: self.print_help() sys.exit(1) subcmd = args[0] for it in self.subcmd_classes: if it.name == subcmd: subcmd_class = it parser = it.parser break else: logger.error("invalid subcommand \"%s\"\n" % subcmd) sys.exit(1) ns = parser.parse_args(args[1:]) subcmd = subcmd_class() subcmd.main_parser = self return subcmd, ns