appium--滑动
Posted 邹邹很busy。
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了appium--滑动相关的知识,希望对你有一定的参考价值。
前戏
在进行app自动化的时候,经常会进行滑动的操作,比如左右滑动,上下的滑动。在appium中滑动用到swipe方法进行滑动。
def swipe(开始横坐标,开始纵坐标,结束横坐标,结束纵坐标,时间(ms))
上面的图表示手机的左上角为坐标原点,往右为x正半轴,往下为y正半轴
获取屏幕的坐标
import pytest from appium import webdriver from selenium.webdriver.support.ui import WebDriverWait class Testcsca(): def setup(self): caps = {} caps["platformName"] = "android" # caps["deviceName"] = "127.0.0.1:62001" caps["deviceName"] = "CLB0219314000452" caps["appPackage"] = "com.jgw.csca" caps["appActivity"] = "com.jgw.csca.view.activity.LoginActivity" caps["platfromVersion"] = "9.0.0" caps["autoGrantPermissions"] = True # 设置自动授权权限 caps[\'unicodeKeyboard\'] = True # 输入中文时要加,要不然输入不了中文 caps[\'resetKeyboard\'] = True # 输入中文时要加,要不然输入不了中文 caps[\'automationName\'] = \'uiautomator2\' self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps) self.driver.implicitly_wait(20) def test_size(self): x = self.driver.get_window_size()[\'width\'] # 获取x轴的长度 y = self.driver.get_window_size()[\'height\'] # 获取y轴的长度 print(x, y)
结果
720 1280
使用坐标定位
from time import sleep import pytest from appium import webdriver from selenium.webdriver.support.ui import WebDriverWait class Testcsca(): def setup(self): caps = {} caps["platformName"] = "Android" # caps["deviceName"] = "127.0.0.1:62001" caps["deviceName"] = "CLB0219314000452" caps["appPackage"] = "com.jgw.csca" caps["appActivity"] = "com.jgw.csca.view.activity.LoginActivity" caps["platfromVersion"] = "9.0.0" caps["autoGrantPermissions"] = True # 设置自动授权权限 caps[\'unicodeKeyboard\'] = True # 输入中文时要加,要不然输入不了中文 caps[\'resetKeyboard\'] = True # 输入中文时要加,要不然输入不了中文 caps[\'automationName\'] = \'uiautomator2\' self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps) self.driver.implicitly_wait(20) # 封装滑动的操作 def swip(self): x = self.driver.get_window_size()[\'width\'] # 获取x轴的长度 y = self.driver.get_window_size()[\'height\'] # 获取y轴的长度 self.driver.swipe(360,900,360,100,1000) # 从下往上滑动 def test_login(self): self.driver.find_element_by_android_uiautomator(\'new UiSelector().text("请输入用户名")\').send_keys(\'123\') self.driver.find_element_by_android_uiautomator(\'new UiSelector().resourceId("com.jgw.csca:id/et_pwd")\').send_keys(\'123456\') self.driver.find_element_by_android_uiautomator(\'new UiSelector().className("android.widget.Button")\').click() self.driver.find_element_by_id(\'com.jgw.csca:id/ll_first_sign\').click() self.driver.find_element_by_xpath(\'//*[@text="已完成"]\').click() sleep(2) self.swip()
虽然上面的方法实现了滑动,但上利用率确不高,因为手机的分辨率不同,所以会导致执行失败,可以使用相对坐标点的方法来进行滑动
相对滑动
from time import sleep import pytest from appium import webdriver from selenium.webdriver.support.ui import WebDriverWait class Testcsca(): def setup(self): caps = {} caps["platformName"] = "Android" # caps["deviceName"] = "127.0.0.1:62001" caps["deviceName"] = "CLB0219314000452" caps["appPackage"] = "com.jgw.csca" caps["appActivity"] = "com.jgw.csca.view.activity.LoginActivity" caps["platfromVersion"] = "9.0.0" caps["autoGrantPermissions"] = True # 设置自动授权权限 caps[\'unicodeKeyboard\'] = True # 输入中文时要加,要不然输入不了中文 caps[\'resetKeyboard\'] = True # 输入中文时要加,要不然输入不了中文 caps[\'automationName\'] = \'uiautomator2\' self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps) self.driver.implicitly_wait(20) # 封装获取屏幕大小的操作 def get_windoe_size(self): x = self.driver.get_window_size()[\'width\'] # 获取x轴的长度 y = self.driver.get_window_size()[\'height\'] # 获取y轴的长度 return x,y # 返回的是一个元组 # 滑动的封装 def swip(self): size = self.get_windoe_size() x1 = int(size[0] * 0.5) # size[0]取元组的第一个值,*0.5表示中间的点 y1 = int(size[1] * 0.9) # size[1]取元组的第二个值,*0.1表示距离底部近 y2 = int(size[1] * 0.1) self.driver.swipe(x1, y1, x1, y2, 1000) def test_login(self): self.driver.find_element_by_android_uiautomator(\'new UiSelector().text("请输入用户名")\').send_keys(\'123\') self.driver.find_element_by_android_uiautomator(\'new UiSelector().resourceId("com.jgw.csca:id/et_pwd")\').send_keys(\'123456\') self.driver.find_element_by_android_uiautomator(\'new UiSelector().className("android.widget.Button")\').click() self.driver.find_element_by_id(\'com.jgw.csca:id/ll_first_sign\').click() self.driver.find_element_by_xpath(\'//*[@text="已完成"]\').click() sleep(2) self.swip()
以上是关于appium--滑动的主要内容,如果未能解决你的问题,请参考以下文章
哪位大神知道怎么用appium实现引导页的滑动,并且成功跳转到主界面?