使用Appium

时间:2016-05-05 17:36:11

标签: java android scroll appium

对于我的原生Android应用程序,我在过去的两周内一直试图让appium向下滚动我的原生应用程序。我试过driver.scrollTo("Accounts"); 然后我收到了这个错误

[org.openqa.selenium.WebDriverException: CATCH_ALL: io.selendroid.server.common.exceptions.SelendroidException: method (by) not found: -android uiautomator

以及我找到的许多其他例子。似乎没什么用。这是我尝试过的最新例子。

使用appium 1.5.2和appium java客户端版本:'3.3.0'。当我尝试运行以下代码时。

    TouchAction tAction=new TouchAction(driver);
    int startx = driver.findElement(By.id("line_chart_line_chart")).getLocation().getX();
    int starty = driver.findElement(By.id("line_chart_line_chart")).getLocation().getY();
    int endx = driver.findElement(By.id("actionBarLogo")).getLocation().getX();
    int endy = driver.findElement(By.id("actionBarLogo")).getLocation().getY();
    System.out.println(startx + " ::::::: " + starty + " ::::::: " + endx +  " ::::::: " +  endy);
    //  This is what the console printed out  startX=560 ::::::: starty=1420 ::::::: endx=560 ::::::: endy=240  
    //First tap on the screen and swipe up using moveTo function
    tAction.press(startx,starty).moveTo(endx,endy).release().perform();

然后我收到此错误消息

org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. Original error: Could not proxy. Proxy 
error: Could not proxy command to remote server. Original error: 404 - undefined (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 27 milliseconds

我不知道该怎么做。为了点击一个元素,它必须在屏幕上可见。为了使这个元素出现在屏幕上,我需要向下滚动到它。

有什么我做错了吗???我似乎无法弄明白。

3 个答案:

答案 0 :(得分:1)

您使用的是Selendroid模式,它不支持UiAutomator By方法。 您可以通过将所需的功能automationName设置为Appium

,将您的appium运行模式更改为UiAutomator

答案 1 :(得分:0)

这个函数对我来说很有用,在你的情况下调用它时,为elementName输入“Accounts”。

public static void scrollToElementAndroid(AndroidDriver driver, String elementName, boolean scrollDown) {
    String listID = ((RemoteWebElement) driver.findElementByAndroidUIAutomator("new UiSelector().className(android.widget.ListView)")).getId();
    String direction;
    if (scrollDown) {
        direction = "down";
    } else {
        direction = "up";
    }
    HashMap<String, String> scrollObject = new HashMap<String, String>();
    scrollObject.put("direction", direction);
    scrollObject.put("element", listID);
    scrollObject.put("text", elementName);
    driver.executeScript("mobile: scrollTo", scrollObject);
}

答案 2 :(得分:0)

使用如下代码:

Dimension size = driver.manage().window().getSize();
int x = size.width / 2;
int endy = (int) (size.height * 0.75);
int starty = (int) (size.height * 0.20);
driver.swipe(x, starty, x, endy, 1000);