簡體   English   中英

Android + Appium:在ListView內滾動到特定位置,然后單擊

[英]Android + Appium: Scroll inside ListView to specific position and click on it

有人可以幫我嗎

任務:在Android App中打開ListView元素,滾動到特定位置並單擊它。 如果未找到element,則滾動到列表底部,然后停止搜索並放下異常測試。 具有結構的應用屏幕截圖

//是的,我一直在尋找其他問題的解決方案,但是我無法結合使用。

我得到了什么:

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;
        }
    }
}

回答:

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;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM