/**
 * File: LoginPage.java 27.05.2014, 12:33:23, author: mreinhardt
 * 
 * Project: OwnCloud
 *
 * https://www.martinreinhardt-online.de/apps
 */
package de.martinreinhardt.owncloud.webtest.pages;

import net.thucydides.core.annotations.DefaultUrl;
import net.thucydides.core.annotations.findby.FindBy;
import net.thucydides.core.reports.adaptors.xunit.model.TestError;

import org.apache.log4j.Logger;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import de.martinreinhardt.owncloud.webtest.util.UITestConstants;

@DefaultUrl(UITestConstants.DEFAULT_URL)
public class RoundCubePage extends PortalPage {

	// Logger
	private static final Logger LOG = Logger.getLogger(RoundCubePage.class);

	/**
	 * iframe id
	 */
	private static final String ROUNDCUBE_FRAME = "roundcubeFrame";

	/**
	 * reference id of loader item
	 */
	private static final String ROUNDCUBE_LOADER = "roundcubeLoader";
	/**
	 * reference xpath to new loader
	 */
	private static final String ROUNDCUBE_LOADER_NEW = "//*[contains(@class,'loading')]";

	@FindBy(id = ROUNDCUBE_FRAME)
	private WebElement rcFrame;

	@FindBy(id = "errorMsg")
	private WebElement errorMsg;

	@FindBy(id = "rcmloginuser")
	private WebElement rcLogin;

	@FindBy(xpath = "(//*[contains(@class,'messagelist')]//tbody//td[contains(@class,'subject')]/a)[1]")
	private WebElement firstEmail;

	@FindBy(id = ROUNDCUBE_LOADER)
	private WebElement ajaxLoader;

	@FindBy(xpath = ROUNDCUBE_LOADER_NEW)
	private WebElement newAjaxLoader;

	public RoundCubePage(final WebDriver pWebDriver) {
		super(pWebDriver);
	}

	/**
	 * Waits for the ajax status loader to disappear
	 * 
	 * @throws TestError
	 */
	private void wait_for_rc_load() throws TestError {
		this.load_iFrame(ROUNDCUBE_FRAME);
		waitFor(500).milliseconds();
		try {
			if (element(newAjaxLoader).isCurrentlyVisible()) {
				waitFor(50).milliseconds();
				// wait for loader icon to disappear
				element(newAjaxLoader).waitUntilNotVisible();
				if (isRcLoginDisplayed()) {
					throw new TestError("Roundcube Login should not be visible!");
				}
				LOG.info("AJAX loader disappeared. Loading complete...");
			}
		} catch (final NoSuchElementException e) {
			LOG.info("New AJAX Roundcube loader not found");
		}
		try {
			if (element(ajaxLoader).isCurrentlyVisible()) {
				waitFor(50).milliseconds();
				// wait for loader icon to disappear
				element(ajaxLoader).waitUntilNotVisible();
				if (isRcLoginDisplayed()) {
					throw new TestError("Roundcube Login should not be visible!");
				}
				LOG.info("AJAX loader disappeared. Loading complete...");
			}
		} catch (final NoSuchElementException e) {
			LOG.info("AJAX Roundcube loader not found");
		}
	}

	public String getFirstMessageSubject() throws TestError {
		wait_for_rc_load();
		String result = null;
		load_iFrame(ROUNDCUBE_FRAME);
		clickOn(firstEmail);
		result = firstEmail.getText();
		back_to_parent_document();
		return result;
	}

	public boolean isRcLoginDisplayed() throws TestError {
		wait_for_rc_load();
		boolean loginVisible = false;
		try {
			loginVisible = rcLogin.isDisplayed();
		} catch (final NoSuchElementException e) {
		}
		back_to_parent_document();
		return loginVisible;
	}

	public boolean isErrorMessageDisplayed() throws TestError {
		wait_for_rc_load();
		boolean errorDisplayed = false;
		try {
			errorDisplayed = errorMsg.isDisplayed();
		} catch (final NoSuchElementException e) {
		}
		back_to_parent_document();
		return errorDisplayed;
	}

	public boolean isShowingRcFrame() throws TestError {
		wait_for_rc_load();
		boolean frameLoaderd = false;
		try {
			frameLoaderd = rcFrame.isDisplayed();
		} catch (final NoSuchElementException e) {
		}
		back_to_parent_document();
		return frameLoaderd;
	}
}

@@ -1302,43 +1302,8 @@
 %22(//
-*%5Bcontains(@class,'messagelist')%5D//
 tbod
@@ -1774,39 +1774,8 @@
 E);%0A
-%09%09waitFor(500).milliseconds();%0A
 %09%09tr
@@ -1836,40 +1836,8 @@
 ) %7B%0A
-%09%09%09%09waitFor(50).milliseconds();%0A
 %09%09%09%09
@@ -2129,32 +2129,33 @@
 ementException e
+1
 ) %7B%0A%09%09%09LOG.info(
@@ -2199,19 +2199,16 @@
 );%0A%09
-%09%7D%0A
 %09%09try %7B%0A
 %09%09%09i
@@ -2195,32 +2195,33 @@
 und%22);%0A%09%09%09try %7B%0A
+%09
 %09%09%09if (element(a
@@ -2260,39 +2260,8 @@
  %7B%0A%09
-%09%09%09waitFor(50).milliseconds();%0A
 %09%09%09%09
@@ -2289,32 +2289,33 @@
 on to disappear%0A
+%09
 %09%09%09%09element(ajax
@@ -2337,32 +2337,33 @@
 ilNotVisible();%0A
+%09
 %09%09%09%09if (isRcLogi
@@ -2375,32 +2375,33 @@
 layed()) %7B%0A%09%09%09%09%09
+%09
 throw new TestEr
@@ -2443,34 +2443,36 @@
 visible!%22);%0A%09%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09%09LOG.info(%22AJ
@@ -2512,34 +2512,36 @@
 mplete...%22);%0A%09%09%09
+%09
 %7D%0A
+%09
 %09%09%7D catch (final
@@ -2565,23 +2565,25 @@
 eption e
+2
 ) %7B%0A%09%09%09
+%09
 LOG.info
@@ -2619,16 +2619,21 @@
 ound%22);%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%0A

Make UI test working in all roundcube versions