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