
ui.auto.core.pagecomponent.PageObject Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of page-component Show documentation
Show all versions of page-component Show documentation
Selenium Page Object pattern extension to support composite components
/*
Copyright 2010-2012 Michael Braiman
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 ui.auto.core.pagecomponent;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.pagefactory.DefaultElementLocatorFactory;
import ui.auto.core.context.PageComponentContext;
import ui.auto.core.data.DataTypes;
import ui.auto.core.data.PageComponentDataConverter;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamOmitField;
import datainstiller.data.DataAliases;
import datainstiller.data.DataGenerator;
import datainstiller.data.DataPersistence;
import datainstiller.data.DataValueConverter;
/**
* @author Michael Braiman [email protected]
*
* This is main Page Object class and it includes all the page component manipulation methods.
* All user defined page object classes must inherit this class and override required constructors.
*/
public class PageObject extends DataPersistence{
@XStreamOmitField
private PageComponentContext context;
@XStreamOmitField
private String currentUrl;
@XStreamOmitField
private boolean ajaxIsUsed;;
protected PageObject(T context){
initPage(context);
}
protected PageObject(T context,String expectedUrl){
initPage(context,expectedUrl);
}
protected PageObject(T context,boolean ajaxIsUsed){
initPage(context,ajaxIsUsed);
}
protected PageObject(T context,String expectedUrl,boolean ajaxIsUsed){
initPage(context,expectedUrl,ajaxIsUsed);
}
protected PageObject() {}
private DataGenerator getGenerator(){
List converters = new ArrayList<>();
converters.add(new PageComponentDataConverter());
return new DataGenerator(converters);
}
private void addToGlobalAliases(DataPersistence data){
DataAliases global = PageComponentContext.getGlobalAliases();
DataAliases local = data.getDataAliases();
if (local !=null ) {
global.putAll(local);
}
}
@Override
public T fromXml(String xml,boolean resolveAliases) {
T data = super.fromXml(xml, resolveAliases);
addToGlobalAliases(data);
return data;
}
@Override
public T fromURL(URL url, boolean resolveAliases) {
T data = super.fromURL(url, resolveAliases);
addToGlobalAliases(data);
return data;
}
@Override
public T fromInputStream(InputStream inputStream, boolean resolveAliases) {
T data = super.fromInputStream(inputStream, resolveAliases);
addToGlobalAliases(data);
return data;
}
@Override
public T fromFile(String filePath, boolean resolveAliases) {
T data = super.fromFile(filePath, resolveAliases);
addToGlobalAliases(data);
return data;
}
@Override
public void generateData() {
DataPersistence obj = getGenerator().generate(this.getClass());
deepCopy(obj, this);
}
@Override
public String generateXML() {
DataPersistence obj = getGenerator().generate(this.getClass());
return obj.toXML();
}
@Override
public String toXML(){
String header="\n\n";
XStream xstream=getXstream();
// If this class extends PageObject and it initialized with context
// then all the WebComponent fields of this class are CGLIB proxies and by default xml node
// is marked with attribute class. This will remove class attribute from the xml node
if ( PageObject.class.isAssignableFrom(this.getClass()) &&
((PageObject)this).getContext()!=null){
xstream.aliasSystemAttribute(null,"class");
}
String xml=xstream.toXML(this);
return header + xml;
}
public void initPage(T context,boolean ajaxIsUsed){
this.context=context;
this.ajaxIsUsed=ajaxIsUsed;
currentUrl=context.getDriver().getCurrentUrl();
if (ajaxIsUsed){
AjaxVisibleElementLocatorFactory ajaxVisibleElementLocatorFactory=new AjaxVisibleElementLocatorFactory(context.getDriver(),context.getAjaxTimeOut());
PageFactory.initElements(new ComponentFieldDecorator(ajaxVisibleElementLocatorFactory,this),this);
}else {
DefaultElementLocatorFactory defLocFactory=new DefaultElementLocatorFactory(context.getDriver());
PageFactory.initElements(new ComponentFieldDecorator(defLocFactory, this),this);
}
}
public void initPage(T context){
initPage(context,true);
}
public void initPage(T context,String expectedUrl){
initPage(context,expectedUrl,true);
}
private void initPage(T context, String expectedUrl,boolean ajaxIsUsed) {
initPage(context,ajaxIsUsed);
if (expectedUrl!=null) waitForUrl(expectedUrl);
}
@SuppressWarnings("unchecked")
public T getContext(){
return (T) context;
}
public void waitForUrl(String expectedUrl){
long endTime=System.currentTimeMillis() + context.getWaitForUrlTimeOut();
while (System.currentTimeMillis()
© 2015 - 2025 Weber Informatics LLC | Privacy Policy