com.tascape.reactor.webui.driver.WebPage Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of reactor-x-webui Show documentation
Show all versions of reactor-x-webui Show documentation
Reactor Extension for Web UI Automation
The newest version!
/*
* Copyright (c) 2015 - present Nebula Bay.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.tascape.reactor.webui.driver;
import com.tascape.reactor.webui.comm.WebBrowser;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.LoadableComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author linsong wang
*/
@SuppressWarnings("ProtectedField")
public abstract class WebPage extends LoadableComponent {
private static final Logger LOG = LoggerFactory.getLogger(WebPage.class);
protected WebApp app;
protected WebBrowser webBrowser;
@CacheLookup
@FindBy(tagName = "body")
protected WebElement body;
/**
* Gets the path of this page, comparing to web app base URL.
*
* @return such as /photos/edit.html
*/
public abstract String getPath();
protected void setApp(WebApp app) {
this.app = app;
this.webBrowser = app.getWebBrowser();
}
@Override
protected void load() {
String url = app.getBaseUrl() + this.getPath();
LOG.debug("load page {}", url);
webBrowser.get(url);
}
protected void load(WebElement element) {
LOG.trace("load page by clicking {}", element);
webBrowser.click(element);
checkIsLoaded();
}
public void hasLoaded() {
checkIsLoaded();
}
public void refresh() {
LOG.debug("refresh page {}", webBrowser.getWebDriver().getCurrentUrl());
webBrowser.navigate().refresh();
}
/**
* Blocks current thread for specified milliseconds. Shortcut to Thread.sleep.
* Throws RuntimeException.
*
* @param millis milliseconds
*/
public void delay(int millis) {
webBrowser.delay(millis);
}
/**
* Shows a fragment of a web page.
*
* @param fragment type
* @param fragmentClass fragment class
*
* @return fragment instance
*/
public T show(Class fragmentClass) {
return this.show(null, fragmentClass);
}
/**
* Shows a fragment of a web page.
*
* @param fragment type
* @param element web element to click
* @param fragmentClass fragment class
*
* @return fragment instance
*/
public T show(WebElement element, Class fragmentClass) {
if (element != null) {
webBrowser.click(element);
}
WebFragment fragment = PageFactory.initElements(webBrowser.getWebDriver(), fragmentClass);
fragment.setPage(this);
fragment.load();
return fragmentClass.cast(fragment);
}
public WebApp getApp() {
return app;
}
public void takeBrowserScreenshot() {
app.takeBrowserScreenshot();
}
private void checkIsLoaded() {
try {
isLoaded();
} catch (Error e) {
takeBrowserScreenshot();
throw e;
}
}
}