Based on Java + Selenium automated testing framework the WebUI (ix) ----- base page class (BasePage)

  We wrote articles on java read xml file class that implements elements can be read from xml file mode. Well, then we need to consider a problem. After we took those elements how to operate it?

  Let's look at our time is how the manual testing carried out.

  Double-click browser, open the website (browser initialization), then some operations (such as input, what a click) on an open Web page. If we write a class based on each page, so if there are several hundred pages, we have to package hundreds of class, this is very troublesome and complicated! Is not conducive to automated maintenance scripts.

  Further think about it, in fact, what we just did it on every page so few. (Enter the text, click to obtain the text of an element, to see whether an element is displayed, switch frame, window switching, processing popups, etc.) based on common operating on these pages, we can design a page base class, use the base for each page class to instantiate specific pages. Then the base page class method, we can call in the instance of a specific page.

  Because now we just write a class to read XML. Therefore, we will first write a page base class to read XML support.

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;
        //Page element profile path     
        protected the HashMap <String, the Position> positionMap;
         // store information page elements     
        protected logUtil log = new new logUtil (BasePageX. Class ); 
        the Position position = null ;
         public BasePageX (the WebDriver Driver, the pageName String, String XMLName)   throws {Exception        
             Super (Driver);
             the this .driver = Driver;        
             the this .pageName = the pageName;         // Get page.xml path, in the same directory page.xml 
            XMLPath = the 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 (the getPosition (positionName));     
            log.info ( "switch to the frame:" + positionName); 
            Reporter.log ( "switch to the frame:" + positionName); 
            }      
        @Override 
        public String getAlertText () {
             return  Super . getAlertText (); 
        } 
     // somewhere using Robot click-coordinates, the positioning elements can not be used, for example (elements of type Object)
public void mouseMoveClick ( int X, int Y) throws of AWTException { Robot Rb1 = new 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 ( "not found" + positionName + "page elements" ); } return position; } }

     In this way, after the completion of the base page class. We can use the following code to define an instance of a page, and then use the method to call the base page instance class, to achieve the purpose of the operation of the page. E.g:

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

 

Guess you like

Origin www.cnblogs.com/generalli2019/p/11424352.html