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

com.tidal.wave.commands.ScrollToView Maven / Gradle / Ivy

There is a newer version: 1.3.1
Show newest version
package com.tidal.wave.commands;

import com.tidal.wave.command.Command;
import com.tidal.wave.command.CommandAction;
import com.tidal.wave.command.CommandContext;
import com.tidal.wave.command.Commands;
import com.tidal.wave.counter.TimeCounter;
import com.tidal.wave.exceptions.CommandExceptions;
import com.tidal.wave.supplier.ObjectSupplier;
import com.tidal.wave.webelement.Element;
import org.openqa.selenium.ElementNotInteractableException;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebElement;

import java.util.Map;
import java.util.function.Supplier;

@SuppressWarnings("unused")
public final class ScrollToView extends CommandAction implements Command {

    private final Supplier, Supplier>> ignoredExceptions = this::ignoredEx;
    private final Element webElement = (Element) ObjectSupplier.instanceOf(Element.class);
    private final TimeCounter timeCounter = new TimeCounter();
    private boolean visibility;
    private boolean isMultiple;

    @Override
    public void contextSetter(CommandContext context) {
        this.locators = context.getLocators();
        this.visibility = context.getVisibility();
        this.isMultiple = context.isMultiple();
    }

    @Override
    public Map, Supplier> ignoredEx() {
        return CommandExceptions.TypeOf.notInteractableAndStale();
    }

    public void scrollToViewAction() {
        WebElement element = webElement.getElement(locators, false,  isMultiple);
        ((JavascriptExecutor) ((RemoteWebElement) element).getWrappedDriver()).executeScript("arguments[0].scrollIntoView(true);", element);

        element = webElement.getElement(locators, false,  isMultiple);

        if(visibility && !element.isDisplayed()){
            throw new ElementNotInteractableException("Element is not in view, applying scroll-to-view again");
        }
    }

    public void scrollToView() {
        timeCounter.restart();
        super.execute(Commands.MoveCommands.SCROLL_TO_VIEW.toString(), ignoredExceptions, timeCounter);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy