码迷,mamicode.com
首页 > 移动开发 > 详细

appium--滑动

时间:2019-09-10 20:59:10      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:一个   技术   表示   eset   class   unicode   用户名   封装   坐标   

前戏

在进行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--滑动

标签:一个   技术   表示   eset   class   unicode   用户名   封装   坐标   

原文地址:https://www.cnblogs.com/zouzou-busy/p/11403078.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!