|
1 | 1 | package pages; |
2 | 2 |
|
| 3 | +import com.google.common.base.Preconditions; |
3 | 4 | import org.openqa.selenium.By; |
4 | 5 | import org.openqa.selenium.NoSuchElementException; |
5 | 6 | import org.openqa.selenium.WebDriver; |
| 7 | +import org.openqa.selenium.support.ui.ExpectedCondition; |
| 8 | +import org.openqa.selenium.support.ui.WebDriverWait; |
| 9 | + |
| 10 | +import java.util.concurrent.TimeUnit; |
6 | 11 |
|
7 | 12 | public abstract class HelperBase { |
8 | 13 | protected WebDriver driver; |
@@ -31,4 +36,32 @@ protected boolean isElementPresent(By locator) { |
31 | 36 | return false; |
32 | 37 | } |
33 | 38 | } |
| 39 | + |
| 40 | + public boolean explicitWait(final ExpectedCondition<?> condition, long maxCheckTimeInSeconds, long millisecondsBetweenChecks) { |
| 41 | + Preconditions.checkNotNull(condition, "Condition must be not null"); |
| 42 | + Preconditions.checkArgument(TimeUnit.MINUTES.toSeconds(3) > maxCheckTimeInSeconds, "Max check time in seconds should be less than 3 minutes"); |
| 43 | + checkConditionTimeouts(maxCheckTimeInSeconds, millisecondsBetweenChecks); |
| 44 | + try { |
| 45 | + driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); |
| 46 | + WebDriverWait explicitWait = new WebDriverWait(driver, maxCheckTimeInSeconds, millisecondsBetweenChecks); |
| 47 | + explicitWait.until(condition); |
| 48 | + return true; |
| 49 | + } catch (Exception e) { |
| 50 | + return false; |
| 51 | + } finally { |
| 52 | + if (driver != null) { |
| 53 | + driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS); |
| 54 | + } else { |
| 55 | + throw new IllegalArgumentException("Driver shouldnt be null"); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private void checkConditionTimeouts(long maxCheckTimeInSeconds, long millisecondsBetweenChecks) { |
| 61 | + Preconditions.checkState(maxCheckTimeInSeconds > 0, "maximum check time in seconds must be not 0"); |
| 62 | + Preconditions.checkState(millisecondsBetweenChecks > 0, "milliseconds count between checks must be not 0"); |
| 63 | + Preconditions.checkState(millisecondsBetweenChecks < (maxCheckTimeInSeconds * 1000), |
| 64 | + "Millis between checks must be less than max seconds to wait"); |
| 65 | + } |
| 66 | + |
34 | 67 | } |
0 commit comments