package io.tromba.testdriver;

import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

/**
 * Created by jao on 25/09/15.
 */
public class TestdriverWait extends WebDriverWait {

    public TestdriverWait(WebDriver driver, long timeOutInSeconds) {
        super(driver, timeOutInSeconds);
    }

    public void forElementVisible(WebElement element) {
        until(ExpectedConditions.visibilityOf(element));
    }

    public void forElementNotStale(final WebElement element) {
        until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver webDriver) {
                try {
                    element.isDisplayed();
                    return true;
                } catch (StaleElementReferenceException ex) {
                    return false;
                }
            }
        });
    }

    private ExpectedCondition<Boolean> not(final ExpectedCondition<?> toInvert) {
        return new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver driver) {
                try {
                    Object result = toInvert.apply(driver);
                    return (result == null || result == Boolean.FALSE);
                } catch (Exception e) {
                    return true;
                }
            }
        };
    }
}

@@ -341,33 +341,32 @@
  * C
-reated by jao on 25/09/15
+lass for wait conditions
 .%0A *
@@ -1530,11 +1530,264 @@
 %7D;%0A    %7D
+%0A%0A    // this should only be used for debugging purposes%0A    @Deprecated%0A    public void sleep(int timeInMillis) %7B%0A        try %7B%0A            Thread.sleep(timeInMillis);%0A        %7D catch (InterruptedException e) %7B%0A            // do nothing%0A        %7D%0A    %7D
 %0A%7D%0A

add wait