android 如何等待移动的元素

kyxcudwk  于 2022-12-02  发布在  Android
关注(0)|答案(3)|浏览(165)

我想等待MobilElement,不确定什么是正确的方法。
使用Appium,Selenium和Java创建一些自动化测试,但运行时都使用模拟器,有时我需要等待一点元素,我想使用我在webElement中使用东西我使用按类查找原因是在应用程序中没有id,我不能更改它

public static MobileElement myButton(AndroidDriver driver, int index) {
    List<MobileElement> button = driver.findElements(By.className("android.widget.Button"));

    return  button.get(index);
}

模拟器对我来说很慢,所以我想使用waitForElement方法

bf1o4zei

bf1o4zei1#

你可以实现一个隐式等待。它基本上是在抛出一个异常之前等待一定的时间,该异常表示它在页面上找不到元素。

WebDriver driver => new FirefoxDriver();

 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

 driver.get("http://url_that_delays_loading");

 WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));

另一种方法是使用Expected Conditions wait来评估某个值既不是null也不是false。

WebDriverWait wait = new WebDriverWait(driver, 10);

 WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(>someid>)));

等待页面加载以设置在引发错误之前等待页面加载完成的时间量。如果超时为负,则页面加载可以是无限期的。

driver.manage().timeouts().pageLoadTimeout(100, SECONDS);

Thread.Sleep也可以使用,但它不是一个理想的等待声明。

thread.sleep(1000);
a9wyjsp7

a9wyjsp72#

更新

尝试使用visibilityOf,如下所示:

public static MobileElement myButton(AndroidDriver driver, int index) {
    List<MobileElement> button = driver.findElements(By.className("android.widget.Button"));

    //update here
    new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOf(button.get(index)));
    return  button.get(index);
}

30,以秒为单位。

vaqhlq81

vaqhlq813#

当然可以,只需更改以下行:

List<MobileElement> button = driver.findElements(By.className("android.widget.Button"));

假设WebDriverWait类用于显式等待实现:

List<MobileElement> button = new WebDriverWait(driver, 10)
        .until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.className("android.widget.Button")))
        .stream()
        .map(element -> (MobileElement) element)
        .collect(Collectors.toList());

接下来,您可以考虑实施Page Object Model设计模式并使用@AndroidFindBy注解,查看https://github.com/appium/java-client/blob/master/docs/Page-objects.md示例代码。

相关问题