五、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); } }
未完待續!!!馬上奉上!!!