001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Benoit Delbosc
018 *     Antoine Taillefer
019 */
020package org.nuxeo.functionaltests.pages.admincenter.usermanagement;
021
022import org.nuxeo.functionaltests.AjaxRequestManager;
023import org.nuxeo.functionaltests.Locator;
024import org.nuxeo.functionaltests.Required;
025import org.nuxeo.functionaltests.forms.Select2WidgetElement;
026import org.openqa.selenium.By;
027import org.openqa.selenium.NoSuchElementException;
028import org.openqa.selenium.WebDriver;
029import org.openqa.selenium.WebElement;
030import org.openqa.selenium.support.FindBy;
031
032/**
033 * Edit user details (New one in the admin center)
034 *
035 * @since 5.4.2
036 */
037public class UserEditFormPage extends UsersGroupsBasePage {
038
039    @Required
040    @FindBy(id = "viewUserView:editUser:nxl_user_1:nxw_firstname_1")
041    WebElement firstnameInput;
042
043    @Required
044    @FindBy(id = "viewUserView:editUser:nxl_user_1:nxw_lastname_1")
045    WebElement lastnameInput;
046
047    @Required
048    @FindBy(id = "viewUserView:editUser:nxl_user_1:nxw_company_1")
049    WebElement companyInput;
050
051    @Required
052    @FindBy(id = "viewUserView:editUser:nxl_user_1:nxw_email_1")
053    WebElement emailInput;
054
055    @Required
056    @FindBy(xpath = "//input[@value=\"Save\"]")
057    WebElement saveButton;
058
059    public UserEditFormPage(WebDriver driver) {
060        super(driver);
061    }
062
063    /**
064     * Edit a user, only update non null value.
065     *
066     * @param firstname
067     * @param lastname
068     * @param company
069     * @param email
070     * @param password
071     * @param group
072     * @throws NoSuchElementException
073     */
074    public UserViewTabSubPage editUser(String firstname, String lastname, String company, String email, String group)
075            throws NoSuchElementException {
076        updateInput(firstnameInput, firstname);
077        updateInput(lastnameInput, lastname);
078        updateInput(companyInput, company);
079        updateInput(emailInput, email);
080        if (group != null) {
081            Select2WidgetElement groups = new Select2WidgetElement(driver, driver.findElement(
082                    By.xpath("//*[@id='s2id_viewUserView:editUser:nxl_user_1:nxw_groups_1_select2']")), true);
083            groups.selectValue(group);
084        }
085        AjaxRequestManager arm = new AjaxRequestManager(driver);
086        arm.begin();
087        Locator.waitUntilEnabledAndClick(saveButton);
088        arm.end();
089        return asPage(UserViewTabSubPage.class);
090    }
091
092    private void updateInput(WebElement elem, String value) {
093        if (value != null) {
094            elem.clear();
095            elem.sendKeys(value);
096        }
097    }
098
099}