首页 > 技术文章 > 基于Java+Selenium的WebUI自动化测试框架(十三)-----基础页面类BasePage(Excel)

generalli2019 2019-09-02 09:45 原文

  前面,我们讲了如何使用POI进行Excel的“按需读取”。根据前面我们写的BasePageX,我们可以很轻松的写出来基于这个“按需读取”的BasePage。

package webui.xUtils;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.util.HashMap;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.Reporter;

public class BasePageE extends UIExcutorImpl{
        protected WebDriver driver;    
        protected String pageName;
        // 页面名称    
        protected String xmlPath;
        // 页面元素配置文件路径    
        protected HashMap<String, Position> positionMap;
        //存储页面元素信息    
        protected logUtil log = new logUtil(BasePageE.class); 
        Position position = null;
        public BasePageE(WebDriver driver, String pageName,String excelpath)  throws Exception {        
            super(driver);
            this.driver = driver;        
            this.pageName = pageName;
            positionMap = ExcelReadUtil.readExcelDocuement(excelpath, pageName);
            log.info("成功读取:" + pageName + "页面信息");
            Reporter.log("成功读取:" + pageName + "页面信息");
            }

        public void click(String positionName) throws Exception {        
            super.click(getPosition(positionName));    
            }     
        public void sendKey(String positionName, String value) throws Exception {        
            super.sendKey(getPosition(positionName), value);            
            }     
        public String getText(String positionName) throws Exception {        
            return super.getText(getPosition(positionName));    
            }     
        public String getAttribute(String positionName,String value) throws Exception {        
            return super.getAttribute(getPosition(positionName), value);    
            } 
        public WebElement getElement(String positionName) throws Exception {        
            return super.getElement(getPosition(positionName));    
            }     
        public boolean isElementDisplayed(String positionName) throws Exception {        
            return super.isElementDisplayed(getPosition(positionName));    
            }     
        @Override
        public void switchWindow(String title) {        
            super.switchWindow(title);    
            log.info("切换窗口");
            Reporter.log("切换窗口"+title);
            }     
        public void switchFrame(String positionName) {        
            super.switchFrame(getPosition(positionName));    
            log.info("切换frame至:" + positionName);
            Reporter.log("切换frame至:" + positionName);
            }     
        @Override
        public String getAlertText() {
            return super.getAlertText();
        }
        public void mouseMoveClick(int x , int y) throws AWTException {
            Robot rb1 = new Robot();
            rb1.mouseMove(x,y);
            rb1.delay(500);
            rb1.mousePress(InputEvent.BUTTON1_DOWN_MASK);
            rb1.delay(500);
            rb1.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
            rb1.delay(500);
            log.info("将鼠标移动至:" + "(" + x +"," + y + ")");
            Reporter.log("将鼠标移动至:" + "(" + x +"," + y + ")");
        }
        public void jsClick(String positionName) throws Exception {        
            super.jsClick(getPosition(positionName));    
            }     
        public void waitElement(String positionName,int sec) {
            super.waitElement(getPosition(positionName), sec);
        }

        /**     * 根据positionName返回对应的position
        */    
        public Position getPosition(String positionName) {        
            Position position = null;        
            if (positionMap != null) {            
                position = positionMap.get(positionName);        
                }
            if(position ==null) {
                log.error("没有找到"+positionName+"页面元素");
                Reporter.log("没有找到"+positionName+"页面元素");
            }
            return position;    
            }
}

  这样,我们就完成了使用Exce读取页面元素的基础页面类。

  大家如果回顾之前的内容,就可以发现,我们根据读取方式的不同,可以写出不同的基础页面类。BasePageX----->基于XML读取,BasePageE------>基于Excel读取,那么我们也可以写BasePageM------>基于Mysql读取,BasePageO------>基于Oracle读取等等。细心的童鞋会发现,我能不能只写一个类,然后把读取方式作为参数放进去,然后根据读取方式参数的不同来定义我想要的BasePage呢?当然是可以的。大家可以自行扩展,我在这里就不赘述了。

  

  

推荐阅读