All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.magentys.cinnamon.webdriver.elements.ElementCache Maven / Gradle / Ivy

There is a newer version: 0.2.0
Show newest version
package io.magentys.cinnamon.webdriver.elements;

import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.pagefactory.ElementLocator;

class ElementCache {

    private final ElementLocator elementLocator;
    private WebElement cachedElement;

    public ElementCache(final ElementLocator locator, WebElement element) {
        this.elementLocator = locator;
        setElement(element);
    }

    public synchronized WebElement getElement() {
        if (cachedElement == null) {
            setElement(elementLocator.findElement());
        }
        invalidateCache();
        return cachedElement;
    }

    public synchronized void setElement(WebElement cachedElement) {
        this.cachedElement = cachedElement;
    }

    private void invalidateCache() {
        try {
            cachedElement.getTagName();
        } catch (NullPointerException | StaleElementReferenceException ex) {
            setElement(elementLocator.findElement());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy