holmos.webtest.struct.Frame Maven / Gradle / Ivy
package holmos.webtest.struct;
import holmos.webtest.Allocator;
import holmos.webtest.BrowserWindow;
import holmos.webtest.SeleniumBrowserWindow;
import holmos.webtest.WebDriverBrowserWindow;
import holmos.webtest.basetools.HolmosBaseTools;
import holmos.webtest.constvalue.ConfigConstValue;
import holmos.webtest.constvalue.ConstValue;
import holmos.webtest.element.Element;
import holmos.webtest.element.locator.Locator;
import holmos.webtest.element.locator.LocatorChain;
import holmos.webtest.element.tool.SeleniumElementExist;
import holmos.webtest.element.tool.WebDriverElementExist;
import holmos.webtest.element.tool.WebElementExist;
import holmos.webtest.log.MyLogger;
import java.lang.reflect.Field;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import com.thoughtworks.selenium.Selenium;
/**页面的Frame结构,支持两种结构(1)Frame(2)iframe 继承于Page
*
* @author 吴银龙([email protected])
* */
public class Frame extends Page{
/**指示此Frame在上级Frame或者Page里面的索引号*/
private int index=-1;
public int getLocatorByIndex() {
return index;
}
private WebElementExist exist;
/**用来指示当前的Frame对象是否被选中*/
/**此元素的全名,在css校验的时候,为了给css本地文件取名设置的变量信息*/
protected String fullName="";
/**此Frame的父容器*/
protected Page parent;
public void setParent(Page parent) {
this.parent = parent;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
private WebElement element;
public WebElement getElement() {
return element;
}
public void setElement(WebElement element) {
this.element = element;
}
/**Frame的定位器*/
private Locator locator=new Locator();
/**Frame操作的日志记录器*/
private static MyLogger logger=MyLogger.getLogger(Frame.class);
public Locator getLocator() {
return locator;
}
public void setLocator(Locator locator) {
this.locator = locator;
}
public Frame(String comment){
super();
this.setSelected(false);
this.comment=comment;
this.init();
}
public void addIDlocator(String id){
this.locator.addIdLocator(id);
}
public void addNameLocator(String name){
this.locator.addNameLocator(name);
}
public void addXpathLocator(String xpath){
this.locator.addXpathLocator(xpath);
}
public void addCSSLocator(String css){
this.locator.addCSSLocator(css);
}
public void addLinkTextLocator(String linkText){
this.locator.addLinkTextLocator(linkText);
}
public void addPartialLinkTextLocator(String partialLinkText){
this.locator.addPartialLinkTextLocator(partialLinkText);
}
public void addAttributeLocator(String attributeName, String attributeValue) {
this.locator.addAttributeLocator(attributeName, attributeValue);
}
public void addTagNameLocator(String tagName){
this.locator.addTagNameLocator(tagName);
}
public void addClassLocator(String className){
this.locator.addClassLocator(className);
}
public void addIndexLocator(int index){
this.index=index;
}
/**收集页面元素,这是一个简单的观察者模式的应用
* 以此来获取页面元素的信息,在重新写locator
* 和comment的时候用得到
*/
protected void init(){
Field[] fields=this.getClass().getFields();
try{
for(Field field : fields){
if(field.getModifiers()==ConstValue.nestedFatherObjectModifier){
continue;
}Object o=field.get(this);
if(o instanceof Element){
HolmosBaseTools.insertElementName((Element)o, field.getName());
((Element)o).getInfoChain().addParentNode(this.getInfoChain().getInfoNodes());
((Element) o).getInfoChain().addNode(this);
((Element) o).setFullName(getClass().getName().substring(getClass().getName().lastIndexOf('.')+1)+field.getName());
elements.add((Element)o);
}else if(o instanceof SubPage){
HolmosBaseTools.insertSubPageName((SubPage)o, field.getName());
((SubPage)o).getInfoChain().addParentNode(this.getInfoChain().getInfoNodes());
((SubPage) o).getInfoChain().addNode(this);
((SubPage) o).setFullName(getClass().getName().substring(getClass().getName().lastIndexOf('.')+1)+field.getName());
this.subpages.add((SubPage)o);
((SubPage)o).init();
}else if(o instanceof Collection){
HolmosBaseTools.insertCollectionName((Collection)o, field.getName());
((Collection)o).getInfoChain().addParentNode(this.getInfoChain().getInfoNodes());
((Collection)o).getInfoChain().addNode(this);
((Collection)o).setFullName(getClass().getName().substring(getClass().getName().lastIndexOf('.')+1)+field.getName());
this.collections.add((Collection)o);
((Collection)o).init();
}else if(o instanceof Frame){
HolmosBaseTools.insertFrameName((Frame)o, field.getName());
((Frame)o).getInfoChain().addParentNode(this.getInfoChain().getInfoNodes());
((Frame)o).getInfoChain().addNode(this);
((Frame)o).setFullName(getClass().getName().substring(getClass().getName().lastIndexOf('.')+1)+field.getName());
this.frames.add((Frame)o);
((Frame)o).setParent(this);
((Frame)o).init();
}
}
}catch(IllegalAccessException e){
e.printStackTrace();
}catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
/**Frame里面所包含的链信息
* 用来保存这个节点所用到的comment和locator信息
*/
private LocatorChain infoChain=new LocatorChain();
public LocatorChain getInfoChain() {
return infoChain;
}
/**
* 查看Frame是否存在,等待加载waitCount次
* */
private boolean isFrameExist(int waitCount){
BrowserWindow currentWindow=Allocator.getInstance().currentWindow;
if(currentWindow instanceof SeleniumBrowserWindow){
exist=new SeleniumElementExist(this);
}
else if(currentWindow instanceof WebDriverBrowserWindow){
exist=new WebDriverElementExist(this);
}
return exist.isElementExist(waitCount);
}
/**
* 检查此Frame是否存在
* @return true 存在
false 不存在
* */
public boolean isExist(){
return isFrameExist(ConfigConstValue.defaultWaitCount);
}
/**选中当前的Frame,如果有多级Frame,需要按照层级
* 级级递进的顺序,步步选择
*/
private void selectFrame(){
StringBuilder message=new StringBuilder();
BrowserWindow currentWindow=Allocator.getInstance().currentWindow;
if(isExist()){
message.append(this.comment+":");
if(currentWindow instanceof SeleniumBrowserWindow){
((Selenium)currentWindow.getDriver().getEngine()).selectFrame(locator.getSeleniumCurrentLocator());
setSelected(true);parent.setSelected(false);
}else if(currentWindow instanceof WebDriverBrowserWindow){
try{
if(locator.getLocatorById()!=null)
((WebDriver)currentWindow.getDriver().getEngine()).switchTo().frame(locator.getLocatorById());
else if(locator.getLocatorByName()!=null){
((WebDriver)currentWindow.getDriver().getEngine()).switchTo().frame(locator.getLocatorByName());
}else if(getLocatorByIndex()!=-1){
((WebDriver)currentWindow.getDriver().getEngine()).switchTo().frame(index);
}
else{
((WebDriver)currentWindow.getDriver().getEngine()).switchTo().frame(element);
}
message.append("定位Frame成功!现在的控制权交予这个"+comment+" Frame!");
logger.info(message);
setSelected(true);
parent.setSelected(false);
}catch (Exception e) {
setSelected(false);
message.append("定位Frame失败,此Frame元素存在,但是由于某种原因定位出错"+comment+" Frame!");
logger.error(message);
}
}
}else{
logger.error(comment+" frame不存在");
setSelected(false);
}
}
public void select(){
if(!isSelected()){
selectTopPage();//将控制权交予主页面
for(int i=1;i
* 默认等待时间是30s
* */
public boolean waitForExist() {
if(!isFrameExist(ConfigConstValue.waitCount)){
logger.error("元素"+comment+"一直没有出现");
return false;
}return true;
}
/**等待SubPage消失,失败打错误日志,程序一直运行*/
public void waitForDisppear() {
int waitCount=0;
while(waitCount++