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