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