def installApp(self, app_path=None, app_dir=None): """ APK在PC端 :arg app_path: app的完整路径 app_dir:app所在的目录 Usage: InstallApp(System().data_path) or InstallApp("G:\workspace\kooTest\Data\test.apk") """ if app_dir: for app in System().listFile(app_dir): if app.endswith(".apk"): app_path = os.path.join(app_dir, app) break elif os.path.isfile(app_path): app_path = app_path.strip() if app_path: try: Command().adb('install -r {}'.format(app_path)).wait() except Exception as e: mylog.warn("%s 安装失败:%s" % (os.path.basename(app_path), e)) else: mylog.info("%s 安装成功.." % os.path.basename(app_path)) else: pass
def screenShot(self, name): """ name:图片名称 """ time.sleep(String.WAIT_TIME) mylog.info("当前截图存储路径:%s" % self.__savePicture(name)) return self.driver.get_screenshot_as_file(self.__savePicture(name))
def sysInfo(self): sdk = self.sdk mylog.info("Android SDK版本:%s" % sdk) release = Command().shell(String.RELEASE).stdout.read().decode( String.UTF8).strip() mylog.info("Android 版本:%s" % release) model = Command().shell(String.MODEL).stdout.read().decode( String.UTF8).strip() mylog.info("Android 产品型号:%s" % model) brand = Command().shell(String.BRAND).stdout.read().decode( String.UTF8).strip() mylog.info("Android 产品品牌:%s" % brand) return sdk, release, model, brand
def sendEmail(html_path): conf_info = configure() html = open(html_path, "rb") #SMTP发送邮件 msg = MIMEText(html.read(), String.HTML, String.UTF8) html.close() msg[String.FROM] = _format_addr("new <%s>" % conf_info[String.SENDER]) msg[String.TO] = _format_addr("<%s>" % conf_info[String.RECEIVER]) msg[String.SUBJECT] = Header(String.TITLE, String.UTF8).encode() server = smtplib.SMTP(conf_info[String.SMTPSERVER], 25) try: server.login(conf_info[String.USERNAME], conf_info[String.PASSWORD]) server.sendmail(conf_info[String.SENDER], [conf_info[String.RECEIVER]], msg.as_string()) except Exception as e: mylog.error("Send_mail Error : %s " % e) else: mylog.info("Send_mail Success...") finally: server.quit()
def excelRead(self,table_sheet): # table_sheet 表格sheet的数值,如0,1,2 file_path = self.__excelPath() if not os.path.exists(file_path): mylog.error("目录" + self.__path + "下不存在用例文件") # read excel else: mylog.info("测试用例路径:" + file_path) __xls = xlrd.open_workbook(file_path) # 按表格名称,返回当前sheet,每个sheet只编写某个模块用例 __table = __xls.sheet_by_index(table_sheet) __rows = __table.nrows #行 __cols = __table.ncols #列 __colnames = __table.row_values(0) #以列表形式返回第一行的所有值 #mylog.info(__colnames) #将每一行数据以字典返回,将每一行字段组成的字典添加生成一个list data = [] for r in range(1,__rows): xls_row = {} for c in range(__cols): __cell_value = __table.cell(r,c).value # 判断表格值是否为空 if __cell_value == "": __cell = [] for __r in range(1,r): if __table.cell(__r,c).value != "": __cell.append(__table.cell(__r,c).value) if len(__cell) == 0: __cell_value = "" else: # 表格值为空,使用该列中最后显示的数值 __cell_value = __cell[-1] xls_row.setdefault(__colnames[c],__cell_value) # 判断定位方式是否小写 if xls_row[String.POSITION_METHOD].isupper(): xls_row[String.POSITION_METHOD] = xls_row[String.POSITION_METHOD].lower() data.append(xls_row) return data
def checkEnv(self): __version = subprocess.check_output(String.PYTHON_VERSION).decode( String.UTF8).strip() mylog.info("当前Python 版本:%s" % __version) __versionNum = __version.split(String.PYTHON)[1].strip() if __versionNum.split(".")[0] == "2": mylog.info("当前Python 版本:%s\n请安装Python 3.x版本..." % __version) os.system(String.PAUSE) sys.exit(-1) else: devicelist = self.deviceList() if len(devicelist) == 1: mylog.info("ADB当前连接设备:%s" % (devicelist)) else: mylog.info("ADB当前连接共 %s 个设备:%s" % (len(devicelist), devicelist))
def appInfo(self): if os.name == String.WINDOWS: __find = String.FIND else: __find = String.GREP # 返回第一行app路径 __app = Command().shell("dumpsys activity activities | %s baseDir" % __find).stdout.readlines()[0].decode( String.UTF8).strip() mylog.info("当前APK文件在Android中路径:%s" % (__app.split("=")[1])) # 拉取到本地的app相对路径 Command().adb( "pull {0} {1}".format(__app.split("=")[1], self.tempFile), "-s") __appPath = self.tempFile + "\\" + __app.split("/")[-1] mylog.info("APK 文件相对路径:%s" % __appPath) try: __zipFile = zipfile.ZipFile(__appPath) __data = __zipFile.read(__zipFile.namelist()[0]) except Exception as e: mylog.warn("zipfile Error:%s" % e) return False # 从压缩包里解压缩出AndroidManifest.xml else: # 将解压出的AndroidManifest.xml文件保存到本地,二进制的xml文件 mylog.info("解压后AndroidManifest.xml文件路径:%s" % (self.tempFile + "\\AndroidManifest.xml")) with open(self.tempFile + "\\AndroidManifest.xml", 'wb') as xml: xml.write(__data) __apkInfo = AXMLParser.APK(__data) packageName = __apkInfo.packageName mylog.info("当前APK包名:%s" % packageName) name = __apkInfo.versionName mylog.info("当前APK版本:%s" % name) # 得到app的文件大小 size = floor(os.path.getsize(__appPath) / (1024 * 1000)) mylog.info("APK 文件大小:%s M" % str(size)) # apk大小,版本名称 return packageName, size, name #DeviceInfo().sysInfo()
def pressKeycode(self, key): mylog.info("按键事件:keycode %s" % key) return self.driver.press_keycode(key)