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 *     Sun Seng David TAN
016 *     Florent Guillaume
017 *     Antoine Taillefer
018 */
019package org.nuxeo.functionaltests.pages.admincenter.usermanagement;
020
021import java.util.List;
022
023import org.apache.commons.lang.StringUtils;
024import org.nuxeo.functionaltests.Locator;
025import org.nuxeo.functionaltests.Required;
026import org.nuxeo.functionaltests.forms.Select2WidgetElement;
027import org.openqa.selenium.By;
028import org.openqa.selenium.NoSuchElementException;
029import org.openqa.selenium.WebDriver;
030import org.openqa.selenium.WebElement;
031import org.openqa.selenium.support.FindBy;
032
033/**
034 * Nuxeo DM user management creation form page. (New one in the admin center)
035 *
036 * @since 5.4.2
037 */
038public class UserCreationFormPage extends UsersGroupsBasePage {
039
040    @FindBy(name = "createUserView:createUser:nxl_user:nxw_passwordMatcher_immediate_creation")
041    List<WebElement> immediateCreation;
042
043    @FindBy(id = "createUserView:createUser:nxl_user:nxw_username")
044    WebElement usernameInput;
045
046    @FindBy(id = "createUserView:createUser:nxl_user:nxw_firstname")
047    WebElement firstnameInput;
048
049    @FindBy(id = "createUserView:createUser:nxl_user:nxw_lastname")
050    WebElement lastnameInput;
051
052    @FindBy(id = "createUserView:createUser:nxl_user:nxw_company")
053    WebElement companyInput;
054
055    @FindBy(id = "createUserView:createUser:nxl_user:nxw_email")
056    WebElement emailInput;
057
058    @FindBy(id = "createUserView:createUser:nxl_user:nxw_passwordMatcher_firstPassword")
059    WebElement firstPasswordInput;
060
061    @FindBy(id = "createUserView:createUser:nxl_user:nxw_passwordMatcher_secondPassword")
062    WebElement secondPasswordInput;
063
064    @FindBy(id = "createUserView:createUser:button_save")
065    WebElement createButton;
066
067    @Required
068    @FindBy(xpath = "//div[@class=\"tabsContent\"]//input[@value=\"Cancel\"]")
069    WebElement cancelButton;
070
071    public UserCreationFormPage(WebDriver driver) {
072        super(driver);
073    }
074
075    public UsersGroupsBasePage createUser(String username, String firstname, String lastname, String company,
076            String email, String password, String group) throws NoSuchElementException {
077        return createUser(username, firstname, lastname, company, email, password, group, false);
078    }
079
080    public UsersGroupsBasePage inviteUser(String username, String firstname, String lastname, String company,
081            String email, String group) throws NoSuchElementException {
082        return createUser(username, firstname, lastname, company, email, "", group, true);
083    }
084
085    private boolean isObjectChecked(int index) {
086        assert (index < 2 && index >= 0);
087        org.junit.Assert.assertNotNull(immediateCreation);
088        org.junit.Assert.assertEquals(2, immediateCreation.size());
089
090        return immediateCreation.get(index).isSelected();
091    }
092
093    public boolean isImmediateCreationYesSelected() {
094        return isObjectChecked(1);
095    }
096
097    public UsersGroupsBasePage createUser(String username, String firstname, String lastname, String company,
098            String email, String password, String group, final boolean invite) throws NoSuchElementException {
099        if (!invite) {
100            switchCreationFormPage();
101            usernameInput.sendKeys(username);
102            firstnameInput.sendKeys(firstname);
103            lastnameInput.sendKeys(lastname);
104            companyInput.sendKeys(company);
105            emailInput.sendKeys(email);
106            firstPasswordInput.sendKeys(password);
107            secondPasswordInput.sendKeys(password);
108            if (StringUtils.isNotBlank(group)) {
109                Select2WidgetElement groups = new Select2WidgetElement(
110                        driver,
111                        driver.findElement(By.xpath("//div[@id='s2id_createUserView:createUser:nxl_user:nxw_groups_select2']")),
112                        true);
113                groups.selectValue(group);
114            }
115            createButton.click();
116        } else {
117            usernameInput.sendKeys(username);
118            firstnameInput.sendKeys(firstname);
119            lastnameInput.sendKeys(lastname);
120            companyInput.sendKeys(company);
121            emailInput.sendKeys(email);
122            if (StringUtils.isNotBlank(group)) {
123                Select2WidgetElement groups = new Select2WidgetElement(
124                        driver,
125                        driver.findElement(By.xpath("//div[@id='s2id_createUserView:createUser:nxl_user:nxw_groups_select2']")),
126                        true);
127                groups.selectValue(group);
128            }
129            createButton.click();
130        }
131        return asPage(UsersGroupsBasePage.class);
132    }
133
134    public UsersTabSubPage cancelCreation() {
135        cancelButton.click();
136        return asPage(UsersTabSubPage.class);
137    }
138
139    protected void switchCreationFormPage() {
140        if (!isImmediateCreationYesSelected()) {
141            immediateCreation.get(1).click();
142            Locator.waitUntilElementPresent(By.id("createUserView:createUser:nxl_user:nxw_passwordMatcher_firstPassword"));
143        }
144    }
145}