目录
滑动方法介绍
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)