117 lines
3.8 KiB
Python
117 lines
3.8 KiB
Python
# -*- coding: utf-8 -*-
|
||
import sys
|
||
import io
|
||
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
|
||
|
||
"""
|
||
测试拍照功能
|
||
"""
|
||
from airtest.core.api import *
|
||
from PIL import Image
|
||
import os
|
||
import time
|
||
|
||
def init_device():
|
||
# dev = connect_device("android://127.0.0.1:5037/emulator-5554") # 模拟器
|
||
dev = connect_device("android://127.0.0.1:5037/APH0219A29002701") # 真机
|
||
print(f"已连接设备: {dev}")
|
||
return dev
|
||
|
||
def capture_screen(filename="screen.png"):
|
||
screen = device().snapshot()
|
||
img = Image.fromarray(screen)
|
||
img.save(filename)
|
||
print(f"截图已保存: {filename}")
|
||
return screen
|
||
|
||
def test_camera():
|
||
print("=" * 50)
|
||
print("测试:拍照功能")
|
||
print("=" * 50)
|
||
|
||
# 启动相机应用
|
||
print("1. 启动相机应用...")
|
||
shell("am start -n com.inspection.camera/.ui.MainActivity")
|
||
sleep(3)
|
||
|
||
capture_screen("01_app_loaded.png")
|
||
print(" 相机应用已启动")
|
||
|
||
# 点击拍照按钮 (屏幕底部中央 - 2400高,底部区域约在2200)
|
||
print("2. 点击拍照按钮...")
|
||
touch([540, 2200]) # 拍照按钮位置
|
||
sleep(3)
|
||
|
||
capture_screen("02_after_capture.png")
|
||
print(" 拍照完成")
|
||
|
||
# 检查照片是否生成
|
||
print("3. 检查照片是否生成...")
|
||
|
||
try:
|
||
# 创建Pictures目录(如果不存在)
|
||
shell("mkdir -p /storage/emulated/0/Pictures/inspection")
|
||
sleep(1)
|
||
|
||
# 列出Pictures目录下的文件
|
||
result = shell("ls -la /storage/emulated/0/Pictures/inspection/")
|
||
print(f" Pictures目录内容: {result}")
|
||
|
||
# 检查是否有新的照片文件
|
||
files_output = shell("ls /storage/emulated/0/Pictures/inspection/").strip()
|
||
|
||
if files_output and "No such file" not in files_output:
|
||
files = files_output.split('\n')
|
||
print(f" 照片文件列表: {files}")
|
||
|
||
if files and files[0]:
|
||
latest_photo = files[-1].strip()
|
||
print(f" [OK] 照片已生成: {latest_photo}")
|
||
|
||
# 拉取最新照片到本地
|
||
pull_cmd = f"/storage/emulated/0/Pictures/inspection/{latest_photo}"
|
||
os.system(f'"C:\\Users\\xiaji\\AppData\\Local\\Android\\Sdk\\platform-tools\\adb.exe" -s emulator-5554 pull "{pull_cmd}" . 2>nul')
|
||
|
||
if os.path.exists(latest_photo):
|
||
print(f" [OK] 照片已保存到本地: {latest_photo}")
|
||
print("\n" + "=" * 50)
|
||
print("测试结果: 拍照功能正常!")
|
||
print("=" * 50)
|
||
return True
|
||
else:
|
||
print(" [X] 照片未保存到本地")
|
||
return False
|
||
else:
|
||
print(" [X] 未找到照片文件")
|
||
return False
|
||
else:
|
||
# 检查DCIM目录
|
||
dcim_result = shell("ls /storage/emulated/0/DCIM/Camera/")
|
||
print(f" DCIM目录内容: {dcim_result}")
|
||
if dcim_result and "No such file" not in dcim_result and dcim_result.strip():
|
||
print(f" [OK] 照片已生成在DCIM目录")
|
||
print("\n" + "=" * 50)
|
||
print("测试结果: 拍照功能正常!")
|
||
print("=" * 50)
|
||
return True
|
||
|
||
print(" [X] 未找到任何照片")
|
||
return False
|
||
|
||
except Exception as e:
|
||
print(f" [X] 检查照片时出错: {e}")
|
||
return False
|
||
|
||
if __name__ == "__main__":
|
||
try:
|
||
init_device()
|
||
result = test_camera()
|
||
if result:
|
||
print("\n测试通过!")
|
||
else:
|
||
print("\n测试失败!")
|
||
except Exception as e:
|
||
print(f"测试出错: {e}")
|
||
import traceback
|
||
traceback.print_exc()
|