Selenium 有条件等待中间动作
Selenium conditional wait with intermediate action
我正在尝试在 Selenium 中执行一个操作,它将等待状态更改为特定的“完成”状态。
从概念上讲,它可以用伪代码布局如下:
public boolean waitForActionToComplete(long maxTimeoutInSeconds, int repeatTimeInSeconds, Callable<T> action, Callable<T> condition) {
long startTime = 0;
while (startTime < maxTimeoutInSeconds)
perform <action>; // e.g., click on a "Refresh" button to refresh the results
boolean done = verify <condition>; // e.g., check whether the job status is "Done"
if (done)
return true; // if done, then exit with TRUE
else
Thread.sleep(repeatTimeInSeconds);
end while;
return false; // status still not complete, timeout gracefully
}
使用 ExpectedCondition 和 WebdriverWait/FluentWait 可以通过某种简单的方式实现此方法。但是,由于框架的某些限制,我不能完全实现和使用这样的方法。上面的方法必须这样实现(在具有此方法签名的框架中实现一个接口):
public void execute(final WebDriver webDriver, String... parameters) {
// implementation here
}
谁能告诉我如何将方法转换成上面指定的形式?
我正在尝试在 Selenium 中执行一个操作,它将等待状态更改为特定的“完成”状态。
这可以简单地使用 WebDriverWait
和 ExpectedConditions.textToBePresentInElementLocated
而不是创建自己的自定义方法 waitForActionToComplete
来实现,如下所示:-
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.textToBePresentInElementLocated(byObject, "Finished"));
希望对您有所帮助...:)
我正在尝试在 Selenium 中执行一个操作,它将等待状态更改为特定的“完成”状态。
从概念上讲,它可以用伪代码布局如下:
public boolean waitForActionToComplete(long maxTimeoutInSeconds, int repeatTimeInSeconds, Callable<T> action, Callable<T> condition) {
long startTime = 0;
while (startTime < maxTimeoutInSeconds)
perform <action>; // e.g., click on a "Refresh" button to refresh the results
boolean done = verify <condition>; // e.g., check whether the job status is "Done"
if (done)
return true; // if done, then exit with TRUE
else
Thread.sleep(repeatTimeInSeconds);
end while;
return false; // status still not complete, timeout gracefully
}
使用 ExpectedCondition 和 WebdriverWait/FluentWait 可以通过某种简单的方式实现此方法。但是,由于框架的某些限制,我不能完全实现和使用这样的方法。上面的方法必须这样实现(在具有此方法签名的框架中实现一个接口):
public void execute(final WebDriver webDriver, String... parameters) {
// implementation here
}
谁能告诉我如何将方法转换成上面指定的形式?
我正在尝试在 Selenium 中执行一个操作,它将等待状态更改为特定的“完成”状态。
这可以简单地使用 WebDriverWait
和 ExpectedConditions.textToBePresentInElementLocated
而不是创建自己的自定义方法 waitForActionToComplete
来实现,如下所示:-
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.textToBePresentInElementLocated(byObject, "Finished"));
希望对您有所帮助...:)