app自动化测试---appium之鼠标滑动

1.说明

appium官方文档 : https://appium.io/docs/en/about-appium/intro/

uiselector 元素定位:https://developer.android.google.cn/reference/androidx/test/uiautomator/UiSelector?hl=en

 

2. 操作代码

app自动化测试---appium之鼠标滑动

from appium import webdriver
import time

from appium.webdriver.common.touch_action import TouchAction

desired_caps = {
    'platformName':'Android',          # 测试Android系统
    'platformVersion':'7.1.2',         # Android版本 可以在已连接手机 设置->关于手机 中查看
    'deviceName':'127.0.0.1:62001',    # cmd中使用 adb devices 命令查看已连接的设备
    'automationName':'UiAutomator2',   # 自动化引擎(默认UiAutomator2即可)
    'noReset': True,                   # 不要重置app的状态(比如,已经登陆的app,我们运行项目的时候保留之前的状态)
    'fullReset': False,                        # 不要清理app的缓存数据(比如,已经登陆的app,我们运行项目的时候保留之前的状态)
    'appPackage':"org.cnodejs.android.md",     # 应用的包名(打开对应包名的app)
    'appActivity': ".ui.activity.MainActivity" # 应用的活动页名称
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_capabilities=desired_caps)
time.sleep(5)

viewgroup = driver.find_element_by_android_uiautomator('new UiSelector().className("android.view.ViewGroup").index(2)')

# 获取元素控件的坐标位置(使用坐标位置去计算滑动坐标,可以保证我们在不同的手机上正常运行)
print('location', viewgroup.location) # {'x': 0, 'y': 120} 元素的左上角坐标
print('rect', viewgroup.rect)         # {'height': 1440, 'width': 900, 'x': 0, 'y': 120}  元素宽高和元素左上角坐标
zb = viewgroup.rect

# 滑动的开始坐标x,y   结束坐标x,y   滑动时间(毫秒)
# 当前设置效果为:从下往上滑动
driver.swipe(start_x=zb['width']/2,start_y=zb['height']+zb['y']-1,end_x=zb['width']/2,end_y=zb['y']+1,duration=200)
# 等同于
# driver.swipe(start_x=450,start_y=1559,end_x=450,end_y=121,duration=200)

# 当前设置效果为:从上往下滑动
driver.swipe(start_x=300, start_y=200, end_x=300, end_y=800, duration=200)
time.sleep(3)

# 点击页面中的第一个帖子的作者
first_tiezi = '//*[@resource-id="org.cnodejs.android.md:id/recycler_view"]/android.widget.LinearLayout[1]/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout[2]'
driver.find_element_by_xpath(first_tiezi).click()
time.sleep(3)

# 当前设置效果为:从右往左滑动
driver.swipe(start_x=550,start_y=700,end_x=500,end_y=700,duration=200)
time.sleep(3)

# 当前设置效果为:从左往右滑动
driver.swipe(start_x=50,start_y=700,end_x=550,end_y=700,duration=200)
time.sleep(3)

 

上一篇:获取应用包名和入口activity


下一篇:appium-多线程启动app(多台设备启动app)