2022/05/08学习内容
整理时间:2022/05/09
参考资料:https://www.bilibili.com/video/BV1NM4y1K73T?p=1&spm_id_from=333.851.header_right.history_list.click
进入新阶段–实操
入门案例 main.py
1.python控制—打开虚拟机—进入设置界面
2.出错集合
1)如果python运行出现错误,可以检查的方向
=检查下ANDROID_HOME环境变量是否正确配置,配置好之后重启下appium和pycharm 命令行等相关工具
=本人就是ANDROID_HOME环境变量配置出了点问题,一定要配置到build-tools文件夹地址处(我是配置在该文件夹的上一文件夹了)
2)获取deviceName时
==CMD—adb devices
3)提醒:出错后不要着急,多百度会找到解决方案的
# main.py
from appium import webdriver
import time
# 连接移动设备所需参数: 当前要测试的设备的名称deviceName/ 系统platformName/ 系统版本platformVersion
# / 要启动的App的名称appPackage/ 要启动的App的界面appActivity
desired_caps={}
# deviceName -- CMD -- adb devices
desired_caps["deviceName"] = "127.0.0.1:62001"
desired_caps["platformName"] = "Android"
desired_caps["platformVersion"] = "7.1"
# appPackage -- CMD -- adb shell dumpsys window windows | findstr mFocusedApp
desired_caps["appPackage"] = "com.android.settings"
desired_caps["appActivity"] = ".Settings t5"
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_capabilities=desired_caps)
time.sleep(18)
driver.close_app()
driver.quit()
元素定位 find_element.py
# find_element.py
from appium import webdriver
import time
# 连接移动设备所需参数: 当前要测试的设备的名称deviceName/ 系统platformName/ 系统版本platformVersion
# / 要启动的App的名称appPackage/ 要启动的App的界面appActivity
desired_caps={}
# deviceName -- CMD -- adb devices
desired_caps["deviceName"] = "127.0.0.1:62001"
desired_caps["platformName"] = "Android"
desired_caps["platformVersion"] = "7.1"
# appPackage -- CMD -- adb shell dumpsys window windows | findstr mFocusedApp
desired_caps["appPackage"] = "com.android.settings"
desired_caps["appActivity"] = ".Settings t5"
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_capabilities=desired_caps)
time.sleep(1)
# Android手机中 text不是文本 而是属于标签属性
# 获取文本的方法 driver.find_element_by_xpath("//*[text()='显示']").click()
# 获取标签属性的方法
driver.find_element_by_xpath("//*[@text='显示']").click()
time.sleep(3)
driver.close_app()
driver.quit()
基本操作
1.安装卸载App
2.获取包名及启动界面
3.等
输入文本 input_text.py
# input_text.py
from appium import webdriver
import time
# 连接移动设备所需参数: 当前要测试的设备的名称deviceName/ 系统platformName/ 系统版本platformVersion
# / 要启动的App的名称appPackage/ 要启动的App的界面appActivity
desired_caps={}
# deviceName -- CMD -- adb devices
desired_caps["deviceName"] = "127.0.0.1:62001"
desired_caps["platformName"] = "Android"
desired_caps["platformVersion"] = "7.1"
# appPackage -- CMD -- adb shell dumpsys window windows | findstr mFocusedApp
desired_caps["appPackage"] = "com.android.settings"
desired_caps["appActivity"] = ".Settings t5"
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("123")
driver.find_element_by_xpath("//*[@resource-id='android:id/search_src_text']").send_keys("abc")
driver.find_element_by_xpath("//*[@resource-id='android:id/search_src_text']").send_keys("张三")
driver.close_app()
time.sleep(2)
driver.quit()
初级手势操作
1.滑动与滚动 gestures_swipe-scroll.py
2.拖拽 gestures_drag-and-drop.py
# gestures_swipe-scroll.py
from appium import webdriver
import time
# 连接移动设备所需参数: 当前要测试的设备的名称deviceName/ 系统platformName/ 系统版本platformVersion
# / 要启动的App的名称appPackage/ 要启动的App的界面appActivity
desired_caps={}
# deviceName -- CMD -- adb devices
desired_caps["deviceName"] = "127.0.0.1:62001"
desired_caps["platformName"] = "Android"
desired_caps["platformVersion"] = "7.1"
# appPackage -- CMD -- adb shell dumpsys window windows | findstr mFocusedApp
desired_caps["appPackage"] = "com.android.settings"
desired_caps["appActivity"] = ".Settings t5"
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_capabilities=desired_caps)
time.sleep(1)
# 方法1 初始位置与结束位置--滑动
# driver.swipe(0, 972, 0, 576)
# 方法2 操作元素之间的位置--滚动
# el1 = driver.find_element_by_xpath("//*[@text='显示']")
# el2 = driver.find_element_by_xpath("//*[@text='WLAN']")
# driver.scroll(el1, el2)
# 拖拽 起始el 结束el
time.sleep(2)
driver.close_app()
time.sleep(2)
driver.quit()
# gestures_drag-and-drop.py
from appium import webdriver
import time
# 连接移动设备所需参数: 当前要测试的设备的名称deviceName/ 系统platformName/ 系统版本platformVersion
# / 要启动的App的名称appPackage/ 要启动的App的界面appActivity
desired_caps={}
# deviceName -- CMD -- adb devices
desired_caps["deviceName"] = "127.0.0.1:62001"
desired_caps["platformName"] = "Android"
desired_caps["platformVersion"] = "7.1"
# appPackage -- CMD -- adb shell dumpsys window windows | findstr mFocusedApp
desired_caps["appPackage"] = "com.android.launcher3"
desired_caps["appActivity"] = ".launcher3.Launcher t2"
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_capabilities=desired_caps)
time.sleep(1)
# 拖拽 起始el 结束el
el1 = driver.find_element_by_xpath("//*[@text='酷安']")
el2 = driver.find_element_by_xpath("//*[@text='浏览器']")
driver.drag_and_drop(el1, el2)
time.sleep(2)