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(name = "createUserView:createUser:nxl_user:nxw_passwordMatcher_immediate_creation")
044    List<WebElement> immediateCreation;
045
046    @FindBy(id = "createUserView:createUser:nxl_user:nxw_username")
047    WebElement usernameInput;
048
049    @FindBy(id = "createUserView:createUser:nxl_user:nxw_firstname")
050    WebElement firstnameInput;
051
052    @FindBy(id = "createUserView:createUser:nxl_user:nxw_lastname")
053    WebElement lastnameInput;
054
055    @FindBy(id = "createUserView:createUser:nxl_user:nxw_company")
056    WebElement companyInput;
057
058    @FindBy(id = "createUserView:createUser:nxl_user:nxw_email")
059    WebElement emailInput;
060
061    @FindBy(id = "createUserView:createUser:nxl_user:nxw_passwordMatcher_firstPassword")
062    WebElement firstPasswordInput;
063
064    @FindBy(id = "createUserView:createUser:nxl_user:nxw_passwordMatcher_secondPassword")
065    WebElement secondPasswordInput;
066
067    @FindBy(id = "createUserView:createUser:button_save")
068    WebElement createButton;
069
070    @Required
071    @FindBy(xpath = "//div[@class=\"tabsContent\"]//input[@value=\"Cancel\"]")
072    WebElement cancelButton;
073
074    public UserCreationFormPage(WebDriver driver) {
075        super(driver);
076    }
077
078    public UsersGroupsBasePage createUser(String username, String firstname, String lastname, String company,
079            String email, String password, String group) throws NoSuchElementException {
080        return createUser(username, firstname, lastname, company, email, password, group, false);
081    }
082
083    public UsersGroupsBasePage inviteUser(String username, String firstname, String lastname, String company,
084            String email, String group) throws NoSuchElementException {
085        return createUser(username, firstname, lastname, company, email, "", group, true);
086    }
087
088    private boolean isObjectChecked(int index) {
089        assert (index < 2 && index >= 0);
090        org.junit.Assert.assertNotNull(immediateCreation);
091        org.junit.Assert.assertEquals(2, immediateCreation.size());
092
093        return immediateCreation.get(index).isSelected();
094    }
095
096    public boolean isImmediateCreationYesSelected() {
097        return isObjectChecked(1);
098    }
099
100    public UsersGroupsBasePage createUser(String username, String firstname, String lastname, String company,
101            String email, String password, String group, final boolean invite) throws NoSuchElementException {
102        if (!invite) {
103            switchCreationFormPage();
104            usernameInput.sendKeys(username);
105            firstnameInput.sendKeys(firstname);
106            lastnameInput.sendKeys(lastname);
107            companyInput.sendKeys(company);
108            emailInput.sendKeys(email);
109            firstPasswordInput.sendKeys(password);
110            secondPasswordInput.sendKeys(password);
111            if (StringUtils.isNotBlank(group)) {
112                Select2WidgetElement groups = new Select2WidgetElement(
113                        driver,
114                        driver.findElement(By.xpath("//div[@id='s2id_createUserView:createUser:nxl_user:nxw_groups_select2']")),
115                        true);
116                groups.selectValue(group);
117            }
118            AjaxRequestManager arm = new AjaxRequestManager(driver);
119            arm.begin();
120            createButton.click();
121            arm.end();
122        } else {
123            usernameInput.sendKeys(username);
124            firstnameInput.sendKeys(firstname);
125            lastnameInput.sendKeys(lastname);
126            companyInput.sendKeys(company);
127            emailInput.sendKeys(email);
128            if (StringUtils.isNotBlank(group)) {
129                Select2WidgetElement groups = new Select2WidgetElement(
130                        driver,
131                        driver.findElement(By.xpath("//div[@id='s2id_createUserView:createUser:nxl_user:nxw_groups_select2']")),
132                        true);
133                groups.selectValue(group);
134            }
135            AjaxRequestManager arm = new AjaxRequestManager(driver);
136            arm.begin();
137            createButton.click();
138            arm.end();
139        }
140        return asPage(UsersGroupsBasePage.class);
141    }
142
143    public UsersTabSubPage cancelCreation() {
144        AjaxRequestManager arm = new AjaxRequestManager(driver);
145        arm.begin();
146        cancelButton.click();
147        arm.end();
148        return asPage(UsersTabSubPage.class);
149    }
150
151    protected void switchCreationFormPage() {
152        if (!isImmediateCreationYesSelected()) {
153            immediateCreation.get(1).click();
154            Locator.waitUntilElementPresent(By.id("createUserView:createUser:nxl_user:nxw_passwordMatcher_firstPassword"));
155        }
156    }
157}