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.Required;
023import org.nuxeo.functionaltests.forms.Select2WidgetElement;
024import org.openqa.selenium.By;
025import org.openqa.selenium.NoSuchElementException;
026import org.openqa.selenium.WebDriver;
027import org.openqa.selenium.WebElement;
028import org.openqa.selenium.support.FindBy;
029
030/**
031 * Edit user details (New one in the admin center)
032 *
033 * @since 5.4.2
034 */
035public class UserEditFormPage extends UsersGroupsBasePage {
036
037    @Required
038    @FindBy(id = "viewUserView:editUser:nxl_user_1:nxw_firstname_1")
039    WebElement firstnameInput;
040
041    @Required
042    @FindBy(id = "viewUserView:editUser:nxl_user_1:nxw_lastname_1")
043    WebElement lastnameInput;
044
045    @Required
046    @FindBy(id = "viewUserView:editUser:nxl_user_1:nxw_company_1")
047    WebElement companyInput;
048
049    @Required
050    @FindBy(id = "viewUserView:editUser:nxl_user_1:nxw_email_1")
051    WebElement emailInput;
052
053    @Required
054    @FindBy(xpath = "//input[@value=\"Save\"]")
055    WebElement saveButton;
056
057    public UserEditFormPage(WebDriver driver) {
058        super(driver);
059    }
060
061    /**
062     * Edit a user, only update non null value.
063     *
064     * @param firstname
065     * @param lastname
066     * @param company
067     * @param email
068     * @param password
069     * @param group
070     * @throws NoSuchElementException
071     */
072    public UserViewTabSubPage editUser(String firstname, String lastname, String company, String email, String group)
073            throws NoSuchElementException {
074        updateInput(firstnameInput, firstname);
075        updateInput(lastnameInput, lastname);
076        updateInput(companyInput, company);
077        updateInput(emailInput, email);
078        if (group != null) {
079            Select2WidgetElement groups = new Select2WidgetElement(
080                    driver,
081                    driver.findElement(By.xpath("//*[@id='s2id_viewUserView:editUser:nxl_user_1:nxw_groups_1_select2']")),
082                    true);
083            groups.selectValue(group);
084        }
085        saveButton.click();
086        return asPage(UserViewTabSubPage.class);
087    }
088
089    private void updateInput(WebElement elem, String value) {
090        if (value != null) {
091            elem.clear();
092            elem.sendKeys(value);
093        }
094    }
095
096}