Appium 【已解决】官方的 java examples 中 scroll_to 方法报错

学飞 · 2014年05月16日 · 最后由 学飞 回复于 2014年05月20日 · 2061 次阅读
本帖已被设为精华帖!

java 例子 MobileFindJavaTest.java 运行时报错,出错部分

public String scroll_to(String text) {

    text = text.replaceAll("\"", "\\\""); // quotes must be escaped.
    final String[] jsonString = {"\"scroll\"","[[3,\"" + text + "\"]]","[[7,\"" + text + "\"]]"};


    return driver.complexFind(jsonString);

  }

错误

info: Pushing command to appium work queue: ["find",{"strategy":"dynamic","selector":["\"scroll\"","[[3,\"About phone\"]]","[[7,\"About phone\"]]"],"context":"","multiple":false}]
info: [BOOTSTRAP] [info] Got data from client: {"cmd":"action","action":"find","params":{"strategy":
"dynamic","selector":["\"scroll\"","[[3,\"About phone\"]]","[[7,\"About phone\"]]"],"context":"","multiple":false}}
info: [BOOTSTRAP] [info] Got command of type ACTION
info: [BOOTSTRAP] [debug] Got command action: find
info: [BOOTSTRAP] [debug] Finding dynamic.
info: [BOOTSTRAP] [debug] Returning all? false
info: [BOOTSTRAP] [debug] Scrolling? false
info: [BOOTSTRAP] [debug] ["\"scroll\"","[[3,\"About phone\"]]","[[7,\"About phone\"]]"]
info: [BOOTSTRAP] [debug] Parsing selector 0
info: [BOOTSTRAP] [info] Returning result: {"value":"java.lang.String cannot be cast to org.json.JSONArray","status":13}
info: Pushing command to appium work queue: ["find",{"strategy":"dynamic","selector":["\"scroll\"","[[3,\"About phone\"]]","[[7,\"About phone\"]]"],"context":"","multiple":false}]
info: [BOOTSTRAP] [info] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"dynamic","selector":["\"scroll\"","[[3,\"About phone\"]]","[[7,\"About phone\"]]"],"context":"","mu
ltiple":false}}
info: [BOOTSTRAP] [info] Got command of type ACTION
info: [BOOTSTRAP] [debug] Got command action: find
info: Responding to client with error: {"status":13,"value":{"message":"An unknown server-side error occurred while processing the command.","origValue":"java.lang.String cannot be cast to org.json.JSONArray"},"sessionId":"e9328838-a82c-46c8-a0f2-6c3b2ba70a2f"}
POST /wd/hub/session/e9328838-a82c-46c8-a0f2-6c3b2ba70a2f/appium/app/complex_find 500 20379ms - 250b
共收到 1 条回复 时间 点赞

java-client 1.2.0 给出了 scrollTo(String text) 和 scrollToExact(String text) 2 个方法,前者是只要包含即可,后者要完全一致。顺便说下,加入等待时间是有必要的,特别是在比较卡的设备或者模拟器上,修改后的官方代码如下:

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.URL;
import java.util.concurrent.TimeUnit;

public class MobileFindJavaTest {
    private AppiumDriver            driver;
      private static final String     url    = "http://127.0.0.1:4723/wd/hub";

      @Test
      public void apiDemo() throws Exception {
        //scrollTo 是只要包含string即可
        final MobileElement about_phone = driver.scrollTo("About phone");
        if (about_phone != null) {
          System.out.println("scrolled to: aboutPhone");
          System.out.println("returned: " + about_phone.getText());
        }
        //scrollToExact要与查找的内容完全一致
        driver.scrollToExact("Bluetooth");
      }



      @Before
      public void setUp() throws Exception {
        final DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
        capabilities.setCapability("deviceName", "Nexus 4");
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("appPackage", "com.android.settings");
        capabilities.setCapability("appActivity", ".Settings");
        capabilities.setCapability("platformVersion", "4.4.2");
        driver = new AppiumDriver(new URL(url), capabilities);
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
      }

      @After
      public void tearDown() throws Exception {
        driver.quit();
      }
}
需要 登录 後方可回應,如果你還沒有帳號按這裡 注册