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