Selenium测试框架使用xml作为对象库

发表于:2015-9-11 11:01

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:to be crazy    来源:51Testing软件测试网采编

分享:
  每一个Locator对象包含3属性 ByType 、timeOut时间和相应的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起来,也算是一种关键字驱动.
  传入的两个参数分别是library的路径和对应的page
32/3<123>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号