操作蓝牙开关

安装

pip install airtest
pip install opencv-python

复制

import HTMLTestRunner
import unittest
import time
from airtest.core.api import *
from airtest.report.report import simple_report
import os

# 初始化
auto_setup(__file__)

# 连接到指定设备
connect_device("Android:///10AD9T0CS80028C")
os.system("adb shell am start -a android.settings.BLUETOOTH_SETTINGS")

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_error(self):
        """ This test should be marked as error one. """
        raise ValueError

    def test_fail(self):
        """ This test should fail. """
        self.assertEqual(1, 2)

    @unittest.skip("This is a skipped test.")
    def test_skip(self):
        """ This test should be skipped. """
        pass

    def test_bluetooth_switch(self):
        try:
            # 打开蓝牙开关
            touch(Template(r"tpl1711383764391.png", record_pos=(0.375, -0.71), resolution=(1260, 2800)))
            sleep(5)  # 等待蓝牙开关操作完成

            # 检查蓝牙是否成功打开
            if exists(Template(r"tpl1711383764391.png")):
                print("蓝牙成功打开")
            else:
                raise AssertionError("蓝牙未成功打开")

            # 关闭蓝牙开关
            touch(Template(r"tpl1711383807120.png", record_pos=(0.369, -0.717), resolution=(1260, 2800)))
            sleep(2)  # 等待蓝牙开关操作完成

            # 检查蓝牙是否成功关闭
            if exists(Template(r"tpl1711383807120.png")):
                print("蓝牙成功关闭")
            else:
                raise AssertionError("蓝牙未成功关闭")
        except Exception as e:
            print("测试用例执行出现异常:", e)

def Suite():
    suite = unittest.TestSuite()
    # 添加测试用例到测试套件
    suite.addTest(TestStringMethods('test_upper'))
    suite.addTest(TestStringMethods('test_error'))
    suite.addTest(TestStringMethods('test_fail'))
    suite.addTest(TestStringMethods('test_skip'))
    suite.addTest(TestStringMethods('test_bluetooth_switch'))
    return suite

if __name__=="__main__":
    for i in range(10):
        print(f"执行第 {i+1} 次测试")
        testSuite = Suite()
        now = time.strftime("%Y-%m-%d %H_%M_%S")
        Html = '.\\' + now + f'_result_{i+1}.htm'
        fp = open(Html, 'wb')

        runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u'百度搜索测试报告', description=u'用例执行情况:')
        runner.run(testSuite)

        fp.close()

        # 生成简单测试报告
        # simple_report(__file__)

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值