Files
anroid-CheckShot/test_android.py

117 lines
2.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""
Android 模拟器图像识别测试脚本
"""
from airtest.core.api import *
import sys
# 初始化设备连接
def init_device():
# 连接 Android 模拟器
dev = connect_device("android://127.0.0.1:5037/emulator-5554")
print(f"已连接设备: {dev}")
return dev
# 截取当前屏幕
def capture_screen(filename="screen.png"):
dev = device()
screen = dev.snapshot()
# numpy.ndarray 转换为 PIL Image 并保存
from PIL import Image
import numpy as np
img = Image.fromarray(screen)
img.save(filename)
print(f"截图已保存: {filename}")
return screen
# 测试1: 打开相机应用
def test_open_camera():
print("测试1: 打开相机应用...")
# 使用 ADB 启动相机
shell("am start -n com.inspection.camera/.ui.MainActivity")
sleep(2)
# 截图
capture_screen("test_open_camera.png")
print("相机已打开")
return True
# 测试2: 模拟点击屏幕中心(测试触摸功能)
def test_touch():
print("测试2: 测试触摸功能...")
# 点击屏幕中心
touch([540, 960]) # 模拟器分辨率 1080x1920 的中心点
sleep(1)
capture_screen("test_touch.png")
print("触摸测试完成")
return True
# 测试3: 滑动测试
def test_swipe():
print("测试3: 测试滑动功能...")
# 从上往下滑动
swipe([540, 300], [540, 900])
sleep(1)
capture_screen("test_swipe.png")
print("滑动测试完成")
return True
# 测试4: 查找屏幕上是否有特定文字使用OCR
def test_ocr():
print("测试4: OCR文字识别测试...")
try:
from airtest.aircv import aircv
import numpy as np
# 截图
screen = device().snapshot()
# 使用 PIL 显示图像信息
from PIL import Image
img = Image.fromarray(screen)
print(f"屏幕分辨率: {img.size}")
print(f"屏幕模式: {img.mode}")
capture_screen("test_ocr.png")
print("OCR 测试完成")
return True
except Exception as e:
print(f"OCR测试出错: {e}")
return False
# 主测试函数
def main():
print("=" * 50)
print("Android 模拟器图像识别测试")
print("=" * 50)
try:
# 初始化设备
init_device()
# 执行测试
test_open_camera()
test_touch()
test_swipe()
test_ocr()
print("\n" + "=" * 50)
print("所有测试完成!")
print("=" * 50)
except Exception as e:
print(f"测试出错: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
main()