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