您的位置:軟件測試 > 開源軟件測試 > 開源功能測試工具 > Selenium
Selenium測試框架使用xml作為對象庫
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2015/9/14 10:53:03 ] 推薦標簽:軟件測試工具,功能測試工具

  每一個Locator對象包含3屬性 ByType 、timeOut時間和相應(yīng)的xpath、id......的value
  接下來需要寫一個wrapper
package com.dbyl.libarary.utils;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import com.dbyl.libarary.utils.Locator.ByType;
public class xmlUtils {
/**
* @author Young
* @param path
* @param pageName
* @return
* @throws Exception
*/
public static HashMap<String, Locator> readXMLDocument(String path,
String pageName) throws Exception {
System.out.print(pageName);
HashMap<String, Locator> locatorMap = new HashMap<String, Locator>();
locatorMap.clear();
File file = new File(path);
if (!file.exists()) {
throw new IOException("Can't find " + path);
}
SAXReader reader = new SAXReader();
Document document = reader.read(file);
Element root = document.getRootElement();
for (Iterator<?> i = root.elementIterator(); i.hasNext();) {
Element page = (Element) i.next();
if (page.attribute(0).getValue().equalsIgnoreCase(pageName)) {
System.out.println("page Info is:" + pageName);
for (Iterator<?> l = page.elementIterator(); l.hasNext();) {
String type = null;
String timeOut = "3";
String value = null;
String locatorName = null;
Element locator = (Element) l.next();
for (Iterator<?> j = locator.attributeIterator(); j
.hasNext();) {
Attribute attribute = (Attribute) j.next();
if (attribute.getName().equals("type")) {
type = attribute.getValue();
System.out.println(">>>>type " + type);
} else if (attribute.getName().equals("timeOut")) {
timeOut = attribute.getValue();
System.out.println(">>>>timeOut " + timeOut);
} else {
value = attribute.getValue();
System.out.println(">>>>value " + value);
}
}
Locator temp = new Locator(value,
Integer.parseInt(timeOut), getByType(type));
locatorName = locator.getText();
System.out.println("locator Name is " + locatorName);
locatorMap.put(locatorName, temp);
}
continue;
}
}
return locatorMap;
}
/**
* @param type
*/
public static ByType getByType(String type) {
ByType byType = ByType.xpath;
if (type == null || type.equalsIgnoreCase("xpath")) {
byType = ByType.xpath;
} else if (type.equalsIgnoreCase("id")) {
byType = ByType.id;
} else if (type.equalsIgnoreCase("linkText")) {
byType = ByType.linkText;
} else if (type.equalsIgnoreCase("name")) {
byType = ByType.name;
} else if (type.equalsIgnoreCase("className")) {
byType = ByType.className;
} else if (type.equalsIgnoreCase("cssSelector")) {
byType = ByType.cssSelector;
} else if (type.equalsIgnoreCase("partialLinkText")) {
byType = ByType.partialLinkText;
} else if (type.equalsIgnoreCase("tagName")) {
byType = ByType.tagName;
}
return byType;
}
/**
* @author Young
* @throws IOException
*/
public static void writeXMLDocument() throws IOException {
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(new FileWriter("output.xml"), format);
Document document = DocumentHelper.createDocument();
Element root = document.addElement("map");
root.addComment("locator of page map info");
Element page = root.addElement("page").addAttribute("pageName",
"com.dbyl.libarary.pageAction.HomePage");
page.addComment("Locator lists");
page.addElement("locator").addAttribute("type", "ByName")
.addAttribute("timeOut", "3")
.addAttribute("value", "\\div[@name]").addText("loginButton");
page.addElement("locator").addAttribute("type", "ById")
.addAttribute("timeOut", "3")
.addAttribute("value", "\\div[@id]").addText("InputButton");
writer.write(document);
writer.close();
}
}
  定義一個readXMLDocument 方法,返回一個hashMap用來和頁面元素名字和Locator對象match起來,也算是一種關(guān)鍵字驅(qū)動.
  傳入的兩個參數(shù)分別是library的路徑和對應(yīng)的page
上一頁123下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd