乐于分享
好东西不私藏

ios_自动化测试(1)

ios_自动化测试(1)
在运行脚本之前iPhone需要先安装WebDriverAgent;
"""
iOS App 自动化测试脚本
使用 Appium 和 Python 进行 iOS 应用测试"""
import unittest
import time
from appium import webdriver
from appium.options.ios import XCUITestOptions
from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 配置参数
APP_BUNDLE_ID = "com.hello.world"  # 替换为你的应用的Bundle ID
# APP_PATH = "/path/to/your/app.ipa"  # 替换为你的.app或.ipa文件路径
TEAM_ID = "X6D34ert"  # 你的Team ID
DEVICE_NAME = "iPhone15"  # 设备名称或模拟器名称
PLATFORM_VERSION = "18.6"  # iOS版本
class IOSAutomationTest(unittest.TestCase):
"""iOS应用自动化测试类"""
@classmethod
def setUpClass(cls):
"""测试前的初始化设置"""
# 配置Appium选项
options = XCUITestOptions()
        options.platform_name = "iOS"
options.platform_version = PLATFORM_VERSION
        options.device_name = DEVICE_NAME
        options.bundle_id = APP_BUNDLE_ID
        options.xcode_org_id = TEAM_ID
        options.xcode_signing_id = "iPhone Developer"
options.automation_name = "XCUITest"
options.udid = "00008030-0002398423422"  # 如果使用真机,需要填写设备的UDID
        # 如果需要安装app,取消下面的注释
# options.app = APP_PATH
        # 连接Appium服务器
cls.driver = webdriver.Remote(
"http://localhost:4723",
options=options
        )

@classmethod
def tearDownClass(cls):
"""测试后的清理工作"""
if hasattr(cls, 'driver'):
cls.driver.quit()

def test_app_launch(self):
"""测试应用启动"""
# 等待应用启动
time.sleep(3)

# 获取当前应用的bundle ID
current_bundle = self.driver.current_package
self.assertEqual(current_bundle, APP_BUNDLE_ID, 
f"应用Bundle ID不匹配: 期望 {APP_BUNDLE_ID}, 实际 {current_bundle}")
print("✓ 应用启动测试通过")

def test_element_find(self):
"""测试元素查找"""
try:
# 示例:通过Accessibility ID查找元素
# element = self.driver.find_element(AppiumBy.ACCESSIBILITY_ID, "your_element_id")
            # 示例:通过XPath查找元素
element1 = self.driver.find_element(AppiumBy.XPATH, '//XCUIElementTypeButton[@name="Sale"]')
            element1.click()
            time.sleep(2)


            element2 = self.driver.find_element(AppiumBy.XPATH, '//XCUIElementTypeButton[@name="Buy"]')
            element2.click()
            time.sleep(2)

            element3 = self.driver.find_element(AppiumBy.XPATH, '//XCUIElementTypeButton[@name="Home"]')
            element3.click()
            time.sleep(2)

print("✓ 元素查找测试通过")
except Exception as e:
print(f"✗ 元素查找测试失败: {e}")

def test_click_element(self):
"""测试点击元素"""
try:
# 示例:点击一个按钮
# button = self.driver.find_element(AppiumBy.ACCESSIBILITY_ID, "login_button")
            # button.click()
            # 等待页面响应
# WebDriverWait(self.driver, 10).until(
            #     EC.presence_of_element_located((AppiumBy.ACCESSIBILITY_ID, "home_page"))
            # )
print("✓ 点击元素测试通过")
except Exception as e:
print(f"✗ 点击元素测试失败: {e}")

def test_input_text(self):
"""测试输入文本"""
try:
# 示例:在文本框中输入文本
# text_field = self.driver.find_element(AppiumBy.ACCESSIBILITY_ID, "username_field")
            # text_field.clear()
            # text_field.send_keys("test_user")
print("✓ 输入文本测试通过")
except Exception as e:
print(f"✗ 输入文本测试失败: {e}")

def test_swipe(self):
"""测试滑动操作"""
try:
# 获取屏幕尺寸
size = self.driver.get_window_size()
width = size['width']
height = size['height']

# 示例:从下向上滑动
# self.driver.swipe(
            #     start_x=width / 2,
            #     start_y=height * 0.8,
            #     end_x=width / 2,
            #     end_y=height * 0.2,
            #     duration=800
            # )
print("✓ 滑动操作测试通过")
except Exception as e:
print(f"✗ 滑动操作测试失败: {e}")

def test_screenshot(self):
"""测试截图功能"""
try:
            screenshot_path = "/iOSTest/screenshot.png"
self.driver.save_screenshot(screenshot_path)
print(f"✓ 截图已保存到: {screenshot_path}")
except Exception as e:
print(f"✗ 截图测试失败: {e}")


def run_tests():
"""运行所有测试"""
# 创建测试套件
suite = unittest.TestLoader().loadTestsFromTestCase(IOSAutomationTest)

# 运行测试
runner = unittest.TextTestRunner(verbosity=2)
    result = runner.run(suite)

# 返回测试结果
return result.wasSuccessful()


if __name__ == "__main__":
print("=" * 60)
print("iOS App 自动化测试")
print("=" * 60)
print(f"Team ID: {TEAM_ID}")
print(f"Bundle ID: {APP_BUNDLE_ID}")
print(f"设备: {DEVICE_NAME}")
print(f"iOS版本: {PLATFORM_VERSION}")
print("=" * 60)
print()

    success = run_tests()

print()
print("=" * 60)
if success:
print("✓ 所有测试通过!")
else:
print("✗ 部分测试失败,请检查日志")
print("=" * 60)