appium1.8版本在android應用上實現滑屏swipe

appium1.8版本在android應用上實現滑屏swipe

step1

  1. 上網找使用方法。
    網上提供的方法大多如下:

獲得機器屏幕大小x,y

def getSize(self):
    x = self.driver.get_window_size()['width']
    y = self.driver.get_window_size()['height']
    return (x, y)

# 屏幕向上滑動
def swipeUp(self, t = 5000):
    l = self.getSize()
    x1 = int(l[0] * 0.5)  # x座標
    y1 = int(l[1] * 0.75)  # 起始y座標
    y2 = int(l[1] * 0.25)  # 終點y座標
    self.driver.swipe(x1, y1, x1, y2, t)
    #self.driver.swipe(x1, y1, x1, y2)

does not work!!!

  1. 看報錯日誌。
    python 調用端報錯爲:
    File “/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py”, line 242, in check_response
    raise exception_class(message, screen, stacktrace)
    selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: The swipe did not complete successfully

appium server端報錯爲:
[debug] [AndroidBootstrap] Sending command to android: {“cmd”:“action”,“action”:“swipe”,“params”:{“startX”:720,“startY”:1920,“endX”:720,“endY”:640,“steps”:11}}
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {“cmd”:“action”,“action”:“swipe”,“params”:{“startX”:720,“startY”:1920,“endX”:720,“endY”:640,“steps”:11}}
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: swipe
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Display bounds: [0,0][1440,2560]
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Display bounds: [0,0][1440,2560]
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Swiping from [x=720.0, y=1920.0] to [x=720.0, y=640.0] with steps: 11
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {“status”:13,“value”:“The swipe did not complete successfully”}
[debug] [AndroidBootstrap] Received command result from bootstrap
[debug] [MJSONWP] Matched JSONWP error code 13 to UnknownError

  1. 網上找到新的使用方法。
    https://testerhome.com/topics/1550
    native mobile command的使用方法
    self.driver.swipe(75,500,75,0,500)

python端報錯不允許使用mobile方法
最後通過appium重啓添加參數–relaxed-security

==> 再報錯

找到appium的文檔裏關於native mobile command的文檔
http://appium.io/docs/en/commands/mobile-command/#android-uiautomator2-only
發現裏面對於android並沒有使用modile:swipe,這個是ios使用的。android的只有modile:shell。

=> 嘗試使用adb 命令來實現滑屏
self.driver.execute_script(“mobile:shell”, int(self.x / 2), int(self.y / 2), int(self.x / 2), int(self.y / 4), duration)

==> 再報錯 Error: The ‘command’ argument is mandatory’
https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/android/android-shell.md
官方使用文檔。
最後修改成
def swipe_up_half(self, count=1, duration=500):
for i in range(count):
self.driver.execute_script(self.cmdMobileShell, {
‘command’: ‘input’,
‘args’: [‘swipe’, int(self.x / 2), int(self.y * 3/ 4), int(self.x / 2), int(self.y / 4), duration],
‘includeStderr’: True,
‘timeout’: 5000
})

It work!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章