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 org.nuxeo.functionaltests.Required; 023import org.openqa.selenium.By; 024import org.openqa.selenium.JavascriptExecutor; 025import org.openqa.selenium.NoSuchElementException; 026import org.openqa.selenium.TimeoutException; 027import org.openqa.selenium.WebDriver; 028import org.openqa.selenium.WebElement; 029import org.openqa.selenium.support.FindBy; 030 031/** 032 * Nuxeo default login page. 033 */ 034public class LoginPage extends AbstractPage { 035 036 public static final String FEEDBACK_MESSAGE_DIV_XPATH = "//div[contains(@class,'feedbackMessage')]"; 037 038 public static final String LOGIN_DIV_XPATH = "//div[@class='login']"; 039 040 @Required 041 @FindBy(id = "username") 042 WebElement usernameInputTextBox; 043 044 @Required 045 @FindBy(id = "password") 046 WebElement passwordInputTextBox; 047 048 @Required 049 @FindBy(name = "Submit") 050 WebElement submitButton; 051 052 public LoginPage(WebDriver driver) { 053 super(driver); 054 } 055 056 /** 057 * Fills in the login form with the username, password and language. 058 * 059 * @param username the username 060 * @param password the password 061 * @param language value of one of the options in the language select box. For example, English (United States) 062 * @deprecated since 9.1 not used anymore, use {@link #login(String, String)} insted. 063 */ 064 @Deprecated 065 public void login(String username, String password, String language) { 066 login(username, password); 067 } 068 069 /** 070 * Fills in the login form with the username and password. Uses the default language. 071 * 072 * @param username the username 073 * @param password the password 074 */ 075 public void login(String username, String password) { 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 submitButton.click(); 083 } 084 085 /** 086 * Logs in and returns the next page. 087 * 088 * @param username the username 089 * @param password the password 090 * @param pageClassToProxy the next page's class 091 * @return the next page 092 */ 093 public <T> T login(String username, String password, Class<T> pageClassToProxy) { 094 try { 095 login(username, password); 096 return asPage(pageClassToProxy); 097 } catch (NoSuchElementException | TimeoutException exc) { 098 try { 099 // Try once again because of problem described in NXP-12835. 100 // find the real cause of NXP-12835 and remove second login 101 // attempt 102 if (hasElement(By.xpath(LOGIN_DIV_XPATH))) { 103 login(username, password); 104 return asPage(pageClassToProxy); 105 } else { 106 throw exc; 107 } 108 } catch (NoSuchElementException e) { 109 if (hasElement(By.xpath(LOGIN_DIV_XPATH))) { 110 // Means we are still on login page. 111 if (hasElement(By.xpath(FEEDBACK_MESSAGE_DIV_XPATH))) { 112 throw new NoSuchElementException("Login failed. Application said : " 113 + driver.findElement(By.xpath(FEEDBACK_MESSAGE_DIV_XPATH)).getText(), e); 114 } else { 115 throw new NoSuchElementException("Login failed", e); 116 } 117 } else { 118 throw e; 119 } 120 } 121 } 122 } 123}