Appium [已解决] Appium 测试 app 引导页滑屏,报错 An unknown server-side error occurred while processing the command.

shela2009 · 2016年02月17日 · 最后由 在路上 回复于 2016年04月22日 · 2633 次阅读

之前使用 Robotium,现在学习 Appium。结果马上被 app 的启动页面卡住了。

前提:

公司产品,安装后启动会有一个四屏的引导页面,左右切换。最后一页可以使用点击或者继续左滑,然后进入首页。
以下是第一张引导页的 uiautomator 的截图:

做法:

setup() 和 tearDown() 就不展示了,直接展示 test 函数

@Test
public void enterCoursesList() throws InterruptedException{

    swipeToLeft(driver, 1000);
    Thread.sleep(1000);
    swipeToLeft(driver, 1000);
    Thread.sleep(1000);
    swipeToLeft(driver, 1000);
    Thread.sleep(1000);
    swipeToLeft(driver, 1000);
    Thread.sleep(1000);

    //进入首页
    Assert.assertEquals("com.fasthand.main.MainTabelActivity", driver.currentActivity());
    WebElement el = driver.findElement(By.name("找机构"));
    el.click();

}

//向左滑屏
public void swipeToLeft(AndroidDriver driver, int time) {
    int width = driver.manage().window().getSize().width;
    int height = driver.manage().window().getSize().height;
    driver.swipe(width * 3 / 4, height / 2, width / 4, height / 2, time);
}

运行结果

运行后,app 启动,然后就出错了。
附上 server log:

info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":0,"value":{"height":1776,"width":1080}}
info: --> POST /wd/hub/session/e1c3a393-672c-49b0-b4b1-8ceff1c39e53/touch/perform {"actions":[{"action":"press","options":{"x":810,"y":888}},{"action":"wait","options":
{"ms":1000}},{"action":"moveTo","options":{"x":270,"y":888}},{"action":"release","options":{}}]}
info: [debug] Pushing command to appium work queue: ["swipe",{"startX":810,"startY":888,"endX":270,"endY":888,"steps":28}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"swipe","params":{"startX":810,"startY":888,"endX":270,"endY":888,"steps":28}}
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][1080,1776]
info: [debug] [BOOTSTRAP] [debug] Display bounds: [0,0][1080,1776]
info: [debug] [BOOTSTRAP] [debug] Swiping from [x=810.0, y=888.0] to [x=270.0, y=888.0] with steps: 28
info: [debug] Responding to client with error: {"status":13,"value":{"message":"An unknown server-side error occurred while processing the command.","origValue":"The sw
ipe did not complete successfully"},"sessionId":"e1c3a393-672c-49b0-b4b1-8ceff1c39e53"}
info: <-- POST /wd/hub/session/e1c3a393-672c-49b0-b4b1-8ceff1c39e53/touch/perform 500 24.248 ms - 208
info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":13,"value":"The swipe did not complete successfully"}
info: --> DELETE /wd/hub/session/e1c3a393-672c-49b0-b4b1-8ceff1c39e53 {}
info: Shutting down appium session

有童鞋遇到和我一样的情况吗?去网上查也没看到对这个报错的具体解释。
另外,我感觉这样做滑屏是没问题的,也不知道为什么总是报错。有童鞋也遇到过引导页滑屏的情况吗?可以提供下解决方法吗?

——————————————————————分隔线————————————————————————

解决

因为报错的时机在启动而不是滑屏,于是使用 try-catch 试了下。

/**
 *  Swipe to left on the screen
 */
public void swipeToLeft(AndroidDriver driver, int time) {
    int width = driver.manage().window().getSize().width;
    int height = driver.manage().window().getSize().height;
    try {
        driver.swipe(width * 3 / 4, height / 2, width / 4, height / 2, time);
    } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
  }
}

这样做之后,再次启动。在 app 启动页面时还是会报同样的错,但 test 没有停止,继续进行就会开始滑屏。
也就是说,滑屏部分应该没有问题。那为什么在启动时报错?报错的时候引导页还没有加载出来。
于是在执行滑屏之前先加了延迟,给了启动页的时间。

@Test
public void enterCoursesList() throws InterruptedException{
    Thread.sleep(5000);
    Assert.assertEquals("com.moduleLogin.welcome.GuidePageActivity", driver.currentActivity());
    swipeToLeft(driver, 1000);
    Thread.sleep(2000);
    swipeToLeft(driver, 1000);
    Thread.sleep(2000);
    swipeToLeft(driver, 1000);
    Thread.sleep(2000);
    WebElement btElement = driver.findElementByClassName("android.widget.Button");
    btElement.click();
    Thread.sleep(5000);

    //进入首页
    Assert.assertEquals("com.fasthand.main.MainTabelActivity", driver.currentActivity());
    WebElement el = driver.findElement(By.name("找机构"));
    el.click();
    Thread.sleep(5000);
}

这样做之后,再次启动,成功!
多谢各位的回答!我开始以为是 swipe 写错的原因,结果是没有给启动足够的时间,非常麻烦诸位!

共收到 20 条回复 时间 点赞

@blue_momo2009 查找元素等待时间不要用 sleep,用 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);这样的来控制比较灵活

不错的案例!建议最好不要直接 sleep 固定的时间,通过 WebDriverWait 来等待一个元素的出现比较好,给个超时。diggerplus 上有篇相关的指导可以看下:http://www.diggerplus.org/archives/3101

我也在第一个滑屏之前加了 sleep 但还是不行,新手第一步就被卡了

#16 楼 @g3615471 额,看起来好像是 app 没有配置?


菜鸟,请问这个是怎么回事?

一样的问题,哎 想想自己太二了

#3 楼 @greenplum 恩恩,我开始把 swipe 后的时间加长了,结果应该在第一个滑屏之前就加一个 sleep

#4 楼 @chenhengjie123 已经找到问题的原因,是启动页没有足够的加载时间,导致还没启动完就执行 swipe。那这样我的 swipe 写的没问题,那出现偏移量的情况是怎么回事啊?

#8 楼 @zsx10110 我理解错了,以为你说的是 swipe 后的时间,加大后的确不行。因为错误出现在第一次滑屏之前,启动页没有加载完就滑屏,导致出错。多谢多谢!是我理解错了

#10 楼 @sanlengjingvv 不是,app 一启动就报错了。刚才用 try-catch 试了下,发现是启动时报错。跳过这个报错,滑屏就可以了

只在第四个引导屏报错吧?
即使报错也进入了首页?

#8 楼 @zsx10110 试过了,加了 10 倍,还是不行

#7 楼 @blue_momo2009 我遇到一样的错误是当时前面一个操作完以后立马 swipe 出问题的,解决方法就是加大 sleep 的时间

#5 楼 @kilmer 但是 server log 里的坐标是正确的,没有超出限制啊

#4 楼 @chenhengjie123 今天又运行了一遍,第一次成功了,第二次又出现这个报错。但是我看 server log 里也没显示坐标超限啊。而且 swip 的终点坐标也没有计算成偏移量,的确是终点坐标。
问题里已经附上了 server log

这个问题 我之前遇到过一次 .

我当时出现这个错误的原因是,滑动的坐标范围超出了对应控件的 bounds

#2 楼 @blue_momo2009 看了下 java-client API 文档,貌似没有提供 swipe 结束点为 element 的方法。

那个帖子的意思是当 swipe 结束点传的是坐标时,appium server 会把它识别为是 offset(偏移量)。例如 swipe(50,50,60,60,500),实际上 appium 认为起点坐标是 50,50,终点坐标是:50+60,50+60 。

不过那个 issue 后面又有人说 android 上不是这么做的,我也不知道是后面 appium 改了还是 bootstrap 有另外做处理。

另外,建议你把 server log 附上来,看看 server 发给 bootstrap 的坐标是什么,有没有超出屏幕范围什么的。

time 时间设置大点

#1 楼 @eurekasaber 额。。没看懂,我只是看有人解释了下为什么会出现这种情况,但是不知道这个 elementId 是什么?怎么加啊?

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