08-appium-滑动方法封装
Posted 爱学习de测试小白
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了08-appium-滑动方法封装相关的知识,希望对你有一定的参考价值。
目录
滑动方法介绍
def swipe(self: T, start_x: int, start_y: int, end_x: int, end_y: int, duration: int = 0) -> T:
"""从一个点滑动到另一个点,duration是持续时间.
Args:
start_x: 开始滑动的x坐标
start_y: 开始滑动的y坐标
end_x: 结束点x坐标
end_y: 结束点y坐标
duration: 持续时间,单位毫秒
Usage:
driver.swipe(100, 100, 100, 400)
"""
滑动方法封装
# -*- coding: utf-8 -*-
# @Time : 2021/5/1
# @Author : 大海
from time import sleep
from appium import webdriver
desired_capabilities = {
"platformName": "android", # 测试的平台 Android/ios
"deviceName": "127.0.0.1:62001", # adb devices 查看,这里使用的是夜神模拟器
"platformVersion": "7.1.2",
"appPackage": "com.jingdong.app.mall", # 京东app
"appActivity": ".main.MainActivity"
}
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_capabilities=desired_capabilities)
driver.implicitly_wait(10)
# 获取屏幕的size
size = driver.get_window_size()
print(size)
# 屏幕宽度width
print(size['width'])
# 屏幕高度width
print(size['height'])
# 点击同意
driver.find_element_by_id('com.jingdong.app.mall:id/bqd').click()
def swipe_up(driver, t=500, n=1):
"""向上滑动屏幕"""
print("向上滑动屏幕")
s = driver.get_window_size()
x1 = s['width'] * 0.5 # x坐标
y1 = s['height'] * 0.75 # 起始y坐标
y2 = s['height'] * 0.25 # 终点y坐标
for i in range(n):
driver.swipe(x1, y1, x1, y2, t)
def swipe_down(driver, t=500, n=1):
"""向下滑动屏幕"""
print("向下滑动屏幕")
s = driver.get_window_size()
x1 = s['width'] * 0.5 # x坐标
y1 = s['height'] * 0.25 # 起始y坐标
y2 = s['height'] * 0.75 # 终点y坐标
for i in range(n):
driver.swipe(x1, y1, x1, y2, t)
def swipe_left(driver, t=500, n=1):
"""向左滑动屏幕"""
print("向左滑动屏幕")
s = driver.get_window_size()
x1 = s['width'] * 0.75
y1 = s['height'] * 0.5
x2 = s['width'] * 0.25
for i in range(n):
driver.swipe(x1, y1, x2, y1, t)
def swipe_right(driver, t=500, n=1):
"""向右滑动屏幕"""
print("向右滑动屏幕")
s = driver.get_window_size()
x1 = s['width'] * 0.25
y1 = s['height'] * 0.5
x2 = s['width'] * 0.75
for i in range(n):
driver.swipe(x1, y1, x2, y1, t)
if __name__ == '__main__':
sleep(5)
swipe_up(driver)
swipe_down(driver)
以上是关于08-appium-滑动方法封装的主要内容,如果未能解决你的问题,请参考以下文章
将 Google API 客户端对象从活动传递到滑动选项卡片段