通过自动化测试脚本测试iQOO Neo 8 Pro手机热点开关

更新时间202404100346

# -*- encoding=utf8 -*-
__author__ = "Tmi2szh"

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

# 初始化Airtest
auto_setup(__file__, logdir=True)

# 定义一个全局变量来存储测试结果
test_results = []

# 最大重试次数和重试间隔(秒)
MAX_RETRY = 5
RETRY_INTERVAL = 5

class MyTestCase(unittest.TestCase):
 
    def setUp(self):
        # 初始化ADB连接状态
        self.adb_connected = False
 
    # 检查ADB连接状态并重试,直到连接成功或达到最大重试次数
    def check_adb_connection(self):
        retry_count = 0
        while not self.adb_connected and retry_count < MAX_RETRY:
            try:
                connect_device("Android:///172.20.116.2:46279")
                self.adb_connected = True
                print("ADB连接成功")
            except Exception as e:
                print(f"ADB连接失败,正在重试... ({retry_count+1}/{MAX_RETRY})")
                time.sleep(RETRY_INTERVAL)
                retry_count += 1
 
        if not self.adb_connected:
            print("ADB连接失败,无法执行测试用例!")
            self.fail("ADB连接失败,无法执行测试用例!")
 
    # Airtest测试用例1,操作蓝牙开关
    def test_airtest_case1(self):
        self.check_adb_connection()
        if not self.adb_connected:
            self.skipTest("ADB连接失败")
 
        try:
            # 在这里写入具体的测试步骤,使用Airtest进行操作
            os.system("adb shell input keyevent 3")
            sleep(2)
            os.system("adb shell am start -n com.android.car.settings/.common.CarSettingActivities\$BluetoothSettingsActivity")
            # 假设有一些要测试的操作
            if exists(Template(r"t28_bt_switch_off.png", threshold=0.8, resolution=(1260, 2800))):
                touch(Template(r"t28_bt_switch_off.png", threshold=0.8, resolution=(1260, 2800)))
                sleep(1)
                if exists(Template(r"t28_bt_switch_on.png")):
                    test_results.append(("恭喜!成功关闭蓝牙开关!", "通过"))
                else:
                    test_results.append(("抱歉!无法关闭蓝牙开关!", "失败"))
            else:
                touch(Template(r"t28_bt_switch_on.png", threshold=0.8, resolution=(1260, 2800)))
                sleep(1)
                if exists(Template(r"t28_bt_switch_off.png")):
                    test_results.append(("恭喜!成功打开蓝牙开关!", "通过"))
                else:
                    test_results.append(("抱歉!无法打开蓝牙开关!", "失败"))
        except Exception as e:
            print(f"测试用例1发生异常:{str(e)}")
        os.system("adb kill-server")
        os.system("adb start-server")
 
    # Airtest测试用例2,操作WiFi开关
    def test_airtest_case2(self):
        self.check_adb_connection()
        if not self.adb_connected:
            self.skipTest("ADB连接失败")
 
        try:
            # 在这里写入具体的测试步骤,使用Airtest进行操作
            os.system("adb shell input keyevent 3")
            sleep(2)
            os.system("adb shell am start -a android.intent.action.MAIN -n com.android.car.settings/com.android.car.settings.common.CarSettingActivities\$WifiSettingsActivity")
            # 假设有一些要测试的操作
            if exists(Template(r"t28_wifi_switch_off.png", threshold=0.8, resolution=(1260, 2800))):
                touch(Template(r"t28_bt_switch_off.png", threshold=0.8, resolution=(1260, 2800)))
                sleep(1)
                if exists(Template(r"t28_wifi_switch_on.png")):
                    test_results.append(("恭喜!成功关闭WiFi开关!", "通过"))
                else:
                    test_results.append(("抱歉!无法关闭WiFi开关!", "失败"))
            else:
                touch(Template(r"t28_wifi_switch_on.png", threshold=0.8, resolution=(1260, 2800)))
                sleep(1)
                if exists(Template(r"t28_wifi_switch_off.png")):
                    test_results.append(("恭喜!成功打开WiFi开关!", "通过"))
                else:
                    test_results.append(("恭喜!成功打开WiFi开关!", "失败"))
        except Exception as e:
            print(f"测试用例2发生异常:{str(e)}")
        os.system("adb kill-server")
        os.system("adb start-server")
 
    # Airtest测试用例3,操作热点开关
    def test_airtest_case3(self):
        self.check_adb_connection()
        if not self.adb_connected:
            self.skipTest("ADB连接失败")
 
        try:
            # 在这里写入具体的测试步骤,使用Airtest进行操作
            os.system("adb shell input keyevent 3")
            sleep(2)
            os.system("adb shell am start -n com.android.settings/com.android.settings.Settings\$VivoTetherSettingsActivity")
            # 假设有一些要测试的操作
            if exists(Template(r"t28_hostpot_switch_off.png", threshold=0.8, record_pos=(1132,547), resolution=(1260, 2800))):
                touch(Template(r"t28_hostpot_switch_off.png", threshold=0.8, record_pos=(1132,547), resolution=(1260, 2800)))
                sleep(1)
                if exists(Template(r"t28_hostpot_switch_on.png")):
                    test_results.append(("恭喜!成功关闭热点开关!", "通过"))
                else:
                    test_results.append(("抱歉!无法关闭热点开关!", "失败"))
            else:
                touch(Template(r"t28_hostpot_switch_on.png", threshold=0.8, record_pos=(1132,547), resolution=(1260, 2800)))
                sleep(1)
                if exists(Template(r"t28_hostpot_switch_off.png")):
                    test_results.append(("恭喜!成功打开热点开关!", "通过"))
                else:
                    test_results.append(("抱歉!无法打开热点开关!", "失败"))
        except Exception as e:
            print(f"测试用例3发生异常:{str(e)}")
        os.system("adb kill-server")
        os.system("adb start-server")

# 使用 suite() 函数创建测试套件
def suite():
    suite = unittest.TestSuite()
    # suite.addTest(MyTestCase('test_airtest_case1'))
    # suite.addTest(MyTestCase('test_airtest_case2'))
    suite.addTest(MyTestCase('test_airtest_case3'))
    return suite

if __name__ == '__main__':
    # 运行测试用例
    runner = unittest.TextTestRunner()
    result = runner.run(suite())
    # 生成测试报告,保存在当前文件夹下并以时间命名
    current_time = time.strftime("%Y%m%d%H%M%S", time.localtime())
    current_directory = os.path.dirname(__file__)
    report_path = simple_report(__file__, logpath=True, output=os.path.join(current_directory, f'report_{current_time}.html'))
    print(f"测试报告已生成:{report_path}")

更新时间20240410

# -*- encoding=utf8 -*-
__author__ = "Tmi2szh"

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

# Initialize Airtest
auto_setup(__file__,logdir=True)

# Define a global variable to store test results
test_results = []

# Maximum number of retries and retry interval (seconds)
MAX_RETRY = 5
RETRY_INTERVAL = 5

class MyTestCase(unittest.TestCase):
 
    def setUp(self):
        # Initialize ADB connection status
        self.adb_connected = False
 
    # Check ADB connection status and retry until connected or reach maximum retry count
    def check_adb_connection(self):
        retry_count = 0
        while not self.adb_connected and retry_count < MAX_RETRY:
            try:
                connect_device("Android:///172.20.116.2:46279")
                self.adb_connected = True
                print("ADB connection successful")
            except Exception as e:
                print(f"ADB connection failed, retrying... ({retry_count+1}/{MAX_RETRY})")
                time.sleep(RETRY_INTERVAL)
                retry_count += 1
 
        if not self.adb_connected:
            print("ADB connection failed, unable to execute test cases!")
            self.fail("ADB connection failed, unable to execute test cases!")
 
    # Airtest test case 1, operate Bluetooth switch
    def test_airtest_case1(self):
        self.check_adb_connection()
        if not self.adb_connected:
            self.skipTest("ADB connection failed")
 
        try:
            # Write specific test steps here, operate with airtest
            os.system("adb shell input keyevent 3")
            sleep(2)
            os.system("adb shell am start -n com.android.car.settings/.common.CarSettingActivities\$BluetoothSettingsActivity")
            # Assume there are some operations to be tested
            if exists(Template(r"t28_bt_switch_off.png",threshold=0.8, resolution=(1260, 2800))):
                touch(Template(r"t28_bt_switch_off.png",threshold=0.8, resolution=(1260, 2800)))
                sleep(1)
                if exists(Template(r"t28_bt_switch_on.png")):
                    test_results.append(("Congratulations! Bluetooth switch is successfully turned off!!!", "Pass"))
                else:
                    test_results.append(("Sorry! Failed to turn off Bluetooth switch!!!", "Fail"))
            else:
                touch(Template(r"t28_bt_switch_on.png",threshold=0.8, resolution=(1260, 2800)))
                sleep(1)
                if exists(Template(r"t28_bt_switch_off.png")):
                    test_results.append(("Congratulations! Bluetooth switch is successfully turned on!!!", "Pass"))
                else:
                    test_results.append(("Sorry! Failed to turn on Bluetooth switch!!!", "Fail"))
        except Exception as e:
            print(f"Exception occurred in test case 1: {str(e)}")
        os.system("adb kill-server")
        os.system("adb start-server")
 
    # Airtest test case 2, operate WiFi switch
    def test_airtest_case2(self):
        self.check_adb_connection()
        if not self.adb_connected:
            self.skipTest("ADB connection failed")
 
        try:
            # Write specific test steps here, operate with airtest
            os.system("adb shell input keyevent 3")
            sleep(2)
            os.system("adb shell am start -a android.intent.action.MAIN -n com.android.car.settings/com.android.car.settings.common.CarSettingActivities\$WifiSettingsActivity")
            # Assume there are some operations to be tested
            if exists(Template(r"t28_wifi_switch_off.png",threshold=0.8, resolution=(1260, 2800))):
                touch(Template(r"t28_bt_switch_off.png",threshold=0.8, resolution=(1260, 2800)))
                sleep(1)
                if exists(Template(r"t28_wifi_switch_on.png")):
                    test_results.append(("Congratulations! WiFi switch is successfully turned off!!!", "Pass"))
                else:
                    test_results.append(("Sorry! Failed to turn off WiFi switch!!!", "Fail"))
            else:
                touch(Template(r"t28_wifi_switch_on.png",threshold=0.8, resolution=(1260, 2800)))
                sleep(1)
                if exists(Template(r"t28_wifi_switch_off.png")):
                    test_results.append(("Congratulations! WiFi switch is successfully turned on!!!", "Pass"))
                else:
                    test_results.append(("Congratulations! WiFi switch is successfully turned on!!!", "Fail"))
        except Exception as e:
            print(f"Exception occurred in test case 2: {str(e)}")
        os.system("adb kill-server")
        os.system("adb start-server")
 
    # Airtest test case 3, operate hotspot switch
    def test_airtest_case3(self):
        self.check_adb_connection()
        if not self.adb_connected:
            self.skipTest("ADB connection failed")
 
        try:
            # Write specific test steps here, operate with airtest
            os.system("adb shell input keyevent 3")
            sleep(2)
            os.system("adb shell am start -n com.android.settings/com.android.settings.Settings\$VivoTetherSettingsActivity")
            # Assume there are some operations to be tested
            if exists(Template(r"t28_hostpot_switch_off.png",threshold=0.8,record_pos=(1132,547), resolution=(1260, 2800))):
                touch(Template(r"t28_hostpot_switch_off.png",threshold=0.8, record_pos=(1132,547),resolution=(1260, 2800)))
                sleep(1)
                if exists(Template(r"t28_hostpot_switch_on.png")):
                    test_results.append(("Congratulations! Hotspot switch is successfully turned off!!!", "Pass"))
                else:
                    test_results.append(("Sorry! Failed to turn off Hotspot switch!!!", "Fail"))
            else:
                touch(Template(r"t28_hostpot_switch_on.png",threshold=0.8, record_pos=(1132,547), resolution=(1260, 2800)))
                sleep(1)
                if exists(Template(r"t28_hostpot_switch_off.png")):
                    test_results.append(("Congratulations! Hotspot switch is successfully turned on!!!", "Pass"))
                else:
                    test_results.append(("Sorry! Failed to turn on Hotspot switch!!!", "Fail"))
        except Exception as e:
            print(f"Exception occurred in test case 3: {str(e)}")
        os.system("adb kill-server")
        os.system("adb start-server")

# 使用 suite() 函数创建测试套件
def suite():
    suite = unittest.TestSuite()
    # suite.addTest(MyTestCase('test_airtest_case1'))
    # suite.addTest(MyTestCase('test_airtest_case2'))
    suite.addTest(MyTestCase('test_airtest_case3'))
    return suite

# if __name__ == '__main__':
#     # 运行测试用例
#     runner = unittest.TextTestRunner()
#     result = runner.run(suite())
#     # 生成测试报告
#     report_path = simple_report(__file__, logpath=True,output=r'C:\Users\Administrator\Desktop\tmp\Airtest0409\Airtest0409_1\HTMLTestRunner_Chart_0409_1\report\ltm.html')
#     print(f"测试报告已生成:{report_path}")

# if __name__ == '__main__':
#     import os

#     # 运行测试用例
#     runner = unittest.TextTestRunner()
#     result = runner.run(suite())

#     # 获取用户输入的报告路径
#     output_path = input("请输入测试报告的输出路径:")

#     # 生成测试报告
#     report_path = simple_report(__file__, logpath=True, output=output_path)
#     print(f"测试报告已生成:{report_path}")

if __name__ == '__main__':
    # 运行测试用例
    runner = unittest.TextTestRunner()
    result = runner.run(suite())
    # 生成测试报告,保存在当前文件夹下
    current_directory = os.path.dirname(__file__)
    # report_path = simple_report(__file__, logpath=True, output=os.path.join(current_directory, 'report', '2024tm.html'))
    report_path = simple_report(__file__, logpath=True, output=os.path.join(current_directory, '.', '2024tm.html'))
    print(f"测试报告已生成:{report_path}")

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值