001/*
002 * (C) Copyright 2006-2010 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 *     bstefanescu
018 */
019package org.nuxeo.ecm.webengine.test;
020
021import org.nuxeo.runtime.test.runner.web.WebPage;
022import org.openqa.selenium.By;
023import org.openqa.selenium.NoSuchElementException;
024import org.openqa.selenium.WebDriverException;
025import org.openqa.selenium.WebElement;
026import org.openqa.selenium.support.FindBy;
027import org.openqa.selenium.support.How;
028
029/**
030 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
031 */
032public class LoginPage extends WebPage {
033
034    @FindBy(how = How.ID, using = "username")
035    protected WebElement inputUsername;
036
037    @FindBy(how = How.ID, using = "password")
038    protected WebElement inputPassword;
039
040    @FindBy(how = How.NAME, using = "nuxeo_login")
041    protected WebElement login;
042
043    @FindBy(how = How.ID, using = "logout")
044    protected WebElement logout;
045
046    @FindBy(how = How.ID, using = "logstate")
047    protected WebElement logstate;
048
049    public void login(String username, String password) {
050        inputUsername.clear();
051        inputUsername.sendKeys(username);
052        inputPassword.clear();
053        inputPassword.sendKeys(password);
054        login.click();
055    }
056
057    public void ensureLogin(String username, String password) {
058        login(username, password);
059        isAuthenticated(5);
060    }
061
062    public void logout() {
063        logout.click();
064    }
065
066    public void ensureLogout() {
067        logout();
068        isNotAuthenticated(5);
069    }
070
071    public boolean isAuthenticated(int timeoutInSeconds) {
072        try {
073            findElement(By.id("logout"), timeoutInSeconds);
074            return true;
075        } catch (WebDriverException e) {
076            return false;
077        }
078    }
079
080    public boolean isNotAuthenticated(int timeoutInSeconds) {
081        try {
082            findElement(By.id("login"), timeoutInSeconds);
083            return true;
084        } catch (WebDriverException e) {
085            return false;
086        }
087    }
088
089    public boolean isAuthenticated() {
090        try {
091            driver.findElement(By.id("logout"));
092            return true;
093        } catch (NoSuchElementException e) {
094            return false;
095        }
096    }
097
098}