def __init__(self, outdevice=None, apppacakagename=None, isusb=None): print(outdevice) try: if outdevice is None: # self.d = u2.connect('192.168.199.168') # self.d = u2.connect_usb('192.168.1.100:5555') self.d = u2.connect() # 没有输入设备号则默认不要设备号 else: self.d = u2.connect_usb(outdevice) except Exception as e: print(e) self.install_uiaumator2_yilai() # 先安装 # 然后再运行 if outdevice is None: # self.d = u2.connect('192.168.199.168') # self.d = u2.connect_usb('192.168.1.100:5555') self.d = u2.connect() # 没有输入设备号则默认不要设备号 else: self.d = u2.connect_usb(outdevice) # if apppacakagename == None: # self.packagename = "com.ahdi.qrindo.wallet" # else: # self.packagename =apppacakagename self.timeStr = GetTimeStr() # 实例化 self.isstartapp = False
def __init__(self, outdevice=None, apppacakagename=None): if outdevice == None: # self.d = u2.connect('192.168.199.168') self.d = u2.connect_usb('810EBM32TZ4K') else: self.d = u2.connect_usb(outdevice) if apppacakagename == None: self.packagename = "com.ahdi.qrindo.wallet" else: self.packagename = apppacakagename self.timeStr = GetTimeStr() # 实例化 self.isstartapp = False
def __generateTestCases(): # forcount = 10 #获取用例重复次数 # for i in range(1, forcount): # 循环,但去掉第一 # args = [] # args.append(i) # # setattr(TestIndexClass, 'test_func_%s' % i, # TestIndexClass.getTestFunc(*args)) # 通过setattr自动为TestCase类添加成员方法,方法以“test_func_”开头 from performancestatistics.models import MeminfoTestCase meminfotestcase_all = MeminfoTestCase.objects.filter(testproject="Qrindo").filter(testpage="Balance页").order_by('id') for meminfotestcase in meminfotestcase_all: forcount = meminfotestcase.forcount starttime = GetTimeStr().getTimeStr() if len(str(meminfotestcase.id)) == 1: meminfotestcaseid = '0000%s'% meminfotestcase.id elif len(str(meminfotestcase.id)) == 2: meminfotestcaseid = '000%s' % meminfotestcase.id elif len(str(meminfotestcase.id)) == 3: meminfotestcaseid = '00%s' % meminfotestcase.id elif len(str(meminfotestcase.id)) == 4: meminfotestcaseid = '0%s' % meminfotestcase.id elif len(str(meminfotestcase.id)) == 5: meminfotestcaseid = '%s' % meminfotestcase.id else: meminfotestcaseid ='Id已经超过5位数,请重新定义' for i in range(1,forcount+1): # 循环,从1开始 if len(str(i)) == 1: forcount_i = '0000%s' % i elif len(str(i)) == 2: forcount_i = '000%s' % i elif len(str(i)) == 3: forcount_i = '00%s' % i elif len(str(i)) == 4: forcount_i = '0%s' % i elif len(str(i)) == 5: forcount_i = '%s' % i else: forcount_i = 'Id已经超过5位数,请重新定义' args = [] args.append(i) args.append(meminfotestcase.currentpagetext) args.append(meminfotestcase.currrentfindstyle) args.append(meminfotestcase.currentstyleparame) args.append(meminfotestcase.nextpagetext) args.append(meminfotestcase.testproject) args.append(meminfotestcase.testmodule) args.append(meminfotestcase.testpage) args.append(meminfotestcase.testcasetitle) args.append(starttime) args.append(meminfotestcase.dependcase_id) setattr(TestBalanceClass, 'test_func_%s_%s_%s' % (meminfotestcaseid,meminfotestcase.testcasetitle,forcount_i), TestBalanceClass.getTestFunc(*args)) # 通过setattr自动为TestCase类添加成员方法,方法以“test_func_”开头
def runAllTest(self, testproject=None, testmodule=None): if testproject == None: testproject = u"测试项目" else: testproject = testproject if testmodule == None: testmodule = u"测试模块" else: testmodule = testmodule #将用例组件成数组 alltestnames = caselist() suite = unittest.TestSuite() for testpy in alltestnames: try: suite.addTest( unittest.defaultTestLoader.loadTestsFromName( testpy)) #默认加载所有用例 except Exception: print('ERROR: Skipping tests from "%s".' % testpy) try: __import__(test) except ImportError: print('Could not import the "%s" test module.' % testpy) else: print('Could not load the "%s" test suite.' % testpy) from traceback import print_exc print_exc() self.outPutMyLog('Running the tests...') # print('Running the tests...') gettime = GetTimeStr() reporttimestr = gettime.getTimeStr() #获取当前时间 currentny = gettime.getTimeStrNY() #获取当前时间的年月 reportdir = "%s/media/report/%s" % (str( os.path.dirname( os.path.dirname(os.path.dirname( os.path.abspath(__file__))))), currentny) gettime.createdir(reportdir) #创建报告目录 reportname = "report/%s/%s_report.html" % (currentny, reporttimestr) filename = '%s/media/%s' % (str( os.path.dirname( os.path.dirname(os.path.dirname( os.path.abspath(__file__))))), reportname) fp = open(filename, 'wb') self.outPutMyLog('The report path:%s' % filename) # 定义测试报告 runner = HTMLTestRunner.HTMLTestRunner( stream=fp, title=u'%s_%s 自动化测试_测试报告' % (testproject, testmodule), description=u'用例执行情况:', verbosity=2) #verbosity=2,输出测试用例中打印的信息 runresult = runner.run(suite) fp.close() #保存报告到数据库 from reportrecord.models import Report reportrd = Report() # 数据库的对象等于Report,实例化 reportrd.testproject = testproject reportrd.testmodule = testmodule reportrd.reportname = reporttimestr print(reportname) reportrd.reportfile = reportname reportrd.save() # 发送report至邮箱 emailtitle = u'%s_%s 自动化测试_测试报告' % (testproject, testmodule) send_e = SendEmail() send_e.send_main_result_num(runresult.success_count, runresult.failure_count, runresult.error_count, filename, emailtitle=emailtitle)
class BaseFrame: def __init__(self, outdevice=None, apppacakagename=None): if outdevice == None: # self.d = u2.connect('192.168.199.168') self.d = u2.connect_usb('810EBM32TZ4K') else: self.d = u2.connect_usb(outdevice) if apppacakagename == None: self.packagename = "com.ahdi.qrindo.wallet" else: self.packagename = apppacakagename self.timeStr = GetTimeStr() # 实例化 self.isstartapp = False def outPutMyLog(self, context): mylog = MyLog(context) mylog.runMyLog() def outPutErrorMyLog(self, context): mylog = MyLog(context) mylog.runErrorLog() #执行shell命令 def adbshell(self, order): d = self.d d.adb_shell(order) self.outPutMyLog('输入shell命令:', order, '。\n') #启动app def startapp(self): self.stopapp() self.d.app_start(self.packagename) self.outPutMyLog('启动包名为[%s]的应用---------' % self.packagename) # self.outPutMyLog('设备信息:', d.device_info) self.delaytime(3) #关闭app def stopapp(self): self.d.app_stop(self.packagename) self.outPutMyLog('关闭包名为[%s]的应用---------' % self.packagename) #延时 def delaytime(self, dalaytime): dalaytime = int(dalaytime) time.sleep(dalaytime) self.outPutMyLog('等待%d秒...' % dalaytime) #点击返回按钮 def clickback(self): self.d.press("back") self.outPutMyLog('点击返回按键') self.delaytime(3) #点击Home按钮 def clickhome(self): self.d.press("home") self.outPutMyLog('点击Home按键') self.delaytime(3) #元素是否存在 def ele_is_exist(self, findstyle, styleparame): try: if findstyle == "resourceId": ele = self.d(resourceId=styleparame) ele.info # self.outPutMyLog("ele.info:%s" % ele.info) # self.outPutMyLog("找到元素") elif findstyle == "text": ele = self.d(text=styleparame) ele.info # self.outPutMyLog("ele.info:%s"% ele.info) # self.outPutMyLog("找到元素") except Exception as e: # self.outPutMyLog("报错:%s"% e) return False else: return True #查找元素 def findelement(self, findstyle, styleparame): try: if findstyle == "resourceId": ele = self.d(resourceId=styleparame) eletext = self.geteleinfo_text(ele) if eletext == "": self.outPutMyLog("定位到resourceId为[%s]的控件。" % styleparame) else: self.outPutMyLog("定位到text为[%s]的控件。" % eletext) return ele elif findstyle == "text": ele = self.d(text=styleparame) eletext = self.geteleinfo_text(ele) if eletext == "": self.outPutMyLog("没有定位到控件。") else: self.outPutMyLog("定位到text为[%s]的控件。" % eletext) return ele except Exception as e: self.outPutMyLog("self.isstartappd的值为:%s" % self.isstartapp) self.isstartapp = True self.outPutMyLog("self.isstartappd的值为:%s" % self.isstartapp) self.getScreenshotError() self.outPutErrorMyLog("出错原因:定位控件失败.具体原因:%s" % e) self.delaytime(3) self.isstartapp = True self.outPutMyLog("self.isstartappd的值为:%s" % self.isstartapp) #查找到元素并且输入内容 def findelement_and_input(self, findstyle, styleparame, inputtext): ele = self.findelement(findstyle, styleparame) self.ele_input(ele, inputtext) # 查找到元素并且点击该元素 def findelement_and_click(self, findstyle, styleparame, outpretoastmessage=None): ele = self.findelement(findstyle, styleparame) toastmessage = self.ele_click_and_return_toastmessage( ele, outpretoastmessage) return toastmessage # 查找到元素并且返回enabled属性的状态 def findelement_and_return_enabledstatus(self, findstyle, styleparame): ele = self.findelement(findstyle, styleparame) eleinfo_enabled = self.geteleinfo_enabled(ele) return eleinfo_enabled # 查找到元素并且text内容 def findelement_and_return_text(self, findstyle, styleparame): ele = self.findelement(findstyle, styleparame) eleinfo_text = self.geteleinfo_text(ele) return eleinfo_text # 查找到元素并且返回selected属性状态 def findelement_and_return_selectedstatus(self, findstyle, styleparame): ele = self.findelement(findstyle, styleparame) eleinfo_selected = self.geteleinfo_selected(ele) return eleinfo_selected #通过ID查找到元素 def findbyresourceId(self, resourceId): d = self.d try: ele = d(resourceId=resourceId) eletext = self.geteleinfo_text(ele) if eletext == "": self.outPutMyLog("定位到resourceId为[%s]的控件。" % resourceId) else: self.outPutMyLog("定位到text为[%s]的控件。" % eletext) return ele except Exception as e: self.outPutMyLog("self.isstartappd的值为:%s" % self.isstartapp) self.isstartapp = True self.outPutMyLog("self.isstartappd的值为:%s" % self.isstartapp) self.getScreenshotError() self.outPutErrorMyLog("出错原因:定位控件失败.具体原因:%s" % e) self.delaytime(3) self.isstartapp = True self.outPutMyLog("self.isstartappd的值为:%s" % self.isstartapp) #通过ID查找到元素并且输入内容 def findbyresourceId_and_input(self, resourceId, inputtext): ele = self.findbyresourceId(resourceId) self.ele_input(ele, inputtext) # 通过ID查找到元素并且点击该元素 def findbyresourceId_and_click(self, resourceId, outpretoastmessage=None): ele = self.findbyresourceId(resourceId) toastmessage = self.ele_click_and_return_toastmessage( ele, outpretoastmessage) return toastmessage # 通过ID查找到元素并且返回enabled属性的状态 def findbyresourceId_and_return_enabledstatus(self, resourceId): ele = self.findbyresourceId(resourceId) eleinfo_enabled = self.geteleinfo_enabled(ele) return eleinfo_enabled # 通过ID查找到元素并且text内容 def findbyresourceId_and_return_text(self, resourceId): ele = self.findbyresourceId(resourceId) eleinfo_text = self.geteleinfo_text(ele) return eleinfo_text # 通过ID查找到元素并且返回selected属性状态 def findbyresourceId_and_return_selectedstatus(self, resourceId): ele = self.findbyresourceId(resourceId) eleinfo_selected = self.geteleinfo_selected(ele) return eleinfo_selected #通过text查找到元素 def findbytext(self, text): d = self.d try: ele = d(text=text) eletext = self.geteleinfo_text(ele) if eletext == "": self.outPutMyLog("没有定位到控件。") else: self.outPutMyLog("定位到text为[%s]的控件。" % eletext) return ele except Exception as e: self.outPutMyLog("self.isstartappd的值为:%s" % self.isstartapp) self.isstartapp = True self.outPutMyLog("self.isstartappd的值为:%s" % self.isstartapp) self.getScreenshotError() self.outPutErrorMyLog("出错原因:定位控件失败.具体原因:%s" % e) self.delaytime(3) #通过text查找到元素并且输入内容 def findbytext_and_input(self, text, inputtext): ele = self.findbytext(text) self.ele_input(ele, inputtext) #通过text查找到元素并且点击内容 def findbytext_and_click(self, text, outpretoastmessage=None): ele = self.findbytext(text) toastmessage = self.ele_click_and_return_toastmessage( ele, outpretoastmessage) return toastmessage #通过text查找到元素并且返回enabled状态 def findbytext_and_return_enabledstatus(self, text): ele = self.findbytext(text) eleinfo_enabled = self.geteleinfo_enabled(ele) return eleinfo_enabled #通过text查找到元素并且返回enabled状态 def findbytext_and_return_text(self, text): ele = self.findbytext(text) eleinfo_text = self.geteleinfo_text(ele) return eleinfo_text #通过text查找到元素并且返回selected状态 def findbytext_and_return_selectedstatus(self, text): ele = self.findbytext(text) eleinfo_selected = self.geteleinfo_selected(ele) return eleinfo_selected #元素中输入内容 def ele_input(self, ele, inputtext): ele.clear_text() ele.send_keys(inputtext) self.outPutMyLog("输入:%s。" % inputtext) self.delaytime(1) #点击元素并返回toast提示信息 def ele_click_and_return_toastmessage(self, ele, outpretoastmessage=None): ele.click() self.outPutMyLog("点击该控件。") if outpretoastmessage == None: self.delaytime(3) return None else: toastmessage = self.getToast() self.outPutMyLog("pretoastmessage:", outpretoastmessage) return toastmessage #得到元素enabled属性值 def geteleinfo_enabled(self, ele): value = 'enabled' eleinfo_enabled = self.geteleinfo_value(ele, value) self.outPutMyLog('该控件的enabled属性的值为:%s' % eleinfo_enabled) return eleinfo_enabled #得到元素text属性值 def geteleinfo_text(self, ele): value = 'text' eleinfo_text = self.geteleinfo_value(ele, value) if eleinfo_text != '': self.outPutMyLog('该控件的text属性的值为:%s' % eleinfo_text) return eleinfo_text #得到元素selected属性值 def geteleinfo_selected(self, ele): value = 'selected' eleinfo_selected = self.geteleinfo_value(ele, value) if eleinfo_selected != '': self.outPutMyLog('该控件的selected属性的值为:%s' % eleinfo_selected) return eleinfo_selected #得到元素属性值 def geteleinfo_value(self, ele, value): eleinfo = ele.info # self.outPutMyLog('eleinfo:',eleinfo) eleinfo_value = eleinfo[value] return eleinfo_value #得到toast提示信息 def getToast(self): toastmessage = self.d.toast.get_message(5.0, default="") self.outPutMyLog("toastmessage:", toastmessage) return toastmessage #得到设备信息 def getdeviceinfo(self): deviceinfo = self.d.device_info self.outPutMyLog("deviceinfo:%s" % deviceinfo) self.outPutMyLog("deviceinfo类型:%s" % type(deviceinfo)) return deviceinfo #建立crash监听 def createwatcher(self): d = self.d d.watcher('crash').when(text='很抱歉,“QRindo MCH”已停止运行。').click(text="确定") d.watcher("crash").triggered self.outPutMyLog('d.watcher:', d.watcher) #获取时间串 def getTimeStr(self): tStr = self.timeStr.getTimeStr() return tStr def getTimeStrNY(self): tStrNY = self.timeStr.getTimeStrNY() return tStrNY #出错时,获取页面截图 def getScreenshotError(self): d = self.d self.outPutMyLog("调用截取图片函数") currentny = self.getTimeStrNY() #获取当前时间的年月 tStr = self.getTimeStr() # path = "../screenshots/screenpicture_%s.png"% tStr firedir = r'%s/media/report/%s/screenshots/' % (os.path.dirname( os.path.dirname(os.path.dirname( os.path.abspath(__file__)))), currentny) self.createdir(firedir) path = '%s/screenpicture_%s.png' % (firedir, tStr) pathaboutmysql = r'media\report\%s\screenshots\screenpicture_%s.png' % ( currentny, tStr) # path = '%s/screenshots/screenpicture_%s.png'%(str(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))),tStr) d.screenshot(path) self.outPutMyLog("*****") # self.outPutMyLog(path) self.outPutMyLog(pathaboutmysql) self.outPutMyLog("*****") return path #正常,获取页面截图 def getScreenshotNormal(self): d = self.d self.outPutMyLog("调用截取图片函数") tStr = self.getTimeStr() # path = "../screenshots/screenpicture_%s.png"% tStr path = '%s/screenshots/screenpicture_%s.png' % (str( os.path.dirname(os.path.dirname(os.path.abspath(__file__)))), tStr) d.screenshot(path) self.outPutMyLog("*****") self.outPutMyLog(path) self.outPutMyLog("*****") return path def createdir(self, filedir): filelist = filedir.split("/") # print(filelist) long = len(filelist) # print(long) zuhefiledir = filelist[0] for i in range(1, long): zuhefiledir = zuhefiledir + "/" + filelist[i] if os.path.exists(zuhefiledir): self.outPutMyLog("已经存在目录:%s" % zuhefiledir) else: os.mkdir(zuhefiledir) self.outPutMyLog("已经创建目录:%s" % zuhefiledir)
class BaseFrame(object): def __init__(self, outdevice=None, apppacakagename=None, isusb=None): print(outdevice) try: if outdevice is None: # self.d = u2.connect('192.168.199.168') # self.d = u2.connect_usb('192.168.1.100:5555') self.d = u2.connect() # 没有输入设备号则默认不要设备号 else: self.d = u2.connect_usb(outdevice) except Exception as e: print(e) self.install_uiaumator2_yilai() # 先安装 # 然后再运行 if outdevice is None: # self.d = u2.connect('192.168.199.168') # self.d = u2.connect_usb('192.168.1.100:5555') self.d = u2.connect() # 没有输入设备号则默认不要设备号 else: self.d = u2.connect_usb(outdevice) # if apppacakagename == None: # self.packagename = "com.ahdi.qrindo.wallet" # else: # self.packagename =apppacakagename self.timeStr = GetTimeStr() # 实例化 self.isstartapp = False # 打印日志 def outPutMyLog(self, context): mylog = MyLog(context) mylog.runMyLog() # 打印错误日志 def outPutErrorMyLog(self, context): mylog = MyLog(context) mylog.runErrorLog() # 执行shell命令 def adbshell(self, order): d = self.d d.shell(order) # self.outPutMyLog('输入shell命令:',order,'。\n') # python -m weditor def start_weditor(self): # order = "python -m uiautomator2 init" order = "python -m weditor" self.adbshell(order=order) # 执行cmd命令 def exec_order(self, orderName): check = os.popen(orderName) c = check.read() print("执行命令:%s" % orderName) print("执行命令后返回的内容:%s" % c) check.close() return c # 安装uiautomator2依赖脚本 def install_uiaumator2_yilai(self): # 获取当前文件路径 current_path = os.path.dirname(os.path.abspath(__file__)) print(current_path) # 安装app-uiautomator.apk app_uiautomator_apk_path = str( current_path) + "\\" + "uiautomator2yilaibao" + "\\" + "app-uiautomator.apk" print(app_uiautomator_apk_path) install_app_uiautomator_apk_order = "adb install %s" % str( app_uiautomator_apk_path) self.exec_order(orderName=install_app_uiautomator_apk_order) self.delaytime(5) # 安装app-uiautomator-test.apk app_uiautomator_test_apk_path = str( current_path) + "\\" + "uiautomator2yilaibao" + "\\" + "app-uiautomator-test.apk" install_app_uiautomator_test_apk_order = "adb install %s" % str( app_uiautomator_test_apk_path) self.exec_order(orderName=install_app_uiautomator_test_apk_order) self.delaytime(5) # 将atx-agent推送到手机的/data/local/tmp/目录中 atx_agent_path = str(current_path) + "\\" + \ "uiautomator2yilaibao" + "\\" + "atx-agent" push_atx_agent_order = "adb push %s /data/local/tmp" % str( atx_agent_path) self.exec_order(orderName=push_atx_agent_order) self.delaytime(5) # 设置手机端atx-agent权限为755,支持所有用户组可读可执行 set_atx_agent_order = "adb shell chmod 755 /data/local/tmp/atx-agent" self.exec_order(orderName=set_atx_agent_order) self.delaytime(5) # 启动atx-agent并切换后台运行,默认**端口是7912 start_atx_agent_order = "adb shell /data/local/tmp/atx-agent server -d" self.exec_order(orderName=start_atx_agent_order) self.delaytime(5) # 启动app def startapp(self, packagename, activityname=None): self.stopapp(packagename=packagename) # 先关闭应用 self.d.app_start(packagename, activityname) self.outPutMyLog('启动包名为[%s]的应用---------' % packagename) # self.outPutMyLog('设备信息:', d.device_info) self.delaytime(3) # 关闭app def stopapp(self, packagename): self.d.app_stop(packagename) self.outPutMyLog('关闭包名为[%s]的应用---------' % packagename) # 延时 def delaytime(self, dalaytime): dalaytime = int(dalaytime) time.sleep(dalaytime) self.outPutMyLog('等待%d秒...' % dalaytime) # 点击返回按钮 def clickback(self): self.d.press("back") self.outPutMyLog('点击返回按键') self.delaytime(3) # 点击Home按钮 def clickhome(self): self.d.press("home") self.outPutMyLog('点击Home按键') self.delaytime(3) # 元素是否存在 def ele_is_exist(self, findstyle, styleparame): try: if findstyle == "resourceId": ele = self.d(resourceId=styleparame) ele.info # self.outPutMyLog("ele.info:%s" % ele.info) # self.outPutMyLog("找到元素") elif findstyle == "text": ele = self.d(text=styleparame) ele.info # self.outPutMyLog("ele.info:%s"% ele.info) # self.outPutMyLog("找到元素") except Exception as e: # self.outPutMyLog("报错:%s"% e) return False else: return True # 查找元素 def findelement(self, findstyle, styleparame): try: if findstyle == "resourceId": ele = self.d(resourceId=styleparame) elif findstyle == "text": ele = self.d(text=styleparame) elif findstyle == "xpath": ele = self.d.xpath(styleparame) elerect = self.geteleinfo_rect_screen(ele) #调用元素属性,如果没有成功,则说明元素不存在 except Exception as e: self.outPutMyLog("self.isstartappd的值为:%s" % self.isstartapp) self.isstartapp = True self.outPutMyLog("self.isstartappd的值为:%s" % self.isstartapp) self.getScreenshotError() self.outPutErrorMyLog("出错原因:定位控件失败.具体原因:%s" % e) self.delaytime(3) self.isstartapp = True self.outPutMyLog("self.isstartappd的值为:%s" % self.isstartapp) # 查找到元素并且输入内容 def findelement_and_input(self, findstyle, styleparame, inputtext): ele = self.findelement(findstyle, styleparame) self.ele_input(ele, inputtext) # 查找到元素并且点击该元素 def findelement_and_click( self, findstyle, styleparame, outpretoastmessage=None): ele = self.findelement(findstyle, styleparame) toastmessage = self.ele_click_and_return_toastmessage( ele, outpretoastmessage) return toastmessage # 查找到元素并且返回enabled属性的状态 def findelement_and_return_enabledstatus(self, findstyle, styleparame): ele = self.findelement(findstyle, styleparame) eleinfo_enabled = self.geteleinfo_enabled(ele) return eleinfo_enabled # 查找到元素并且text内容 def findelement_and_return_text(self, findstyle, styleparame): ele = self.findelement(findstyle, styleparame) eleinfo_text = self.geteleinfo_text(ele) return eleinfo_text # 查找到元素并且返回selected属性状态 def findelement_and_return_selectedstatus(self, findstyle, styleparame): ele = self.findelement(findstyle, styleparame) eleinfo_selected = self.geteleinfo_selected(ele) return eleinfo_selected # 通过ID查找到元素 def findbyresourceId(self, resourceId): d = self.d try: ele = d(resourceId=resourceId) eletext = self.geteleinfo_text(ele) if eletext == "": self.outPutMyLog("定位到resourceId为[%s]的控件。" % resourceId) else: self.outPutMyLog("定位到text为[%s]的控件。" % eletext) return ele except Exception as e: self.outPutMyLog("self.isstartappd的值为:%s" % self.isstartapp) self.isstartapp = True self.outPutMyLog("self.isstartappd的值为:%s" % self.isstartapp) self.getScreenshotError() self.outPutErrorMyLog("出错原因:定位控件失败.具体原因:%s" % e) self.delaytime(3) self.isstartapp = True self.outPutMyLog("self.isstartappd的值为:%s" % self.isstartapp) # 通过ID查找到元素并且输入内容 def findbyresourceId_and_input(self, resourceId, inputtext): ele = self.findbyresourceId(resourceId) self.ele_input(ele, inputtext) # 通过ID查找到元素并且点击该元素 def findbyresourceId_and_click(self, resourceId, outpretoastmessage=None): ele = self.findbyresourceId(resourceId) toastmessage = self.ele_click_and_return_toastmessage( ele, outpretoastmessage) return toastmessage # 通过ID查找到元素并且返回enabled属性的状态 def findbyresourceId_and_return_enabledstatus(self, resourceId): ele = self.findbyresourceId(resourceId) eleinfo_enabled = self.geteleinfo_enabled(ele) return eleinfo_enabled # 通过ID查找到元素并且text内容 def findbyresourceId_and_return_text(self, resourceId): ele = self.findbyresourceId(resourceId) eleinfo_text = self.geteleinfo_text(ele) return eleinfo_text # 通过ID查找到元素并且返回selected属性状态 def findbyresourceId_and_return_selectedstatus(self, resourceId): ele = self.findbyresourceId(resourceId) eleinfo_selected = self.geteleinfo_selected(ele) return eleinfo_selected # 通过text查找到元素 def findbytext(self, text): d = self.d try: ele = d(text=text) eletext = self.geteleinfo_text(ele) if eletext == "": self.outPutMyLog("没有定位到控件。") else: self.outPutMyLog("定位到text为[%s]的控件。" % eletext) return ele except Exception as e: self.outPutMyLog("self.isstartappd的值为:%s" % self.isstartapp) self.isstartapp = True self.outPutMyLog("self.isstartappd的值为:%s" % self.isstartapp) self.getScreenshotError() self.outPutErrorMyLog("出错原因:定位控件失败.具体原因:%s" % e) self.delaytime(3) # 通过text查找到元素并且输入内容 def findbytext_and_input(self, text, inputtext): ele = self.findbytext(text) self.ele_input(ele, inputtext) # 通过text查找到元素并且点击内容 def findbytext_and_click(self, text, outpretoastmessage=None): ele = self.findbytext(text) toastmessage = self.ele_click_and_return_toastmessage( ele, outpretoastmessage) return toastmessage # 通过text查找到元素并且返回enabled状态 def findbytext_and_return_enabledstatus(self, text): ele = self.findbytext(text) eleinfo_enabled = self.geteleinfo_enabled(ele) return eleinfo_enabled # 通过text查找到元素并且返回enabled状态 def findbytext_and_return_text(self, text): ele = self.findbytext(text) eleinfo_text = self.geteleinfo_text(ele) return eleinfo_text # 通过text查找到元素并且返回selected状态 def findbytext_and_return_selectedstatus(self, text): ele = self.findbytext(text) eleinfo_selected = self.geteleinfo_selected(ele) return eleinfo_selected # 元素中输入内容 def ele_input(self, ele, inputtext): ele.clear_text() ele.send_keys(inputtext) self.outPutMyLog("输入:%s。" % inputtext) self.delaytime(1) # 点击元素并返回toast提示信息 def ele_click_and_return_toastmessage(self, ele, outpretoastmessage=None): ele.click() self.outPutMyLog("点击该控件。") if outpretoastmessage is None: self.delaytime(3) return None else: toastmessage = self.getToast() self.outPutMyLog("pretoastmessage:", outpretoastmessage) return toastmessage # 得到元素enabled属性值 def geteleinfo_enabled(self, ele): value = 'enabled' eleinfo_enabled = self.geteleinfo_value(ele, value) self.outPutMyLog('该控件的enabled属性的值为:%s' % eleinfo_enabled) return eleinfo_enabled # 得到元素text属性值 def geteleinfo_text(self, ele): value = 'text' eleinfo_text = self.geteleinfo_value(ele, value) if eleinfo_text != '': self.outPutMyLog('该控件的text属性的值为:%s' % eleinfo_text) return eleinfo_text # 得到元素selected属性值 def geteleinfo_selected(self, ele): value = 'selected' eleinfo_selected = self.geteleinfo_value(ele, value) if eleinfo_selected != '': self.outPutMyLog('该控件的selected属性的值为:%s' % eleinfo_selected) return eleinfo_selected # 得到元素rect属性值 def geteleinfo_rect(self, ele): value = 'bounds' eleinfo_bounds = self.geteleinfo_value(ele, value) self.outPutMyLog('该控件的bounds属性的值为:%s' % str(eleinfo_bounds)) eleinfo_rect = {} rect_x = eleinfo_bounds["left"] rect_y = eleinfo_bounds["top"] rect_width = int(eleinfo_bounds["right"])-int(rect_x) rect_height = int(eleinfo_bounds["bottom"])-int(rect_y) # {"x": 692, "y": 838, "width": 216, "height": 278} eleinfo_rect["x"] = rect_x eleinfo_rect["y"] = rect_y eleinfo_rect["width"] = rect_width eleinfo_rect["height"] = rect_height self.outPutMyLog('bounds转为rect的值为:' ) self.outPutMyLog(eleinfo_rect) return eleinfo_rect def get_zhidingquyu_image(self,rect_x,rect_y,rect_width,rect_height): #页面全图 screen_path = self.getScreenshotNormal() coderange = (int(rect_x),int(rect_y),int(int(rect_x)+int(rect_width)),int(int(rect_y)+int(rect_height))) #写成我们需要截取的位置坐标 pageScreenshot = Image.open(screen_path) #打开截图 imageScreen = pageScreenshot.crop(coderange) #使用Image的crop函数,从截图中再次截取我们需要的区域,即验证码区域 currentny = self.getTimeStrNY() # 获取当前时间的年月 firedir = r'%s/media/report/%s/screenshots/' % (os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))),currentny) self.createdir(filedir=firedir) tStr = self.getTimeStr() path = '%s/eleimage_%s.png' % (firedir,tStr) imageScreen.save(path) #保存区域图片 return path #获取到元素控件的截图 def geteleinfo_rect_screen(self, ele): eleinfo_rect_dict = self.geteleinfo_rect(ele=ele) rect_x = eleinfo_rect_dict["x"] rect_y = eleinfo_rect_dict["y"] rect_width = eleinfo_rect_dict["width"] rect_height = eleinfo_rect_dict["height"] ele_image_path = self.get_zhidingquyu_image(rect_x,rect_y,rect_width,rect_height) print("获取到元素的截图:") print(ele_image_path) return ele_image_path # 得到元素属性值 def geteleinfo_value(self, ele, value): eleinfo = ele.info print("元素的信息:") print(eleinfo) # self.outPutMyLog('eleinfo:',eleinfo) eleinfo_value = eleinfo[value] return eleinfo_value # 得到toast提示信息 def getToast(self): toastmessage = self.d.toast.get_message(5.0, default="") self.outPutMyLog("toastmessage:", toastmessage) return toastmessage # 得到设备信息 def getdeviceinfo(self): deviceinfo = self.d.device_info self.outPutMyLog("deviceinfo:%s" % deviceinfo) self.outPutMyLog("deviceinfo类型:%s" % type(deviceinfo)) return deviceinfo # 建立crash监听 def createwatcher(self): d = self.d d.watcher('crash').when(text='很抱歉,“QRindo MCH”已停止运行。').click(text="确定") d.watcher("crash").triggered self.outPutMyLog('d.watcher:', d.watcher) # 获取时间串 def getTimeStr(self): tStr = self.timeStr.getTimeStr() return tStr def getTimeStrNY(self): tStrNY = self.timeStr.getTimeStrNY() return tStrNY # 出错时,获取页面截图 def getScreenshotError(self): d = self.d self.outPutMyLog("调用截取图片函数") currentny = self.getTimeStrNY() # 获取当前时间的年月 tStr = self.getTimeStr() # path = "../screenshots/screenpicture_%s.png"% tStr firedir = r'%s/media/report/%s/screenshots/' % (os.path.dirname( os.path.dirname(os.path.dirname(os.path.abspath(__file__)))), currentny) self.createdir(filedir=firedir) path = '%s/screenpicture_%s.png' % (firedir, tStr) pathaboutmysql = r'media\report\%s\screenshots\screenpicture_%s.png' % ( currentny, tStr) # path = '%s/screenshots/screenpicture_%s.png'%(str(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))),tStr) d.screenshot(path) self.outPutMyLog("*****") # self.outPutMyLog(path) self.outPutMyLog(pathaboutmysql) self.outPutMyLog("*****") return path # 正常,获取页面截图 def getScreenshotNormal(self): d = self.d self.outPutMyLog("调用截取图片函数") tStr = self.getTimeStr() filedir = "%s/screenshots/" % str(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) self.createdir(filedir=filedir) path = '%s/screenpicture_%s.png' % (filedir, tStr) d.screenshot(path) self.outPutMyLog("*****") self.outPutMyLog(path) self.outPutMyLog("*****") return path # 自动创建目录 def createdir(self, filedir): filelist = filedir.split("/") # print(filelist) long = len(filelist) # print(long) zuhefiledir = filelist[0] for i in range(1, long): zuhefiledir = zuhefiledir + "/" + filelist[i] if os.path.exists(zuhefiledir): self.outPutMyLog("已经存在目录:%s" % zuhefiledir) else: os.mkdir(zuhefiledir) self.outPutMyLog("已经创建目录:%s" % zuhefiledir)