我怎样才能使用等待条件?

时间:2015-01-12 12:54:14

标签: java selendroid

首先我上传了一个图像,然后我断言检查我的图像是否在同一个程序本身中被上传。对于该断言,我已经定义了以下编码,但是它会抛出以下错误消息:Failed tests:Timed 10秒后等待出现位于以下位置的元素:By.id:brand。如何动态上传并检查上传的图像是否存在。

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());

1 个答案:

答案 0 :(得分:0)

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

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();