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

org.xpathqs.driver.executor.BaseExecutor.kt Maven / Gradle / Ivy

There is a newer version: 0.0.9
Show newest version
package org.xpathqs.driver.executor

import org.xpathqs.driver.IDriver
import org.xpathqs.driver.actions.IAction
import org.xpathqs.driver.actions.WaitAction
import org.xpathqs.driver.exceptions.XPathQsException
import org.xpathqs.driver.log.Log

typealias ActionExecLambda = (IAction) -> Unit
typealias ActionExecMap = HashMap

abstract class BaseExecutor(
    protected open val driver: IDriver
) : IExecutor {

    private val actionHandlerBase = ActionExecMap().apply {
        set(WaitAction().name) {
            executeAction(it as WaitAction)
        }
    }

    override fun execute(action: IAction) {
        beforeAction(action)
        executeConcreteAction(action)
        afterAction(action)
    }

    open fun hasActionHandler(action: IAction) = actionHandlerBase.containsKey(action.name)

    open fun getActionHandler(action: IAction) =
        actionHandlerBase[action.name] ?: throw  XPathQsException.ActionNotFound(action, this)

    protected open fun executeConcreteAction(action: IAction) {
        if (hasActionHandler(action)) {
            Log.action(action)
            getActionHandler(action).invoke(action)
        } else {
            throw XPathQsException.ActionNotFound(action, this)
        }
    }

    private fun executeAction(action: WaitAction) {
        Thread.sleep(action.timeout.toMillis())
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy