package cz.moneta.test.harness.connectors.web;

import org.openqa.selenium.JavascriptExecutor;

import java.util.Optional;

public class Scripts {

    private final SeleniumWebConnector connector;

    public Scripts(SeleniumWebConnector connector) {
        this.connector = connector;
    }

    public Object executeScript(String script, Object[] args) {
        JavascriptExecutor executor = Optional.of(connector.getDriver())
                .filter(JavascriptExecutor.class::isInstance)
                .map(JavascriptExecutor.class::cast)
                .orElseThrow(() -> new IllegalStateException("This driver does not support JavaScript!"));
        return args == null
                ? executor.executeScript(script)
                : executor.executeScript(script, args);
    }
}
