定时执行APP自动化测试(python3+uiautomator2)QQ群交流 822659419

定时执行APP自动化测试(python3+uiautomator2)QQ群交流 822659419


定时执行APP自动化测试(python3+uiautomator2)QQ群交流 822659419
定时执行app(在10:35到11:35内执行,10:35开始执行,加break,执行一次,不加break循环执行到11:35截止)

# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/01/21 16:34
# @Author  : huangpeng
# @Site    :
# @File    : startRun.py
# @Software: PyCharm
import unittest
import time
import sys
import uiautomator2  as u2
import HTMLTestRunner    #python3
from time import sleep

d = u2.connect()
#d = u2.connect('192.168.1.173')

default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)
# 统一使用utf-8格式

class Test(unittest.TestCase):
    def setUp(self):
        self.watcherson()
        print('d.watchers')
        print  ("----------SetUp -----\n")
    def tearDown(self):
        d.watchers.remove()
        print  ("-----------TearDown----\n")
    def watcherson(self):
        d.watcher(u'注意').when(text=u'你在此次选择后可以到“手机管理 - 权限管理”中修改此项设置').click(text=u'允许', className='android.widget.Button',resourceId='android:id/button1')
        d.watcher(u'无响应').when(text=u'关闭应用').click(text=u'关闭应用', className='android.widget.Button')
        d.watchers.run()
        pass

    def test_kspz(self):
    	time1 = 103500  # 起始时间
		time2 = 113500  # 终止时间
		

        while True:
        now2 = time.strftime("%H%M%S")

            if int(time2) > int(now2) > int(time1): #定时执行app(在10:35到11:35内执行,10:35开始执行,加break,执行一次,不加break循环执行到11:35截止)
                d.app_start("com.hl.train")
                time.sleep(3)
                d(text=u'快速拍照').click()
                time.sleep(2)
                print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), '开始执行循环拍照')
                for count in list(range(1, 3)):  # 拍count-1组
                    for test in list(range(1, 3)):  # 拍test-1张照片,保存一次为一组
                        print('循环拍照' + (time.strftime('%H%M%S')) + ':第', count, '组', ',', '第', test, '张')
                        d(text="拍照").click()
                        time.sleep(1)
                    d(text="保存").click()
                    time.sleep(1)

                d.app_stop("com.hl.train")
                time.sleep(1)

                print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), '拍照总数', test * count, '次')
                break   #(加break,执行一次,不加break循环执行到11:35截止)



if __name__ == '__main__':
    suite = unittest.TestSuite()
    suite.addTest(Test('test_kspz'))


    now=time.strftime("%Y-%m-%d_%H-%M-%S",time.localtime())
    filename = "_ReportCase.html"
    print('run version {}'.format(sys.version))
    fp =open(now+filename,'wb')
    runner = HTMLTestRunner.HTMLTestRunner(stream=fp,title=u"自动化测试报告",description=u"TestCase Report")
    runner.run(suite)

    fp.close()

或者,执行倒计时

            t1 = datetime.datetime.strptime(time.strftime("%H:%M:%S"),"%H:%M:%S")
            t2 = datetime.datetime.strptime("23:59:59", "%H:%M:%S")
            interval_time = (t2 - t1).seconds  # 输入的结果N秒
            for count in range(interval_time):
                remaining=interval_time-count
                logger.info("需要等到明天再次运行,剩余"+str(remaining)+"秒")
                time.sleep(1)
            time.sleep(2)

你可能感兴趣的:(定时执行APP自动化测试(python3+uiautomator2)QQ群交流 822659419)