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.admincenter;
019
020import org.nuxeo.functionaltests.pages.LoginPage;
021import org.openqa.selenium.By;
022import org.openqa.selenium.WebDriver;
023import org.openqa.selenium.WebElement;
024
025public class SystemHomePage extends AdminCenterBasePage {
026
027    public static final String HOST_SUBTAB = "Host";
028
029    public static final String SETUP_SUBTAB = "Setup";
030
031    private static final int RESTART_TIMEOUT_MINUTES = 10;
032
033    public SystemHomePage(WebDriver driver) {
034        super(driver);
035    }
036
037    public LoginPage restart() {
038        if (!HOST_SUBTAB.equals(getSelectedSubTab())) {
039            selectSubTab(HOST_SUBTAB);
040        }
041        WebElement restartButton = findElementWithTimeout(By.xpath("//input[@type='submit' and @value='Restart server']"));
042        if (restartButton != null) {
043            restartButton.click();
044            // Trying wait until on failing alert.accept on some machine:
045            // org.openqa.selenium.WebDriverException:
046            // a.document.getElementsByTagName("dialog")[0] is undefined
047
048            // Confirmation alert is disabled during integration tests
049            // (hostInfo.xhtml),
050            // because sometimes webdriver fails to focus on it.
051
052            // new WaitUntil(4000) {
053            // @Override
054            // public boolean condition() {
055            // Alert alert = driver.switchTo().alert();
056            // alert.accept();
057            // return true;
058            // }
059            // }.waitUntil();
060        } else {
061            return null;
062        }
063        findElementWithTimeout(By.id("username"), RESTART_TIMEOUT_MINUTES * 60 * 1000);
064        return asPage(LoginPage.class);
065    }
066
067    public boolean setConfig(String id, String value) {
068        if (!SETUP_SUBTAB.equals(getSelectedSubTab())) {
069            selectSubTab(SETUP_SUBTAB);
070        }
071        WebElement input = findElementWithTimeout(By.xpath("//td[@id='" + id + "']/input"));
072        if (input != null) {
073            input.sendKeys(value);
074            return true;
075        }
076        return false;
077    }
078
079    public String getConfig(String id) {
080        if (!SETUP_SUBTAB.equals(getSelectedSubTab())) {
081            selectSubTab(SETUP_SUBTAB);
082        }
083        WebElement input = findElementWithTimeout(By.xpath("//td[@id='" + id + "']/input"));
084        if (input != null) {
085            return input.getAttribute("value");
086        }
087        return null;
088    }
089
090}