UIautomator、swipe滑动、press_keycod

2020-02-28  本文已影响0人  清水秋香
java 中string要用双引号
new UiSelector().resourceId("com.hpbr.bosszhipin:id/tv_position_name")
new UiSelector().className("android.widget.TextView")
new UiSelector().text("测试工程师").className("android.widget.TextView")
如果选择的为多个默认只显示第一个,可以用index来选择相应元素
new UiSelector().resourceId("com.hpbr.bosszhipin:id/ll_actions").childSelector(new UiSelector().className("android.widget.ImageView").index(2))
new UiSelector().textContains('工程师')
new UiSelector().textMatches('\d{2}.\d{2}')
可加分号进行匹配选择
new UiSelector().textMatches("\d{2}-\d{2}.*?");new UiSelector().resourceId("com.hpbr.bosszhipin:id/tv_position_name")
 def swipe(self, start_x, start_y, end_x, end_y, duration=None):
        """Swipe from one point to another point, for an optional duration.

        Args:
            start_x (int): x-coordinate at which to start
            start_y (int): y-coordinate at which to start
            end_x (int): x-coordinate at which to stop
            end_y (int): y-coordinate at which to stop
            duration (:obj:`int`, optional): time to take the swipe, in ms.

        Usage:
            driver.swipe(100, 100, 100, 400)

        Returns:
            `WebElement`
        """
        # `swipe` is something like press-wait-move_to-release, which the server
        # will translate into the correct action
        action = TouchAction(self)
        action \
            .press(x=start_x, y=start_y) \
            .wait(ms=duration) \
            .move_to(x=end_x, y=end_y) \
            .release()
        action.perform()
        return self

参数就是起点和终点x/y坐标
duration是滑动从起点到终点坐标所耗费的时间,单位是毫秒,
这个时间十分重要,慢速滑动和快速滑动效果是不同的,快速滑动会有惯性效果。

ele = driver.find_element_by_id('xxx')
location = ele.location  #左上角坐标
size = ele.size    #元素的高度和宽度
#计算起始点坐标
start_x = location['x'] + int(size['width'])*0.2
start_y = location['y'] + int(size['height'])*0.5
end_x = location['x'] + int(size['width'])*0.8
end_y = location['y'] + int(size['height'])*0.5

滑动屏幕打印信息

import time

from appium import webdriver

#准备自动化配置信息
from selenium.webdriver.common.by import By

desired_caps={
    #移动设备平台
    'platformName':'Android',
    #平台OS版本号,写整数位即可
    'plathformVersion':'8',
    #设备的名称--值可以随便写
    'deviceName':'test0106',
    #提供被测app的信息-包名,入口信息
    'appPackage':'com.hpbr.bosszhipin',
    'appActivity':'.module.launcher.WelcomeActivity',
    #确保自动化之后不重置app
    'noReset':True,
    #设置session的超时时间,单位秒
    'newCommandTimeout':6000
}
#初始化driver对象-用于控制手机
driver=webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)
driver.implicitly_wait(10)#稳定元素
# driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.hpbr.bosszhipin:id/tv_position_name")').click()
#滑动
start_x = 500
start_y = 1500
end_x = start_x
# y坐标减去滑动长度
end_y = start_y - 1000
#如果滑动的距离很长,时间很短会有惯性效果
time.sleep(7)
# 不指定不会出现惯性效果,如果指定可能会出现惯性效果
driver.swipe(start_x,start_y,end_x,end_y,500)
for i in range(10):
    driver.swipe(start_x, start_y, end_x, end_y)
    print(driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.hpbr.bosszhipin:id/tv_position_name")').text)
import time

from appium import webdriver

#准备自动化配置信息
from selenium.webdriver.common.by import By

desired_caps={
    #移动设备平台
    'platformName':'Android',
    #平台OS版本号,写整数位即可
    'plathformVersion':'8',
    #设备的名称--值可以随便写
    'deviceName':'test0106',
    #提供被测app的信息-包名,入口信息
    'appPackage':'com.hpbr.bosszhipin',
    'appActivity':'.module.launcher.WelcomeActivity',
    #确保自动化之后不重置app
    'noReset':True,
    #设置session的超时时间,单位秒
    'newCommandTimeout':6000
}
#初始化driver对象-用于控制手机
driver=webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)
driver.implicitly_wait(10)#稳定元素
time.sleep(3)
#打开通知
driver.open_notifications()
time.sleep(2)
#后退键,填入的是值,值在文档中
driver.press_keycode(4)
time.sleep(3)
#press_keycode可模拟键盘按键操作
上一篇 下一篇

猜你喜欢

热点阅读