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 */
017
018package org.nuxeo.functionaltests.pages.wizard;
019
020import java.util.ArrayList;
021import java.util.List;
022
023import org.openqa.selenium.By;
024import org.openqa.selenium.WebDriver;
025import org.openqa.selenium.WebElement;
026
027import com.google.common.base.Function;
028
029public class WizardPage extends AbstractWizardPage {
030
031    // protected static final String NEXT_BUTTON_LOCATOR = "//input[@class=\"glossyButton\" and @value=\"Next step\"]";
032    // protected static final String PREV_BUTTON_LOCATOR =
033    // "//input[@class=\"glossyButton\" and @value=\"Previous step\"]";
034    protected static final String NEXT_BUTTON_LOCATOR = "id('btnNext')";
035
036    protected static final String PREV_BUTTON_LOCATOR = "id('btnPrev')";
037
038    public WizardPage(WebDriver driver) {
039        super(driver);
040        IFrameHelper.focusOnWizardPage(driver);
041    }
042
043    public WizardPage next() {
044        return next(false);
045    }
046
047    public WizardPage next(Boolean errorExpected) {
048        if (errorExpected) {
049            return next(WizardPage.class, new Function<WebDriver, Boolean>() {
050                @Override
051                public Boolean apply(WebDriver driver) {
052                    return hasError();
053                }
054            });
055        } else {
056            return next(WizardPage.class);
057        }
058    }
059
060    public WizardPage previous() {
061        return previous(false);
062    }
063
064    public WizardPage previous(Boolean errorExpected) {
065        if (errorExpected) {
066            return previous(WizardPage.class, new Function<WebDriver, Boolean>() {
067                @Override
068                public Boolean apply(WebDriver driver) {
069                    return hasError();
070                }
071            });
072        } else {
073            return previous(WizardPage.class);
074        }
075    }
076
077    @Override
078    protected String getNextButtonLocator() {
079        return NEXT_BUTTON_LOCATOR;
080    }
081
082    @Override
083    protected String getPreviousButtonLocator() {
084        return PREV_BUTTON_LOCATOR;
085    }
086
087    public ConnectWizardPage getConnectPage() {
088        return asPage(ConnectWizardPage.class);
089    }
090
091    public boolean hasError() {
092        return getErrors().size() > 0;
093    }
094
095    public List<String> getErrors() {
096        List<WebElement> errorsEl = driver.findElements(By.xpath("//div[@class='errBlock']/div[@class='errItem']"));
097        List<String> errors = new ArrayList<String>();
098        for (WebElement errorEl : errorsEl) {
099            if (errorEl.getText() != null && errorEl.getText().length() > 0) {
100                errors.add(errorEl.getText());
101            }
102        }
103        return errors;
104    }
105
106    public List<String> getInfos() {
107        List<WebElement> infosEl = driver.findElements(By.xpath("//div[@class='infoBlock']/div[@class='infoItem']"));
108        List<String> infos = new ArrayList<String>();
109        for (WebElement infoEl : infosEl) {
110            if (infoEl.getText() != null && infoEl.getText().length() > 0) {
111                infos.add(infoEl.getText());
112            }
113        }
114        return infos;
115    }
116
117    public boolean hasInfo() {
118        return getInfos().size() > 0;
119    }
120
121    public String getTitle2() {
122        WebElement title2 = findElementWithTimeout(By.xpath("//h2"));
123        if (title2 == null) {
124            return null;
125        }
126        return title2.getText();
127    }
128
129}