Package org.appfuse.webapp.integration

Source Code of org.appfuse.webapp.integration.SeleniumBaseTestCase

package org.appfuse.webapp.integration;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.subethamail.wiser.Wiser;
import org.subethamail.wiser.WiserMessage;
import org.tuckey.web.filters.urlrewrite.utils.StringUtils;

@ContextConfiguration(locations = {
        "classpath:/applicationContext-resources.xml"
})
@RunWith(SpringJUnit4ClassRunner.class)
public abstract class SeleniumBaseTestCase {

    protected final Log log = LogFactory.getLog(getClass());

    private final int waitTimeOutSeconds = 60;

    protected int smtpPort = 25250;
    public Wiser wiser;

    /**
     * WaitFor callback interface.
     *
     */
    protected interface WaitFor {
        boolean isFinished();
    }

    private String baseUrl = "http://localhost:8080/";
    private WebDriver driver;


    public String getBaseUrl() {
        return baseUrl;
    }

    public WebDriver getDriver() {
        return driver;
    }

    @Value("#{systemProperties.baseUrl}")
    public void setBaseUrl(final String baseUrl) {
        this.baseUrl = baseUrl;
    }

    public void setDriver(final WebDriver driver) {
        this.driver = driver;
    }

    @Value("${mail.port}")
    public void setSmtpPort(final int smtpPort) {
        this.smtpPort = smtpPort;
    }

    @Before
    public void setUp() throws Exception {
        log.debug("");

        log.debug("Starting wiser on port " + smtpPort);
        wiser = new Wiser();
        wiser.setPort(smtpPort);
        wiser.start();

        setDriver(new ChromeDriver());
        getDriver().manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        getDriver().manage().window().setSize(new Dimension(1024, 900));
    }


    @After
    public void tearDown() throws Exception {
        if (getDriver() != null) {
            getDriver().quit();
        }
        if (wiser != null) {
            wiser.stop();
        }
    }

    protected List<MimeMessage> getMailMessages() throws MessagingException {
        final List<MimeMessage> messages = new ArrayList<MimeMessage>();
        for (final WiserMessage message : wiser.getMessages()) {
            messages.add(message.getMimeMessage());
        }
        return messages;
    }

    protected boolean waitFor(final WaitFor waitFor) {
        boolean isFinished = false;
        for (int second = 0; second < waitTimeOutSeconds; second++) {
            try {
                if (waitFor.isFinished()) {
                    isFinished = true;
                    break;
                }
            } catch (final Exception e) {
            }
            try {
                Thread.sleep(1000);
            } catch (final InterruptedException e) {
                log.error("Error sleeping waitFor thread", e);
                throw new RuntimeException(e);
            }
        }

        return isFinished;
    }

    protected boolean waitForTitle(final WebDriver driver, final String title) {
        return waitFor(new WaitFor() {
            @Override public boolean isFinished() {
                return driver.getTitle().matches(title);
            }
        });
    }

    protected boolean waitForElement(final By selector) {
        return waitFor(new WaitFor() {
            @Override public boolean isFinished() {
                return isElementPresent(selector);
            }
        });
    }

    protected boolean waitForElementNotPresent(final By selector) {
        return waitFor(new WaitFor() {
            @Override public boolean isFinished() {
                return !isElementPresent(selector);
            }
        });
    }

    protected boolean waitForAlert() {
        return waitForAlert(null);
    }

    protected boolean waitForAlert(final String alertText) {
        return waitFor(new WaitFor() {
            @Override public boolean isFinished() {
                String regex = alertText;
                if (StringUtils.isBlank(regex) || "*".equals(regex)) {
                    regex = "^[\\s\\S]*$";
                }
                return closeAlertAndGetItsText(true).matches(regex);
            }
        });
    }

    protected boolean isElementPresent(final By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (final NoSuchElementException e) {
            return false;
        }
    }

    private boolean isAlertPresent() {
        try {
            driver.switchTo().alert();
            return true;
        } catch (final NoAlertPresentException e) {
            return false;
        }
    }

    private String closeAlertAndGetItsText(final boolean accept) {
        final Alert alert = driver.switchTo().alert();
        final String alertText = alert.getText();
        if (accept) {
            alert.accept();
        } else {
            alert.dismiss();
        }
        return alertText;
    }

}
TOP

Related Classes of org.appfuse.webapp.integration.SeleniumBaseTestCase

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.