package hu.pazsitz.pacuse.tests.helpers;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

/**
 * FormHelper.java
 *
 * @author Zoltan Pazsit <pazsitz@pazsitz.hu>
 * @copyright Copyright (c) 2014, Zoltan Pazsit
 */
public class FormHelper {

    /**
     * Handles "yes" and "true" String values as checked
     *
     * @param element
     * @param value
     * @return WebElement
     */
    public static WebElement checkboxTicker(WebElement element, String value) {
        boolean boolValue = getCheckboxBoolValue(value);

        if (!element.isSelected() && boolValue) {
            element.click();
        } else if (element.isSelected() && !boolValue) {
            element.click();
        }

        return element;
    }

    /**
     * Converts "yes" and "true" strings (from gherkin table) to boolean
     * @param value
     * @return boolean
     */
    public static boolean getCheckboxBoolValue(String value) {
		final String TICK_YES = "yes";
        final String TICK_TRUE = "true";

        if (value == null) return false;
        
        value = value.trim().toLowerCase();
        boolean boolValue = value.equals(TICK_TRUE) || value.equals(TICK_YES);
		return boolValue;
	}

    /**
     * fills the element only on non-null value
     *
     * @param element
     * @param value
     * @return WebElement
     */
    public static WebElement selectByValue(WebElement element, String value) {
        if (value == null) return element;
        Select selector = new Select(element);
        selector.selectByValue(value);

        return element;
    }

    /**
     * 
     * @param element
     * @param value
     * @return WebElement
     */
    public static WebElement selectByIndex(WebElement element, int value) {
        Select selector = new Select(element);
        selector.selectByIndex(value);

        return element;
    }

    /**
     * fills the element only on non-null value
     *
     * @param element
     * @param value
     * @return WebElement
     */
    public static WebElement selectByText(WebElement element, String value) {
        if (value == null) return element;
        Select selector = new Select(element);
        selector.selectByVisibleText(value);

        return element;
    }

    /**
     * fills the element only on non-null value
     *
     * @param element
     * @param value
     * @return WebElement
     */
    public static WebElement inputFillOverWrite(WebElement element, String value) {
        if (value == null) return element;
        element.clear();
        element.sendKeys(value);

        return element;
    }
}
@@ -272,16 +272,408 @@
 lper %7B%0A%0A
+%09/**%0A%09 * Handles checkbox setting%0A%09 * @param element%0A%09 * @param boolValue%0A%09 * @return WebElement%0A%09 */%0A%09 public static WebElement checkboxTicker(WebElement element, boolean boolValue) %7B%0A%09%09 if (!element.isSelected() && boolValue) %7B%0A%09            element.click();%0A%09        %7D else if (element.isSelected() && !boolValue) %7B%0A%09            element.click();%0A%09        %7D%0A%0A%09        return element;%0A%09 %7D%0A%09 %0A
     /**%0A
@@ -962,198 +962,49 @@
     
-if (!element.isSelected() && boolValue) %7B%0A            element.click();%0A        %7D else if (element.isSelected() && !boolValue) %7B%0A            element.click();%0A        %7D%0A%0A        return element
+return checkboxTicker(element, boolValue)
 ;%0A  
@@ -1482,33 +1482,33 @@
 %0A    /**%0A     * 
-f
+F
 ills the element
@@ -1523,39 +1523,32 @@
  non-null value%0A
-     *%0A
      * @param el
@@ -1862,16 +1862,49 @@
 %0A     * 
+Selects option by numerical index
 %0A     * 
@@ -2173,33 +2173,33 @@
 %0A    /**%0A     * 
-f
+F
 ills the element
@@ -2214,39 +2214,32 @@
  non-null value%0A
-     *%0A
      * @param el

extend form checkboxTicker signature