之前使用 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 写错的原因,结果是没有给启动足够的时间,非常麻烦诸位!


↙↙↙阅读原文可查看相关链接,并与作者交流