1

我正在 android 设备上执行我的脚本。如果应用程序不支持隐式等待显式等待,我该如何等待?我Thread.sleep()在我的情况下使用。谁能给我一个解决方案?

4

2 回答 2

2

显式等待不是应用程序的功能,它是Appium引擎盖下的Selenium的功能

在此处输入图像描述

因此,您通常应该能够使用WebDriverWaitExpectedConditions类,有关概念和代码片段的更多信息,请参阅如何使用 Selenium 使用 AJAX 技术测试 Web 应用程序:

WebDriverWait wait = new WebDriverWait(driver, 10);
MobileElement company = (MobileElement) wait
        .until(ExpectedConditions
                .elementToBeClickable(By
                        .id("usernameTextField")));

带有必要导入语句的完整代码以防万一:

package com.example;

import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.net.URL;


public class AppiumTest {

    public static void main(String[] args) throws Exception {

        DesiredCapabilities dc = new DesiredCapabilities();
        dc.setCapability(MobileCapabilityType.APP, "http://d242m5chux1g9j.cloudfront.net/eribank.apk");
        dc.setCapability("platformName", "Android");
        dc.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");
        dc.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "com.experitest.ExperiBank");
        dc.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, ".LoginActivity");
        dc.setCapability(MobileCapabilityType.DEVICE_NAME, "emulator-5554");
        dc.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 300);

        AndroidDriver driver = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), dc);
        WebDriverWait wait = new WebDriverWait(driver, 10);
        MobileElement company = (MobileElement) wait
                .until(ExpectedConditions
                        .elementToBeClickable(By
                                .id("usernameTextField")));
        company.sendKeys("company");

        driver.quit();
    }
}
于 2019-07-14T09:18:07.940 回答
0

这是一个框架功能,与您运行它的应用程序无关。等待需要你告诉它“它应该等待多长时间?” 以毫秒为单位,它应该等待什么?

#Time input
WebDriverWait wait = new WebDriverWait(driver, 50000);
#What to wait for
wait.until(ExpectedConditions.elementToBeClickable(By.id("usernameTextField")));
于 2019-07-15T11:04:08.160 回答