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 * Thierry Delprat 016 */ 017package org.nuxeo.functionaltests.pages.wizard; 018 019import org.nuxeo.functionaltests.Locator; 020import org.nuxeo.functionaltests.pages.AbstractPage; 021import org.openqa.selenium.By; 022import org.openqa.selenium.WebDriver; 023import org.openqa.selenium.WebElement; 024import org.openqa.selenium.support.ui.Select; 025 026import com.google.common.base.Function; 027 028public abstract class AbstractWizardPage extends AbstractPage { 029 030 protected static final String BUTTON_LOCATOR = "//input[@class=\"glossyButton\" and @value=\"LABEL\"]"; 031 032 public AbstractWizardPage(WebDriver driver) { 033 super(driver); 034 } 035 036 public String getTitle() { 037 WebElement title = findElementWithTimeout(By.xpath("//h1")); 038 return title.getText().trim(); 039 } 040 041 protected abstract String getNextButtonLocator(); 042 043 protected abstract String getPreviousButtonLocator(); 044 045 public <T extends AbstractWizardPage> T next(Class<T> wizardPageClass) { 046 return next(wizardPageClass, null); 047 } 048 049 public <T extends AbstractWizardPage> T next(Class<T> wizardPageClass, Function<WebDriver, Boolean> function) { 050 WebElement buttonNext = findElementWithTimeout(By.xpath(getNextButtonLocator())); 051 String URLbefore = driver.getCurrentUrl(); 052 buttonNext.click(); 053 if (function == null) { 054 waitUntilURLDifferentFrom(URLbefore); 055 } else { 056 Locator.waitUntilGivenFunction(function); 057 } 058 return asPage(wizardPageClass); 059 } 060 061 public <T extends AbstractWizardPage> T previous(Class<T> wizardPageClass) { 062 return previous(wizardPageClass, null); 063 } 064 065 public <T extends AbstractWizardPage> T previous(Class<T> wizardPageClass, Function<WebDriver, Boolean> function) { 066 WebElement buttonPrev = findElementWithTimeout(By.xpath(getPreviousButtonLocator())); 067 String URLbefore = driver.getCurrentUrl(); 068 buttonPrev.click(); 069 if (function == null) { 070 waitUntilURLDifferentFrom(URLbefore); 071 } else { 072 Locator.waitUntilGivenFunction(function); 073 } 074 return asPage(wizardPageClass); 075 } 076 077 public <T extends AbstractPage> T nav(Class<T> wizardPageClass, String buttonLabel) { 078 return nav(wizardPageClass, buttonLabel, false); 079 } 080 081 public <T extends AbstractPage> T nav(Class<T> wizardPageClass, String buttonLabel, Boolean waitForURLChange) { 082 WebElement button = findNavButton(buttonLabel); 083 if (button == null) { 084 return null; 085 } 086 String URLbefore = driver.getCurrentUrl(); 087 button.click(); 088 if (waitForURLChange == true) { 089 waitUntilURLDifferentFrom(URLbefore); 090 } 091 return asPage(wizardPageClass); 092 } 093 094 public <T extends AbstractPage> T navByLink(Class<T> wizardPageClass, String linkLabel) { 095 return navByLink(wizardPageClass, linkLabel, false); 096 } 097 098 public <T extends AbstractPage> T navByLink(Class<T> wizardPageClass, String linkLabel, Boolean waitForURLChange) { 099 WebElement link = findElementWithTimeout(By.linkText(linkLabel)); 100 if (link == null) { 101 return null; 102 } 103 String URLbefore = driver.getCurrentUrl(); 104 link.click(); 105 waitUntilURLDifferentFrom(URLbefore); 106 return asPage(wizardPageClass); 107 } 108 109 public <T extends AbstractPage> T navById(Class<T> wizardPageClass, String buttonId) { 110 return navById(wizardPageClass, buttonId, false); 111 } 112 113 public <T extends AbstractPage> T navById(Class<T> wizardPageClass, String buttonId, Boolean waitForURLChange) { 114 WebElement button = findElementWithTimeout(By.id(buttonId)); 115 if (button == null) { 116 return null; 117 } 118 String URLbefore = driver.getCurrentUrl(); 119 button.click(); 120 if (waitForURLChange == true) { 121 waitUntilURLDifferentFrom(URLbefore); 122 } else { 123 try { 124 Thread.sleep(1000); 125 } catch (InterruptedException e) { 126 // do nothing 127 } 128 } 129 return asPage(wizardPageClass); 130 } 131 132 protected WebElement findNavButton(String label) { 133 return findElementWithTimeout(By.xpath(BUTTON_LOCATOR.replace("LABEL", label))); 134 } 135 136 public boolean fillInput(String name, String value) { 137 WebElement element = findElementWithTimeout(By.name(name)); 138 if (element != null) { 139 element.clear(); 140 element.sendKeys(value); 141 return true; 142 } 143 return false; 144 } 145 146 public boolean selectOption(String name, String value) { 147 WebElement element = findElementWithTimeout(By.name(name)); 148 if (element != null) { 149 Select select = new Select(element); 150 select.selectByValue(value); 151 return select.getFirstSelectedOption().getAttribute("value").equals(value); 152 } 153 return false; 154 } 155 156 public boolean selectOptionWithReload(String name, String value) { 157 158 WebElement element = findElementWithTimeout(By.name(name)); 159 if (element != null) { 160 Select select = new Select(element); 161 select.selectByValue(value); 162 // page is reload, need to fetch element again 163 element = findElementWithTimeout(By.name(name)); 164 select = new Select(element); 165 return select.getFirstSelectedOption().getAttribute("value").equals(value); 166 } 167 return false; 168 } 169 170 public boolean clearInput(String name) { 171 WebElement element = findElementWithTimeout(By.name(name)); 172 if (element != null) { 173 element.clear(); 174 return true; 175 } 176 return false; 177 } 178 179}