Android + Appium:在ListView内滚动到特定位置,然后单击

时间:2018-07-12 23:22:30

标签: java android listview appium

有人可以帮我吗?

任务:在Android App中打开ListView元素,滚动到特定位置并单击它。如果未找到element,则滚动到列表底部,然后停止搜索并放下异常测试。 app screenshot with structure

//是的,我一直在寻找其他问题的解决方案,但我无法结合使用以用于我的情况。

我得到了什么:

1)测试在ListView内部滑动到特定位置并单击它;

2)在ListView底部循环测试堆栈。

问题:

1)如果到达ListView的底部时未找到特定位置,如何异常停止测试?

2)刷卡似乎是不正确的解决方案,似乎必须使用其他解决方案,否则否?

代码:

public void scrollToElementFromList (String keyword_locator){
// keyword_locator = (By.xpath("//*[@resource-id = 'android:id/text1'][@text = 'Spain']"))
    boolean token = false;
    while(!token) {
        if (this.isElementPresent(keyword_locator)){
            waitForElementAndClick(keyword_locator,"Cannot click selected element",3);
            token = true;
        } else {
            TouchAction action = new TouchAction(driver);
            WebElement element = driver.findElement(By.xpath("//android.widget.ListView"));
            int middleX = element.getLocation().getX() + element.getSize().getWidth() / 2;
            int upperY = element.getLocation().getY();
            int lowerY = upperY + element.getSize().getHeight() - 50;
            action.press(middleX, lowerY).waitAction(1200).moveTo(middleX, upperY).release().perform();
            continue;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

answer:

public void scrollToElementFromList (String locator, String keyword_locator, int max_swipes){
    By by = this.getLocatorByString(locator);
    boolean element_found = false;
    int already_swiped = 0;
    while(!element_found) {
        if (this.isElementPresent(keyword_locator)){
            waitForElementAndClick(keyword_locator,"Cannot click selected element",3);
            element_found = true;
        } else if (already_swiped > max_swipes){
            throw new AssertionError("Cannot find element by swiping up.");
        }
        else {
            TouchAction action = new TouchAction(driver);
            WebElement element = driver.findElement(by);
            int middleX = element.getLocation().getX() + element.getSize().getWidth() / 2;
            int upperY = element.getLocation().getY();
            int lowerY = upperY + element.getSize().getHeight() - 50;
            action.press(middleX, lowerY).waitAction(1200).moveTo(middleX, upperY).release().perform();
            ++already_swiped;
            continue;
        }
    }
}