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

aquality.selenium.core.elements.ElementFinder Maven / Gradle / Ivy

Go to download

Library with core functions simplifying work with Selenium-controlled applications.

There is a newer version: 4.2.0
Show newest version
package aquality.selenium.core.elements;

import aquality.selenium.core.elements.interfaces.IElementFinder;
import aquality.selenium.core.localization.ILocalizedLogger;
import aquality.selenium.core.waitings.IConditionalWait;
import com.google.inject.Inject;
import org.openqa.selenium.*;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Implementation of IElementFinder.
 *
 * @author lenovo
 * @version $Id: $Id
 */
public class ElementFinder implements IElementFinder {
    private final ILocalizedLogger localizedLogger;
    private final IConditionalWait conditionalWait;

    @Inject
    /**
     * 

Constructor for ElementFinder.

* * @param localizedLogger a {@link aquality.selenium.core.localization.ILocalizedLogger} object. * @param conditionalWait a {@link aquality.selenium.core.waitings.IConditionalWait} object. */ public ElementFinder(ILocalizedLogger localizedLogger, IConditionalWait conditionalWait) { this.localizedLogger = localizedLogger; this.conditionalWait = conditionalWait; } /** {@inheritDoc} */ @Override public List findElements(By locator, DesiredState desiredState, Duration timeout) { AtomicBoolean wasAnyElementFound = new AtomicBoolean(false); List resultElements = new ArrayList<>(); try { conditionalWait.waitFor(driver -> tryToFindElements(locator, desiredState, wasAnyElementFound, resultElements, driver), timeout); } catch (TimeoutException e) { handleTimeoutException(e, locator, desiredState, wasAnyElementFound.get()); } return resultElements; } /** *

tryToFindElements.

* * @param locator a {@link org.openqa.selenium.By} object. * @param desiredState a {@link aquality.selenium.core.elements.DesiredState} object. * @param wasAnyElementFound a {@link java.util.concurrent.atomic.AtomicBoolean} object. * @param resultElements a {@link java.util.List} object. * @param context a {@link org.openqa.selenium.SearchContext} object. * @return a boolean. */ protected boolean tryToFindElements(By locator, DesiredState desiredState, AtomicBoolean wasAnyElementFound, List resultElements, SearchContext context) { List currentFoundElements = context.findElements(locator); wasAnyElementFound.set(!currentFoundElements.isEmpty()); currentFoundElements .stream() .filter(desiredState.getElementStateCondition()) .forEachOrdered(resultElements::add); return !resultElements.isEmpty(); } /** * depends on configuration of DesiredState object it can be required to throw or not NoSuchElementException * * @param exception TimeoutException to handle * @param locator locator that is using to find elements * @param desiredState DesiredState object * @param wasAnyElementFound was any element found by locator or not. */ protected void handleTimeoutException(TimeoutException exception, By locator, DesiredState desiredState, boolean wasAnyElementFound) { String message = String.format("No elements with locator '%1$s' were found in %2$s state", locator, desiredState.getStateName()); if (desiredState.isCatchingInTimeoutException()) { if (!wasAnyElementFound) { if (desiredState.isThrowingNoSuchElementException()) { throw new NoSuchElementException(message); } localizedLogger.debug("loc.no.elements.found.in.state", locator, desiredState.getStateName()); } else { localizedLogger.debug("loc.elements.were.found.but.not.in.state", locator, desiredState.getStateName()); } } else { String combinedMessage = String.format("%1$s: %2$s", message, exception.getMessage()); if (desiredState.isThrowingNoSuchElementException() && !wasAnyElementFound) { throw new NoSuchElementException(combinedMessage); } throw new TimeoutException(combinedMessage); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy