package org.fundacionjala.automation.framework.utils.common;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.WebDriverWait;

public class ExplicitWait {

    public static void clickWhenReady(By locator, int timeout) {
	WebDriverWait wait = new WebDriverWait(BrowserManager.getDriver(),
		timeout);

	try {
	    WebElement element = wait.until(ExpectedConditions
		    .elementToBeClickable(locator));
	    element.click();
	    LogManager.info("Element was clicked");

	} catch (TimeoutException e) {
	    LogManager.info("Element waiting for clickable was not found");
	}
    }

    public static void waitForUrl(String url, int timeout) {

	WebDriverWait wait = new WebDriverWait(BrowserManager.getDriver(),
		timeout);
	try {
	    wait.until(ExpectedConditions.urlToBe(url));
	} catch (TimeoutException e) {
	    LogManager.error("Error when get the url: " + e.getMessage());
	}

    }

    public static WebElement getWhenVisible(By locator, int timeout) {

	WebElement element = null;
	WebDriverWait wait = new WebDriverWait(BrowserManager.getDriver(),
		timeout);

	try {
	    element = wait.until(ExpectedConditions
		    .visibilityOfElementLocated(locator));
	} catch (TimeoutException e) {
	    LogManager.error("Error when get an element" + e.getMessage());
	}
	return element;
    }

    public static WebElement getWhenVisible(By locator, int timeout,
	    boolean loggeable) {

	WebElement element = null;
	WebDriverWait wait = new WebDriverWait(BrowserManager.getDriver(),
		timeout);

	try {
	    element = wait.until(ExpectedConditions
		    .visibilityOfElementLocated(locator));
	} catch (TimeoutException e) {
	    if (loggeable) {
		LogManager.error("Error when get an element" + e.getMessage());
	    }
	}
	return element;
    }
    
    public static boolean waitForElement(String locator, int timeout) {
	WebDriverWait wait = new WebDriverWait(BrowserManager.getDriver(),
		timeout);
	try{
		wait.until(ExpectedConditions.presenceOfElementLocated(
			By.xpath(locator)));
		return true;
	}catch (Exception e){
		return false;
	}
}

    public static void waitElementVisible(WebElement element, int timeout) {

	WebDriverWait wait = new WebDriverWait(BrowserManager.getDriver(),
		timeout);

	try {
	    wait.until(ExpectedConditions.visibilityOf(element));
	} catch (TimeoutException e) {
	    LogManager.error("Error when get an element" + e.getMessage());
	}

    }

    public static List<WebElement> getElementsWhenVisible(By locator,
	    int timeout) {
	List<WebElement> elements = null;
	WebDriverWait wait = new WebDriverWait(BrowserManager.getDriver(),
		timeout);

	try {
	    elements = wait.until(ExpectedConditions
		    .visibilityOfAllElementsLocatedBy(locator));
	} catch (TimeoutException e) {
	    LogManager.error("Elements could not be found " + e.getMessage());
	}

	return elements;
    }

    public static void waitFluentElement(final By locator,
	    final int timeoutSeconds) {
	final WebDriver driver = BrowserManager.getDriver();

	FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)
		.withTimeout(timeoutSeconds, TimeUnit.SECONDS)
		.pollingEvery(1, TimeUnit.SECONDS)
		.ignoring(NoSuchElementException.class);

	wait.until(ExpectedConditions.invisibilityOfElementLocated(locator));
    }

    public static boolean isElementInvisible(By locator, int timeout) {
	try {
	    WebDriverWait wait = new WebDriverWait(BrowserManager.getDriver(),
		timeout);

	    return wait.until(ExpectedConditions
		.invisibilityOfElementLocated(locator));
    	} catch (Exception exc) {
    	    return true;
    	}
    }
}
    
   
  
@@ -933,24 +933,497 @@
 %22);%0A%09%7D%0A    %7D
+%0A    %0A    public static boolean clickWhenReadyState(By locator, int timeout) %7B%0A%09WebDriverWait wait = new WebDriverWait(BrowserManager.getDriver(),%0A%09%09timeout);%0A%0A%09try %7B%0A%09    WebElement element = wait.until(ExpectedConditions%0A%09%09    .elementToBeClickable(locator));%0A%09    element.click();%0A%09    LogManager.info(%22Element was clicked%22);%0A%0A%09%7D catch (TimeoutException e) %7B%0A%09    LogManager.info(%22Element waiting for clickable was not found%22);%0A%09    return false;%0A%09%7D%0A%09 return true;%0A    %7D
 %0A%0A    public
@@ -3659,32 +3659,36 @@
 elements;%0A    %7D%0A
+    
 %0A    public stat
@@ -4254,28 +4254,36 @@
 meout);%0A
-%0A
 %09    
-return
+boolean state =
  wait.un
@@ -4305,16 +4305,17 @@
 ditions%0A
+%09
 %09%09.invis
@@ -4345,24 +4345,43 @@
 (locator));%0A
+%09    return state;%0A
     %09%7D catch

Fix Email server scenario: Adding an explicit wait returning a boolean