/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.appwithinminutes.test.po;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.xwiki.test.ui.po.InlinePage;

/**
 * Represents the actions possible when editing an application entry.
 * 
 * @version $Id$
 * @since 4.2M1
 */
public class EntryEditPage extends InlinePage
{
    /**
     * The XPath that locates the label of a form field.
     * <p>
     * NOTE: We use a workaround for the fact that ends-with XPath function is not implemented:
     * 
     * <pre>
     * {@code substring(@id, string-length(@id) - string-length(suffix) + 1)}
     * </pre>
     */
    private static final String LABEL_XPATH_FORMAT = "//label[substring(@for, string-length(@for) - %s - 2) = '_0_%s']";

    /**
     * The XPath that locates a form field.
     */
    private static final String FIELD_XPATH_FORMAT = "//*[substring(@id, string-length(@id) - %s - 2) = '_0_%s']";

    /**
     * Retrieves the label of the specified form field.
     * 
     * @param fieldName the name of a form field
     * @return the label of the specified form field
     */
    public String getLabel(String fieldName)
    {
        String xpath = String.format(LABEL_XPATH_FORMAT, fieldName.length(), fieldName);
        WebElement label = getForm().findElement(By.xpath(xpath));
        return label.getText();
    }

    /**
     * Retrieves the value of the specified form field
     * 
     * @param fieldName the name of a form field
     * @return the value of the specified form field
     */
    public String getValue(String fieldName)
    {
        String xpath = String.format(FIELD_XPATH_FORMAT, fieldName.length(), fieldName);
        WebElement field = getForm().findElement(By.xpath(xpath));
        return field.getAttribute("value");
    }

    /**
     * Sets the value of the specified form field
     * 
     * @param fieldName the name of a form field
     * @param fieldValue the new value for the specified form field
     */
    public void setValue(String fieldName, String fieldValue)
    {
        String xpath = String.format(FIELD_XPATH_FORMAT, fieldName.length(), fieldName);
        WebElement field = getForm().findElement(By.xpath(xpath));
        field.clear();
        field.sendKeys(fieldValue);
    }

    /**
     * @return the list of form field names available on this page
     */
    public List<String> getFieldNames()
    {
        List<String> fieldNames = new ArrayList<String>();
        for (WebElement field : getForm().findElements(By.xpath("//*[contains(@name, '_0_')]"))) {
            fieldNames.add(StringUtils.substringAfter(field.getAttribute("name"), "_0_"));
        }
        return fieldNames;
    }
}

@@ -1099,16 +1099,57 @@
 lement;%0A
+import org.xwiki.test.ui.po.FormElement;%0A
 import o
@@ -1180,16 +1180,16 @@
 nePage;%0A
-
 %0A/**%0A * 
@@ -1880,34 +1880,36 @@
 %22//*%5Bsubstring(@
-id
+name
 , string-length(
@@ -1905,26 +1905,28 @@
 ing-length(@
-id
+name
 ) - %25s - 2) 
@@ -2700,108 +2700,71 @@
     
-WebElement field = getForm().findElement(By.xpath(xpath));%0A        return field.getAttribute(%22value%22
+return new FormElement(getForm()).getFieldValue(By.xpath(xpath)
 );%0A 
@@ -3196,58 +3196,363 @@
     
+if (
 field.
-clear();%0A        field.sendKeys(fieldValue);
+getAttribute(%22name%22).equals(field.getAttribute(%22id%22))) %7B%0A            new FormElement(getForm()).setFieldValue(field, fieldValue);%0A        %7D else %7B%0A            xpath = String.format(%22//*%5B@name = '%25s' and @value = '%25s'%5D%22, field.getAttribute(%22name%22), fieldValue);%0A            new FormElement(getForm()).setCheckBox(By.xpath(xpath), true);%0A        %7D
 %0A   
@@ -3939,24 +3939,24 @@
 ;%0A        %7D%0A
-
         retu
@@ -3975,11 +3975,271 @@
 s;%0A    %7D
+%0A%0A    /**%0A     * Sets the entry title, if the application class has a Title field.%0A     * %0A     * @param title the entry title%0A     */%0A    public void setTitle(String title)%0A    %7B%0A        new FormElement(getForm()).setFieldValue(By.name(%22title%22), title);%0A    %7D
 %0A%7D%0A

Add support for setting the value of the Title field and the value of a Static List displayed using check-boxes.