Created
August 27, 2016 13:56
-
-
Save ova2/9903d87ed079b0fc6eb7899793afad31 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.openqa.selenium.support.ui.WebDriverWait; | |
import org.junit.Assert; | |
import org.openqa.selenium.By; | |
import org.openqa.selenium.Keys; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.WebElement; | |
import org.openqa.selenium.interactions.Actions; | |
import org.openqa.selenium.support.PageFactory; | |
import org.openqa.selenium.support.ui.ExpectedCondition; | |
import org.openqa.selenium.support.ui.LoadableComponent; | |
import org.openqa.selenium.support.ui.WebDriverWait; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import java.lang.reflect.Method; | |
import java.util.List; | |
public abstract class LoadablePage<T extends LoadableComponent<T>> extends LoadableComponent<T> { | |
private final static Logger LOGGER = LoggerFactory.getLogger(LoadablePage.class); | |
private AbstractSeleniumTest seleniumTest; | |
private String pageUrl; | |
private Method method; | |
private LoadablePage parent; | |
/** | |
* Init method (invoked by the framework). | |
* | |
* @param seleniumTest instance of type AbstractSeleniumTest | |
* @param method to be invoked method annotated with @FlowOnPage | |
* @param parent parent page of type LoadablePage | |
*/ | |
void init(AbstractSeleniumTest seleniumTest, Method method, LoadablePage parent) { | |
this.seleniumTest = seleniumTest; | |
this.pageUrl = seleniumTest.getUrlToGo(getUrlPath()); | |
this.method = method; | |
this.parent = parent; | |
PageFactory.initElements(getDriver(), this); | |
} | |
/** | |
* Path of the URL without the context root for this page. | |
* | |
* @return String path of the URL | |
*/ | |
protected abstract String getUrlPath(); | |
/*** | |
* Specific check which has to be implemented by every page object. | |
* A rudimentary check on the basis of URL is undertaken by this class. | |
* This method is doing an extra check if the page has been proper loaded. | |
* | |
* @throws Error thrown when the check fails | |
*/ | |
protected abstract void isPageLoaded() throws Error; | |
@Override | |
protected void isLoaded() throws Error { | |
// min. check against the page URL | |
String url = getDriver().getCurrentUrl(); | |
Assert.assertTrue("You are not on the right page.", url.equals(pageUrl)); | |
// call specific check which has to be implemented on every page | |
isPageLoaded(); | |
} | |
@Override | |
protected void load() { | |
if (parent != null) { | |
// call the logic in the parent page | |
parent.get(); | |
// parent page has navigated to this page (via click on button or link). | |
// wait until this page has been loaded. | |
WebDriverWait wait = new WebDriverWait(getDriver(), 20, 250); | |
wait.until(new ExpectedCondition<Boolean> () { | |
@Override | |
public Boolean apply(WebDriver d) { | |
try { | |
isLoaded(); | |
return true; | |
} catch (AssertionError e) { | |
return false; | |
} | |
} | |
}); | |
} else { | |
// Is there no parent page, the page should be navigated directly | |
LOGGER.info("Browser: {}, GET {}", getDriver(), getPageUrl()); | |
getDriver().get(getPageUrl()); | |
} | |
} | |
/** | |
* Ensure that this page has been loaded and execute the test code on the this page. | |
* | |
* @return T LoadablePage | |
*/ | |
public T get() { | |
T loadablePage = super.get(); | |
// execute flow logic | |
seleniumTest.executeFlowOnPage(this); | |
return loadablePage; | |
} | |
/** | |
* See {@link WebDriver#findElement(By)} | |
*/ | |
public WebElement findElement(By by) { | |
return getDriver().findElement(by); | |
} | |
/** | |
* See {@link WebDriver#findElements(By)} | |
*/ | |
public List<WebElement> findElements(By by) { | |
return getDriver().findElements(by); | |
} | |
public WebDriver getDriver() { | |
return seleniumTest.getDriver(); | |
} | |
protected String getPageUrl() { | |
return pageUrl; | |
} | |
Method getMethod() { | |
return method; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment