python+appium自动化测试-07触控

告别诺基亚时代,现在满大街都是触屏智能机。触屏机释放了物理键盘,通过手势动作便能达到操作手机功能的目的。所以要进行APP自动化,模拟触控操作是必要的。

1、左右滑动swipe(int x1,int y1,int x2,int y1, Duration duration)

x = driver.get_window_size()['width']#获取手机宽度
y = driver.get_window_size()['height']#获取手机高度
x1 = int(x*0.9)#开始滑动的坐标x1
y1=int(y*0.5)#开始滑动的坐标y1
x2=int(x*0.1)#结束点坐标x2
driver.swipe(x1,y1,x2,y1,1000)#1000为滑动时间(默认5毫秒)

2、按压某个位置press(WebElement el, int x, int y)

press() 开始按压一个元素或坐标点(x,y)。通过手指按压手机屏幕的某个位置。类似于点击。

TouchAction(driver).press(x=0,y=308)

3、长按long_press(WebElement el, int x, int y, Duration duration)

longPress() 长时间按压一个元素或坐标点(x,y)。 相比press()方法,longPress()多了一个入参duration,以毫秒为单位。其用法与press()方法相同。

action = TouchAction(driver)
action.longPress(names.get(200),1000)

4、点击tap(WebElement el, int x, int y)

tap() 对一个元素或控件执行点击操作。

action = TouchAction(driver)
element = driver.find_element_by_id(‘android:id/button2’)
action.tap(element,200 ,200)

5、移动moveTo(WebElement el, int x, int y)

move_to() 将指针(光标)从上一个点移动到指定的元素或点。
注意:移动到目标位置有时是算绝对坐标点,有时是基于前面一个坐标点的偏移量,这个要结合具体App来实践。

action = TouchAction(driver)
element = driver.find_element_by_id(‘android:id/button2’)
action.move_to(element,200 ,200)

6、暂停wait(Duration duration)

暂停脚本的执行,单位为毫秒。

wait(2000)

7、释放release()

release() 结束的行动取消屏幕上的指针。

TouchAction(driver).press(x=0,y=308).release()

8、执行perform()

perform() 执行的操作发送到服务器的命令操作。

TouchAction(driver).press(x=0,y=308).release().perform()

9、多点触控

多点触控一般用于地图的缩放,需要用到MultiAction类。

from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.common.multi_action import MultiAction

x=driver.get_window_size()['width']
y=driver.get_window_size()['height']

#缩小
def pinch():
    action1=TouchAction(driver)
    action2=TouchAction(driver)
    zoom_action=MultiAction(driver)

    sleep(10)
    action1.press(x=x*0.2,y=y*0.2).wait(1000).move_to(x=x*0.4,y=y*0.4).wait(1000).release()
    action2.press(x=x*0.8,y=y*0.8).wait(1000).move_to(x=x*0.6,y=y*0.6).wait(1000).release()

    print('start pinch...')
    zoom_action.add(action1,action2)
    zoom_action.perform()
    
#放大
def zoom():
    action1=TouchAction(driver)
    action2=TouchAction(driver)
    zoom_action=MultiAction(driver)

    action1.press(x=x*0.4,y=y*0.4).wait(1000).move_to(x=x*0.2,y=y*0.2).wait(1000).release()
    action2.press(x=x*0.6,y=y*0.6).wait(1000).move_to(x=x*0.8,y=y*0.8).wait(1000).release()

    zoom_action.add(action1,action2)
    zoom_action.perform()

发布了48 篇原创文章 · 获赞 15 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_24601279/article/details/104066475