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 */
020package org.nuxeo.functionaltests.pages;
021
022import java.util.List;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.functionaltests.Required;
027import org.openqa.selenium.By;
028import org.openqa.selenium.JavascriptExecutor;
029import org.openqa.selenium.NoSuchElementException;
030import org.openqa.selenium.TimeoutException;
031import org.openqa.selenium.WebDriver;
032import org.openqa.selenium.WebElement;
033import org.openqa.selenium.support.FindBy;
034import org.openqa.selenium.support.ui.Select;
035
036/**
037 * Nuxeo default login page.
038 */
039public class LoginPage extends AbstractPage {
040
041    private static final Log log = LogFactory.getLog(LoginPage.class);
042
043    public static final String FEEDBACK_MESSAGE_DIV_XPATH = "//div[contains(@class,'feedbackMessage')]";
044
045    public static final String LOGIN_DIV_XPATH = "//div[@class='login']";
046
047    @Required
048    @FindBy(id = "username")
049    WebElement usernameInputTextBox;
050
051    @Required
052    @FindBy(id = "password")
053    WebElement passwordInputTextBox;
054
055    @Required
056    @FindBy(name = "Submit")
057    WebElement submitButton;
058
059    /**
060     * removed from login page since 5.6
061     */
062    @Deprecated
063    @FindBy(id = "language")
064    WebElement languageSelectBox;
065
066    public LoginPage(WebDriver driver) {
067        super(driver);
068    }
069
070    /**
071     * Fills in the login form with the username, password and language.
072     *
073     * @param username the username
074     * @param password the password
075     * @param language value of one of the options in the language select box. For example, English (United States)
076     */
077    public void login(String username, String password, String language) {
078        JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
079        usernameInputTextBox.sendKeys(username);
080        passwordInputTextBox.sendKeys(password);
081        jsExecutor.executeScript("document.getElementById('username').blur();return true;");
082        jsExecutor.executeScript("document.getElementById('password').blur();return true;");
083
084        if (language != null) {
085            Select languageSelect = new Select(languageSelectBox);
086
087            List<WebElement> list = languageSelect.getOptions();
088            for (WebElement webElement : list) {
089                if (language.trim().equals(webElement.getText().trim())) {
090                    languageSelect.selectByVisibleText(webElement.getText());
091                    break;
092                }
093            }
094        }
095        submitButton.click();
096    }
097
098    /**
099     * Fills in the login form with the username and password. Uses the default language.
100     *
101     * @param username the username
102     * @param password the password
103     */
104    public void login(String username, String password) {
105        login(username, password, (String) null);
106    }
107
108    /**
109     * Logs in and returns the next page.
110     *
111     * @param username the username
112     * @param password the password
113     * @param pageClassToProxy the next page's class
114     * @return the next page
115     */
116    public <T> T login(String username, String password, Class<T> pageClassToProxy) {
117        try {
118            login(username, password);
119            return asPage(pageClassToProxy);
120        } catch (NoSuchElementException | TimeoutException exc) {
121            try {
122                // Try once again because of problem described in NXP-12835.
123                // find the real cause of NXP-12835 and remove second login
124                // attempt
125                if (hasElement(By.xpath(LOGIN_DIV_XPATH))) {
126                    login(username, password);
127                    return asPage(pageClassToProxy);
128                } else {
129                    throw exc;
130                }
131            } catch (NoSuchElementException e) {
132                if (hasElement(By.xpath(LOGIN_DIV_XPATH))) {
133                    // Means we are still on login page.
134                    if (hasElement(By.xpath(FEEDBACK_MESSAGE_DIV_XPATH))) {
135                        throw new NoSuchElementException("Login failed. Application said : "
136                                + driver.findElement(By.xpath(FEEDBACK_MESSAGE_DIV_XPATH)).getText(), e);
137                    } else {
138                        throw new NoSuchElementException("Login failed", e);
139                    }
140                } else {
141                    throw e;
142                }
143            }
144        }
145    }
146}