Appium 执行 AndroidDriver.swipe 操作时失败

小小南瓜 · 2015年08月26日 · 最后由 duoduo 回复于 2015年12月18日 · 1584 次阅读

背景
目前所测的应用是公司的 app,启动 app 时会有 4 张引导图(需要向左滑动 4 次进入 app 的主界面才能进行下一步操作),其中在写的一段代码就是关于滑动的操作

MainActivityTest.java 中的代码


public void swipeToLeft( int during) 
{
    int width = driver.manage().window().getSize().width;
    int height = driver.manage().window().getSize().height;
    try {
        driver.swipe(width  / 2, height / 2, width / 4, height / 2, during); 
        System.out.println("swipe done");
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    System.out.println("swipetoleft");
}

@Test
public void mainActivity() throws Exception
{
    //等待引导图
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    swipeToLeft(200);
     driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    System.out.println("scroll done");

异常
运行代码时会抛出异常。日志与异常
org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 16 milliseconds
Build info: version: '2.47.1', revision: 'unknown', time: '2015-07-30 11:02:44'
System info: host: 'FX-JSJ337', ip: '192.168.131.199', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_45'
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities [{app=E:\phonesource\aphone\apps\aphone.apk, appPackage=com.test.video.mobile, networkConnectionEnabled=true, warnings={}, databaseEnabled=false, deviceName=Coolpad8675-0x02a108ba, platform=LINUX, appActivity=com.test.video.activity.StartActivity, desired={app=E:\phonesource\aphone\apps\aphone.apk, appPackage=com.test.video.mobile, appActivity=com.test.video.activity.StartActivity, newCommandTimeout=60, automationName=Appium, browserName=, platformName=Android, deviceName=Android}, newCommandTimeout=60, platformVersion=4.4.2, webStorageEnabled=false, locationContextEnabled=false, automationName=Appium, browserName=, takesScreenshot=true, javascriptEnabled=true, platformName=Android}]
Session ID: b3ba5f31-df33-4d03-8928-fb494be2c96c
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:595)
at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:27)
at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
at io.appium.java_client.AppiumDriver.performTouchAction(AppiumDriver.java:380)
at io.appium.java_client.TouchAction.perform(TouchAction.java:302)
at io.appium.java_client.AppiumDriver.swipe(AppiumDriver.java:435)
at test.aphone.MainActivityTest.scrollLoadingPic(MainActivityTest.java:88)
at test.aphone.MainActivityTest.mainActivity(MainActivityTest.java:102)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
scroll done

appium 部分日志

info: [debug] Set Android implicit wait to 3000ms
info: [debug] Responding to client with success: {"status":0,"value":null,"sessionId":"a776984b-a93a-4fbb-99ae-e69b785ab71a"}
info: <-- POST /wd/hub/session/a776984b-a93a-4fbb-99ae-e69b785ab71a/timeouts/implicit_wait 200 1.707 ms - 76 {"status":0,"value":null,"sessionId":"a776984b-a93a-4fbb-99ae-e69b785ab71a"}
info: --> GET /wd/hub/session/a776984b-a93a-4fbb-99ae-e69b785ab71a/window/current/size {}
info: [debug] Pushing command to appium work queue: ["getDeviceSize"]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"getDeviceSize","params":{}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: getDeviceSize
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":{"width":720,"height":1280},"status":0}
info: [debug] Responding to client with success: {"status":0,"value":{"width":720,"height":1280},"sessionId":"a776984b-a93a-4fbb-99ae-e69b785ab71a"}
info: <-- GET /wd/hub/session/a776984b-a93a-4fbb-99ae-e69b785ab71a/window/current/size 200 15.769 ms - 99 {"status":0,"value":{"width":720,"height":1280},"sessionId":"a776984b-a93a-4fbb-99ae-e69b785ab71a"}
info: --> GET /wd/hub/session/a776984b-a93a-4fbb-99ae-e69b785ab71a/window/current/size {}
info: [debug] Pushing command to appium work queue: ["getDeviceSize"]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"getDeviceSize","params":{}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: getDeviceSize
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":{"width":720,"height":1280},"status":0}
info: [debug] Responding to client with success: {"status":0,"value":{"width":720,"height":1280},"sessionId":"a776984b-a93a-4fbb-99ae-e69b785ab71a"}
info: <-- GET /wd/hub/session/a776984b-a93a-4fbb-99ae-e69b785ab71a/window/current/size 200 6.638 ms - 99 {"status":0,"value":{"width":720,"height":1280},"sessionId":"a776984b-a93a-4fbb-99ae-e69b785ab71a"}
info: --> POST /wd/hub/session/a776984b-a93a-4fbb-99ae-e69b785ab71a/touch/perform {"actions":[{"action":"press","options":{"x":180,"y":640}},{"action":"wait","options":{"ms":200}},{"action":"moveTo","options":{"x":360,"y":640}},{"action":"release","options":{}}]}
info: [debug] Pushing command to appium work queue: ["swipe",{"startX":180,"startY":640,"endX":360,"endY":640,"steps":6}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"swipe","params":{"startX":180,"startY":640,"endX":360,"endY":640,"steps":6}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: swipe
info: [debug] [BOOTSTRAP] [debug] Display bounds: [0,0][720,1280]
info: [debug] [BOOTSTRAP] [debug] Display bounds: [0,0][720,1280]
info: [debug] [BOOTSTRAP] [debug] Swiping from [x=180.0, y=640.0] to [x=360.0, y=640.0] with steps: 6
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":"The swipe did not complete successfully","status":13}
info: [debug] Responding to client with error: {"status":13,"value":{"message":"An unknown server-side error occurred while processing the command.","origValue":"The swipe did not complete successfully"},"sessionId":"a776984b-a93a-4fbb-99ae-e69b785ab71a"}
info: <-- POST /wd/hub/session/a776984b-a93a-4fbb-99ae-e69b785ab71a/touch/perform 500 14.197 ms - 208
info: --> DELETE /wd/hub/session/a776984b-a93a-4fbb-99ae-e69b785ab71a {}
info: Shutting down appium session
info: [debug] Pressing the HOME button
info: [debug] executing cmd: D:\Software\eclipse\sdk\platform-tools\adb.exe -s Coolpad8675-0x02a108ba shell "input keyevent 3"
info: [debug] Stopping logcat capture
info: [debug] Logcat terminated with code null, signal SIGTERM
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"shutdown"}
info: [debug] [BOOTSTRAP] [debug] Got command of type SHUTDOWN
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":"OK, shutting down","status":0}
info: [debug] [UIAUTOMATOR STDOUT] [CDS] close[4724]
info: [debug] [UIAUTOMATOR STDOUT] close [socket][/0.0.0.0:4724]
info: [debug] [BOOTSTRAP] [debug] Closed client connection
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: numtests=1
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=.
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: test=testRunServer
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: current=1
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: 0
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=
info: [debug] [UIAUTOMATOR STDOUT] Test results for WatcherResultPrinter=.
info: [debug] [UIAUTOMATOR STDOUT] Time: 5.544
info: [debug] [UIAUTOMATOR STDOUT] OK (1 test)
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: -1
info: [debug] Sent shutdown command, waiting for UiAutomator to stop...
info: [debug] UiAutomator shut down normally
info: [debug] Cleaning up android objects
info: [debug] Cleaning up appium session
info: [debug] Responding to client with success: {"status":0,"value":null,"sessionId":"a776984b-a93a-4fbb-99ae-e69b785ab71a"}
info: <-- DELETE /wd/hub/session/a776984b-a93a-4fbb-99ae-e69b785ab71a 200 903.671 ms - 76 {"status":0,"value":null,"sessionId":"a776984b-a93a-4fbb-99ae-e69b785ab71a"}

共收到 9 条回复 时间 点赞

/***

  • 左滑
  • @param 传入宽度的百分比 (1-99 之间) */ public static void slideLeft(int i){ Assert.assertFalse("左滑宽度传入错误", i<=0||i>=100); int x=.driver.manage().window().getSize().width; int y=driver.manage().window().getSize().height; driver.swipe(x/4*3, y/10*i, x/4*2, y/10*i, 0); }

@testly driver.swipe(x/43, y/10*i, x/4*2, y/10*i, 0); 这行代码我也试过了,还是跟帖子里面的异常一样
不过调试的时候倒是可以滑动,但是执行完 swipe 后一样抛出异常

#2 楼 @best1196

Returning result: {"value":"The swipe did not complete successfully","status":13}

这个结果实际上 appium 调用了 UiAutomator 的 swipe 后返回的结果来判断的,你可以尝试下直接使用 UiAutomator 来看看返回是 true 还是 false

info: [debug] [BOOTSTRAP] [debug] Swiping from [x=180.0, y=640.0] to [x=360.0, y=640.0] with steps: 6
虽然不知道为啥,不过这个这么看都是向右滑动吧

@eurekasaber 说的对, 这个坐标应该是某次我把坐标改错了

6楼 已删除
7楼 已删除

最近也在使用 swipe,偶尔也会遇到这种情况,也许你可以试下我解决的方法,在调用 swipe 前先确认这个引导图在不在,或者弄一个固定的等待时间。

@caikaibai 多谢指点。这个我也试过,让它等待一段时间,但是加个等待之后一闪而过,打出的日志显示 swipe 操作是失败的,报错信息就是上面贴出来的东西,所以我想是哪个地方出错了,但是也搞不明白

@best1196 这个问题解决了吗?我也遇到了一样的问题……

driver.swipe(width / 2, height / 2, width / 4, height / 2, during);这个能滑过去吗?我是这样写的,滑两次,从右往左滑
public void swipeToLeft(AndroidDriver driver, int during) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
driver.swipe(width * 8/9, height / 2, width /9, height / 2, during);
}

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册