appium-循环执行一条用例,失败时停止执行

著论准过秦,作赋拟子虚。这篇文章主要讲述appium-循环执行一条用例,失败时停止执行相关的知识,希望能为你提供帮助。
对于项目中一些难以复现的bug,手工操作比较耗时耗力,而且还不一定能够复现,如果知道操作步骤就可以用自动化来进行反复操作来复现该问题。
1.了解该问题的操作步骤
2.抓取日志
3.遇到问题时,抓取截图
4.复现问题后停止执行命令,并停止日志。
 
首先想到的是使用monkey,可以通过自定义monkey来模拟操作步骤,由于能力有限,不知道monkey如何断言,并且如果一直运行logcat抓取日志,日志又太多,无法准确定位,所以想到使用appium.
 
1.操作步骤,这里就不贴出了
2.抓取日志 (这里进行了封装)

class logcat:#开启adb命令 def start_logcat(self, name): data = https://www.songbingjia.com/android/time.strftime("%Y-%m-%d", time.localtime(time.time())) # path = "../../report/log/" + data dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) path = os.path.join(dir, ‘report/‘ + name)type = ‘.txt‘ now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time())) filename = ‘‘if os.path.exists(path): filename = path + "\" + now + "_" + name + type else: os.makedirs(path) filename = path + "\" + now + "_" + name + type global logcat_file logcat_file = open(filename, ‘w‘) logcmd = "adb logcat -v time" global poplog poplog = subprocess.Popen(logcmd, stdout=logcat_file, stderr=subprocess.PIPE)# 结束adb命令 def stop_logcat(self): logcat_file.close() poplog.terminate()

3. 遇到问题时截图

def take_screenShot(driver,dir, name):day = time.strftime("%Y-%m-%d", time.localtime(time.time())) fq = "../../report/" + dir# 上上级目录../上级目录./当前目录 # dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) # fq = os.path.join(dir, ‘report/log/‘ + day) # fq =os.getcwd()[:-4] +‘screenShots\‘+day根据获取的路径,然后截取路径保存到自己想存放的目录下 tm = time.strftime("%Y-%m-%d_%H_%M_%S", time.localtime(time.time())) type = ‘.png‘ filename = "" if os.path.exists(fq): filename = fq + "\" + tm + "_" + name + type else: os.makedirs(fq) filename = fq + "\" + tm + "_" + name + type # c = os.getcwd() # r"\".join(c.split("\"))#此2行注销实现的功能为将路径中的替换为\ driver.get_screenshot_as_file(filename)print(name + "图片保存成功")

  4.循环该条用例
def choose_case(self): for i in range(1, 10000): ClearAllTestReport()# 可能执行次数过多,这里每次都会清空日志 print(u‘第‘, i, u‘执行用例‘) testunit = unittest.TestSuite() testunit.addTest(multi_run("test_cash")) runner = unittest.TextTestRunner() result = runner.run(testunit)#得到执行的结果 if not result.wasSuccessful():# 如果执行结果为fail,停止执行 break

 
项目实战
class multi_run(unittest.TestCase):@classmethod def setUp(cls): configure = driver_configure.driver_configure() cls.driver = configure.get_driver() cls.assert_result = assert_result() cls.logcat = logcat()def test_cash(self): ‘‘‘ 现金消费输入1 ‘‘‘name = "" dir = u"未返回给第三方" try:self.logcat.start_logcat(dir) # self.main = main_activity.main_activity(self.driver) # self.main.input_one() # time.sleep(1) # ‘‘‘ # 注意调用方式: 1. self.assert_result.assert_equals(*****)assert_equals方法中的self不用传 #2. assert_result().assert_equals()方法中的self不用传 #3. assert_result.assert_equals()方法的self参数需要传入 # ‘‘‘ name = u"银行卡"# 添加name变量 在每一次断言 # self.main.click_card() self.out_call = out_call(self.driver) self.lack_paper = lack_paper(self.driver) self.cash = cash_second(self.driver) self.out_call.click_consume()# self.out_call.long_touch(self.driver) self.order = self.out_call.get_order_no()self.out_call.click_card() gesture_manipulation().swipe_up(self.driver) self.out_call.click_btn_consume() time.sleep(2)# self.lack_paper.click_know() # self.lack_paper.click_know2()self.cash.click_complete() self.content = self.out_call.result_content() time.sleep(2) self.assertIsNotNone(self.content) # self.assertIsNone(self.content) self.out_call.back_activity() self.out_call.click_refund() time.sleep(1) self.out_call.delete_out_trade_no(self.driver) time.sleep(1) self.out_call.input_order_no(self.order) time.sleep(1) self.driver.tap([(644, 717), (685, 749)], 500) self.out_call.click_refund1() self.out_call.click_btn_finish()self.logcat.stop_logcat()# 没有发生异常也要关闭日志 # todo---------------# 删除不需要的日志,也就是正常的日志 except AssertionError as e: self.logcat.stop_logcat()# 发生异常时关闭日志 take_screenShot.take_screenShot(self.driver, dir, name) # self.out_call.kill_AppiumS()@classmethod def tearDownClass(cls): cls.driver.quit()class for_case:def choose_case(self): for i in range(1, 10000): ClearAllTestReport() print(u‘第‘, i, u‘执行用例‘) testunit = unittest.TestSuite() testunit.addTest(multi_run("test_cash")) runner = unittest.TextTestRunner() result = runner.run(testunit) if not result.wasSuccessful(): breakif __name__ == "__main__": for_case().choose_case()

【appium-循环执行一条用例,失败时停止执行】 

    推荐阅读