移动测试
1.概念
1.定义:测试手机程序。
2.测试方面:
- 功能测试:查看功能是否正常。
- 安装卸载测试
- 升级测试
- 兼容性测试:Android系统版本、厂商二次开发版本、不同的分辨率、不同的网络。
- 网络切换、中断测试
- 使用中来电、短信
- 横竖屏切换
- 健壮性:耗电量、流量消耗、崩溃恢复。
2. ADB
1.定义:Android Debug Bridge,既包含ADB服务,也包含与服务对应的Client。
2.作用:通过命令行去操作手机。
adb shell dumpsys window windows | findstr mFocusedApp
3.入门案例:等待5秒后关闭系统设置界面
步骤:打开模拟器系统设置页面 -> 启动appium -> 运行 demo01.py
from appium import webdriver
import time
desired_caps = {}
desired_caps["deviceName"] = "127.0.0.1:62001"
desired_caps["platformName"] = "Android"
desired_caps["platformVersion"] = "7.1"
desired_caps["appPackage"] = "com.android.settings"
desired_caps["appActivity"] = ".Settings"
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_capabilities=desired_caps)
time.sleep(5)
driver.close_app()
driver.quit()
3.操作
基础API
①driver:
close_app()
quit()
install_app('apk在电脑上的绝对路径')
remove_app('应用的包名')
is_app_installed('应用的包名')
push_file(目标位置,base64编码内容)
pull_file(来源位置)
page_source
find_element/find_elements
current_package
current_activity
②element:
text
click()
get_attribute(属性名称)
location
size
元素定位
Android SDK提供工具
可用于查找页面控件和鼠标位置。
from appium import webdriver
import time
desired_caps = {}
desired_caps["deviceName"] = "127.0.0.1:62001"
desired_caps["platformName"] = "Android"
desired_caps["platformVersion"] = "7.1"
desired_caps["appPackage"] = "com.android.settings"
desired_caps["appActivity"] = ".Settings"
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_capabilities=desired_caps)
driver.find_element_by_xpath("//*[@text='显示']").click()
time.sleep(3)
driver.close_app()
driver.quit()
输入文本
send_keys("文本内容")
desired_caps['unicodeKeyboard'] = True
desired_caps['resetKeyboard'] = True
clear()
from appium import webdriver
import time
desired_caps = {}
desired_caps["deviceName"] = "127.0.0.1:62001"
desired_caps["platformName"] = "Android"
desired_caps["platformVersion"] = "7.1"
desired_caps["appPackage"] = "com.android.settings"
desired_caps["appActivity"] = ".Settings"
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_capabilities=desired_caps)
time.sleep(1)
driver.find_element_by_xpath("//*[@resource-id='com.android.settings:id/search']").click()
time.sleep(1)
driver.find_element_by_xpath("//*[@resource-id='android:id/search_src_text']").send_keys("abc")
driver.close_app()
driver.quit()
模拟手势
滑动:
swipe(self, start_x: int, start_y: int, end_x: int, end_y: int, duration: int = 0)
duration默认600毫秒。滑动存在“惯性”,会根据按下、抬起位置及总时间,滚动不一样的距离。
滚动:
scroll(origin_el,destination_el,duration)
不需要手动获取位置,直接传递元素。手机的分辨率不会造成影响。
from appium import webdriver
import time
desired_caps = {}
desired_caps["deviceName"] = "127.0.0.1:62001"
desired_caps["platformName"] = "Android"
desired_caps["platformVersion"] = "7.1"
desired_caps["appPackage"] = "com.android.settings"
desired_caps["appActivity"] = ".Settings"
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_capabilities=desired_caps)
time.sleep(1)
el1 = driver.find_element_by_xpath("//*[@text='通知']")
el2 = driver.find_element_by_xpath("//*[@text='WLAN']")
driver.scroll(el1, el2)
time.sleep(1)
driver.close_app()
driver.quit()
拖拽:
按下 -> 等待一定时间 -> 移动 -> 松手
drag_and_drop(origin_el,destination_el)
TouchAction:
比较复杂的、连续的触摸行为,如图案解锁屏幕。
创建TouchAction对象,传递driver参数 -> 添加动作 -> 执行操作perform()
press(self,el,x,y,pressure)
long_press(self,el,x,y,duration=1000)
move_to(self,el,x,y)
wait(self,ms)
release()
tap(self,el,x,y,count = 1)
TouchAction 模拟滑动
from appium import webdriver
import time
from appium.webdriver.common.touch_action import TouchAction
desired_caps = {}
desired_caps["deviceName"] = "127.0.0.1:62001"
desired_caps["platformName"] = "Android"
desired_caps["platformVersion"] = "7.1"
desired_caps["appPackage"] = "com.android.settings"
desired_caps["appActivity"] = ".Settings"
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_capabilities=desired_caps)
time.sleep(1)
action = TouchAction(driver)
action.press(driver.find_element_by_xpath("//*[@text='通知']")).wait(500).move_to(driver.find_element_by_xpath("//*[@text='WLAN']"))
action.release()
action.perform()
time.sleep(1)
driver.close_app()
driver.quit()
设置解锁图案Z
from appium import webdriver
import time
from appium.webdriver.common.touch_action import TouchAction
desired_caps = {}
desired_caps["deviceName"] = "127.0.0.1:62001"
desired_caps["platformName"] = "Android"
desired_caps["platformVersion"] = "7.1"
desired_caps["appPackage"] = "com.android.settings"
desired_caps["appActivity"] = ".Settings"
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_capabilities=desired_caps)
time.sleep(1)
action = TouchAction(driver)
action.press(driver.find_element_by_xpath("//*[@text='声音']")).wait(500)
action.move_to(driver.find_element_by_xpath("//*[@text='WLAN']"))
action.release()
action.perform()
time.sleep(1)
driver.find_element_by_xpath("//*[@text='安全']").click()
time.sleep(1)
driver.find_element_by_xpath("//*[@text='屏幕锁定']").click()
time.sleep(1)
driver.find_element_by_xpath("//*[@text='图案']").click()
time.sleep(1)
action.press(x=105, y=450).wait(200)
action.move_to(x=270, y=450).wait(200)
action.move_to(x=435, y=450).wait(200)
action.move_to(x=270, y=615).wait(200)
action.move_to(x=105, y=780).wait(200)
action.move_to(x=270, y=780).wait(200)
action.move_to(x=435, y=780).wait(200)
action.release()
action.perform()
driver.close_app()
driver.quit()
driver的其他操作
.device_time
.get_window_size()
.network_connection
.set_network_connection(数字)
.keyevent(按键数字)
.get_screenshot_as_file/save_screenshot(图片路径)
.open_notifications()
|