上篇我们写了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("登录");