基於Java+Selenium的WebUI自動化測試框架(九)-----基礎頁面類(BasePage)


  上篇我們寫了java讀取xml文件的類,實現了可以從xml文件讀取元素的方式。那么,接下來我們需要考慮一個問題。我們拿了這些元素之后怎么去操作呢?

  先來看看我們手工測試的時候是怎么進行的。

  雙擊瀏覽器,打開網站(瀏覽器初始化),然后在打開的網頁上進行一些操作(比如輸入,點擊什么的)。假如,我們根據每個頁面來寫一個類,這樣的話如果有幾百個頁面,我們就要封裝幾百個類,這樣做也是非常的麻煩和復雜!,也不利於自動化腳本的維護。

  進一步想想,其實我們在每個頁面上所做的操作也就那么幾種。(輸入文字,點擊,得到某元素的文字,查看某元素是否顯示,切換frame,切換窗口,處理彈窗等等。)根據這些頁面上操作的共性,我們可以設計一個基礎頁面類,使用這個基礎頁面類來對各個具體的頁面進行實例化。那么基礎頁面類中的方法,我們就可以在實例化的具體頁面中進行調用。

  由於目前我們只寫了讀取XML的類。因此,我們就先寫一個支持XML讀取的基礎頁面類。

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 BasePageX extends UIExcutorImpl {    
        protected WebDriver driver;    
        protected String pageName;
        // 頁面名稱    
        protected String xmlPath;
        // 頁面元素配置文件路徑    
        protected HashMap<String, Position> positionMap;
        //存儲頁面元素信息    
        protected logUtil log = new logUtil(BasePageX.class); 
        Position position = null;
        public BasePageX(WebDriver driver, String pageName,String xmlName)  throws Exception {        
            super(driver);
            this.driver = driver;        
            this.pageName = pageName;        // 獲取page.xml路徑,page.xml在同級目錄
            xmlPath = this.getClass().getResource("").getPath() + xmlName;    
            positionMap = XmlReadUtil.readXMLDocument(xmlPath, 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();
        }
     //使用Robot強制點擊某處坐標,用於無法定位的元素,比如(Object類型的元素)
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; } }

     這樣,完成了基礎頁面類之后。我們可以使用以下的代碼來定義一個頁面的實例,然后使用該實例來調用基礎頁面類的方法,從而實現操作頁面的目的。例如:

BasePageX loginPage = new BasePageX(driver,"loginPage",doc_XmlPath);
loginPage.click("登錄");

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM