selenium之多线程启动grid分布式测试框架封装(二)

时间:2021-08-23 11:26:47

五、domain类创建

  在domain包中创建类:RemoteLanchInfo.java 用来保存启动信息。

package com.lingfeng.domain;

public class RemoteLanchInfo {
private String id;
private String name;
private String address; public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
} }

  这个javabean就是用来保存一条配置文件中的启动信息,后面会将这个javabean封装到集合中,以后需要到这些信息的时候调用起来也方便,比如碰到了IE6哪个页面不兼容,就可以使用名称来进行截图文件的命名,这样就知道了是哪个浏览器出了问题。

六、工具类,远程服务配置文件初始化类

   在utils包中创建java类RemoteServerInit.java

package com.lingfeng.utils;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map; import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver; import com.lingfeng.domain.RemoteLanchInfo; /**
* 远程服务器初始化类
* @author 凌风
*
*/
public class RemoteServerInit {
//存储读取配置文档
private static Document DOCUMENT;
//存储所有状态为on的远程服务端信息
private static Map<String,RemoteLanchInfo> allLaunchInfoMap = new HashMap<String,RemoteLanchInfo>();
//存储所有远程服务端浏览器启动的driver
private static Map<String,WebDriver> allLaunchDriverMap = new HashMap<String, WebDriver>(); //在调用launchBrowser方法后,可用此方法获取到所有远程服务端的driver
public static Map<String,WebDriver> getAllLaunchDriverMap(){
return allLaunchDriverMap;
}
//在调用launchBrowser方法后,可用此方法获取到所有远程服务端的启动信息。
public static Map<String,RemoteLanchInfo> getAllLaunchInfoMap(){
return allLaunchInfoMap;
} public static void launchBrowser() throws DocumentException, MalformedURLException{ File file = new File("src/RemoteServerLaunchInfo.xml");
DOCUMENT = readInitFile(file);
List<Element> servers = getAllRemoteServers(DOCUMENT);
Iterator<Element> it = servers.iterator();
while(it.hasNext()){
addWebDriver(allLaunchDriverMap,it);
} } /**
* 将启动信息以及相对应的driver存储到各自的map集合中
*
* @param allLaunchDriverMap
* @param it
* @throws MalformedURLException
*/
private static void addWebDriver(
Map<String, WebDriver> allLaunchDriverMap, Iterator<Element> it) throws MalformedURLException {
Element e = it.next(); String remoteAddress = e.getText();
String remoteID = e.attributeValue("ID");
String remoteName = e.attributeValue("name"); //将启动的所有信息存储到javabean当中。
RemoteLanchInfo info = setRemoteLanchInfo(remoteAddress,remoteID,remoteName);
allLaunchInfoMap.put(remoteName, info);//启动的信息放到map集合中 DesiredCapabilities desiredCapabilities = DesiredCapabilities.internetExplorer();
WebDriver driver = new RemoteWebDriver(new URL(remoteAddress), desiredCapabilities);
allLaunchDriverMap.put(remoteName, driver);
} /**
* 设置远程启动信息,作为一个作为一个对象保存到map集合中
* @param remoteAddress
* @param remoteID
* @param remoteName
* @return RemoteLanchInfo 远程启动信息
*/
private static RemoteLanchInfo setRemoteLanchInfo(String remoteAddress,
String remoteID, String remoteName) { RemoteLanchInfo info = new RemoteLanchInfo();
info.setAddress(remoteAddress);
info.setId(remoteID);
info.setName(remoteName);
return info;
} public static String getHomepage(){
return DOCUMENT.getRootElement().elementText("homepage");
}
/**
* 将所有配置文件中的remote-server标签对象存储到list集合中
* @param document
* @return List<Element> 存储一个remote-server标签对象的一个集合。
*/
private static List<Element> getAllRemoteServers(Document document) {
List<Element> servers = new ArrayList<Element>();
List<Element> list = document.getRootElement().elements("remote-server");
Iterator<Element> it = list.iterator(); while(it.hasNext()){
Element element = (Element) it.next();
if(element.attributeValue("status").equals("on")){
servers.add(element);
}
}
return servers;
}
/**
* 读取配置文件,返回文件
* @param file
* @return Document
* @throws DocumentException
*/
public static Document readInitFile(File file) throws DocumentException {
SAXReader reader = new SAXReader();
return reader.read(file);
}
}

未完待续!!!马上奉上!!!