001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Benoit Delbosc
016 *     Antoine Taillefer
017 */
018package org.nuxeo.functionaltests.pages.admincenter.usermanagement;
019
020import org.nuxeo.functionaltests.Required;
021import org.nuxeo.functionaltests.forms.Select2WidgetElement;
022import org.openqa.selenium.By;
023import org.openqa.selenium.NoSuchElementException;
024import org.openqa.selenium.WebDriver;
025import org.openqa.selenium.WebElement;
026import org.openqa.selenium.support.FindBy;
027
028/**
029 * Edit user details (New one in the admin center)
030 *
031 * @since 5.4.2
032 */
033public class UserEditFormPage extends UsersGroupsBasePage {
034
035    @Required
036    @FindBy(id = "viewUserView:editUser:nxl_user_1:nxw_firstname_1")
037    WebElement firstnameInput;
038
039    @Required
040    @FindBy(id = "viewUserView:editUser:nxl_user_1:nxw_lastname_1")
041    WebElement lastnameInput;
042
043    @Required
044    @FindBy(id = "viewUserView:editUser:nxl_user_1:nxw_company_1")
045    WebElement companyInput;
046
047    @Required
048    @FindBy(id = "viewUserView:editUser:nxl_user_1:nxw_email_1")
049    WebElement emailInput;
050
051    @Required
052    @FindBy(xpath = "//input[@value=\"Save\"]")
053    WebElement saveButton;
054
055    public UserEditFormPage(WebDriver driver) {
056        super(driver);
057    }
058
059    /**
060     * Edit a user, only update non null value.
061     *
062     * @param firstname
063     * @param lastname
064     * @param company
065     * @param email
066     * @param password
067     * @param group
068     * @throws NoSuchElementException
069     */
070    public UserViewTabSubPage editUser(String firstname, String lastname, String company, String email, String group)
071            throws NoSuchElementException {
072        updateInput(firstnameInput, firstname);
073        updateInput(lastnameInput, lastname);
074        updateInput(companyInput, company);
075        updateInput(emailInput, email);
076        if (group != null) {
077            Select2WidgetElement groups = new Select2WidgetElement(
078                    driver,
079                    driver.findElement(By.xpath("//*[@id='s2id_viewUserView:editUser:nxl_user_1:nxw_groups_1_select2']")),
080                    true);
081            groups.selectValue(group);
082        }
083        saveButton.click();
084        return asPage(UserViewTabSubPage.class);
085    }
086
087    private void updateInput(WebElement elem, String value) {
088        if (value != null) {
089            elem.clear();
090            elem.sendKeys(value);
091        }
092    }
093
094}