移动UI自动化

前面2篇文章已经做好准备了,现在可以用python来实现移动UI自动化了,总结和复习一下以前学的内容。  

                ——2019.11.23

 一、脚本和移动端连接

from appium import  webdriver

#期望的配置
desired_caps = {
 "platformName":"Android",
 "deviceName": "127.0.0.1:62001",
 "appPackage": "com.umaman.laiyifen",
 "appActivity": "com.laiyifen.app.activity.member.login.LoginActivity",
}

dr = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

这里的dr对象相当于web自动化中的dr对象

二、元素定位

第一类(和web定位方式差不多)
1、from appium import webdriver
示例:find_element_by_id("com.example.android.contactmanager:id/showInvisible")
2、from appium import webdriver
from selenium.webdriver.common.by import By
示例:driver.find_element(by=By.ID,value="com.example.android.contactmanager:id/showInvisible")

applium 1.7版本后name定位功能已没有了,在xpath 中有支持。


第二类
find_element_by_ios_uiautomation
find_elements_by_ios_uiautomation
find_element_by_android_uiautomator
find_elements_by_android_uiautomator
find_element_by_accessibility_id
find_elements_by_accessibility_id

第三类(坐标)

借助appium,当你点击某个元素时,你可以看到bounds的值;

1、点击,
#100毫秒,点击(450,989), (746,1079)的区域
dr.tap([(450,989), (746,1079)], 100)
2、滑动
#从200,300滑到200,800的位置
dr.flick(200, 300, 200, 800)

滑动:

#元素用坐标点击
#获取窗口大小
print(self.driver.get_window_size())
#宽
x = self.driver.get_window_size()['width']
#高
print("x=",x)
y = self.driver.get_window_size()['height']
print("y=", y)
#左滑
self.driver.swipe(6 / 7 * x, 1 / 2 * y, 1 / 7 * x, 1 / 2 * y, 100)
sleep(4)
#右滑
self.driver.swipe(1 / 7 * x, 1 / 2 * y, 6/ 7 * x, 1 / 2 * y, 500)
sleep(4)
# 向上滑
self.driver.swipe(1 / 2 * x, 1 / 2 * y, 1 / 2 * x, 1 / 7 * y, 200)
sleep(4)
# 向下滑动
self.driver.swipe(1 / 2 * x, 1 / 7 * y, 1 / 2 * x, 6 / 7 * y, 200)
 
原文地址:https://www.cnblogs.com/yinwenbin/p/11919636.html