package org.getopentest.selenium;

import org.getopentest.annotations.TestActionArgument;
import org.getopentest.annotations.TestActionClass;
import org.getopentest.annotations.Type;
import org.getopentest.selenium.core.SeleniumTestAction;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

@TestActionClass(description = "Clears the cotent of a \"input\" or \"textarea\" element.")
@TestActionArgument(
        name = "locator", type = Type.OBJECT, optional = false,
        description = "Locator object that identifies the \"input\" or \"textarea\" element.")

public class ClearContent extends SeleniumTestAction {

    @Override
    public void run() {
        super.run();

        By locator = this.readLocatorArgument("locator");

        this.waitForAsyncCallsToFinish();

        try {
            WebElement element = this.getElement(locator);
            WebDriverWait wait = new WebDriverWait(this.driver, this.getExplicitWaitSec());
            wait.until(ExpectedConditions.elementToBeClickable(element));

            element.clear();
        } catch (Exception ex) {
            throw new RuntimeException(String.format(
                    "Failed clicking on element %s",
                    locator), ex);
        }
    }
}
