Java 如何使用等待条件?

Java 如何使用等待条件?,java,selendroid,Java,Selendroid,首先我上传了一张图片,然后我做了一个断言来检查我的图片是否上传到同一个程序中。对于这个断言,我定义了以下编码,但它会将以下错误消息作为失败的测试抛出:等待位于by:by.id:brand的元素出现10秒后超时。如何动态上载并检查上载的图像是否存在 WebDriverWait wdw = new WebDriverWait(driver, 40); ExpectedCondition<Boolean> condition = new ExpectedCondition<Boole

首先我上传了一张图片,然后我做了一个断言来检查我的图片是否上传到同一个程序中。对于这个断言,我定义了以下编码,但它会将以下错误消息作为失败的测试抛出:等待位于by:by.id:brand的元素出现10秒后超时。如何动态上载并检查上载的图像是否存在

WebDriverWait wdw = new WebDriverWait(driver, 40);
ExpectedCondition<Boolean> condition = new ExpectedCondition<Boolean>() {
    public Boolean apply(WebDriver d) {
        WebElement imageIcon = d.findElement(By.id("brand"));
        return "Image".equals(imageIcon.isDisplayed());
    }
};
wdw.until(condition);

//Check if Image has been uploaded
WebElement imageIcon = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("brand")));
Assert.assertTrue(imageIcon.isDisplayed());

您可以启动一个新线程并在其中运行计时器。 这样做很容易,但可能不是最好的解决方案

long timestamp = System.currentTimeMillis();
new Thread(new Runnable() {
    public void run() {
    if(System.currentTimeMillis()-timestamp > 10000){
        //The 10 seconds passed. You can have some message in here.
        //Also a boolean to keep track if it was successful.
        return;//stops the thread
    }
}).start();