package cz.moneta.test.harness.utils.expectedconditions;

import cz.moneta.test.harness.exception.FailElementDisplayedException;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;

public class BooleanExpectedConditions {

    /**
     * Custom expected condition for check element not displayed on page.
     * <p>
     * If element displayed condition will throw new FailElementDisplayedException. In all other cases (even NoSuchElementException)
     * returns true - it means that element not displayed.
     * </p>
     *
     * @param locator - locator (By) value to check if not displayed
     */
    public static ExpectedCondition<Boolean> failElementNotDisplayed(By locator) {
        return driver -> {
            try {
                WebElement element = driver.findElement(locator);
                if (element.isDisplayed()) {
                    throw new FailElementDisplayedException("Fail element displayed. Locator: " + locator.toString());
                }
                return true;
            } catch (NoSuchElementException e) {
                return true;
            }
        };
    }
}
