概述:实现了dom4j在指定位置进行进行追加元素,并生成新的jmx文件。
代码如下:
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class TestM2 {
static String hashTree_xpath="/jmeterTestPlan/hashTree/hashTree/hashTree";
public static void main(String [] args) throws DocumentException {
SAXReader reader = new SAXReader();
String httpmoduleJmxPath=System.getProperty("user.dir")+"/resources/httpmodule.jmx";
String httpmoduleJmxPath2=System.getProperty("user.dir")+"/resources/httpmodule2.jmx";
Document document = reader.read(new File(httpmoduleJmxPath));
//通过模板,将数据放入document对象中
creatJmx( document,"http://www.baidu.com/user/login.html","www.baidu.com","80","https","/user/login.html","GET") ;
outJmx( document,httpmoduleJmxPath2) ;//输出追加后的新jmx文件
}
//通过xpath定位节点,并修改dom树
public static void chengNodeValue(Element root,String xpath,String newValue) {
Node node= root.selectSingleNode(xpath);
node.setText(newValue);
}
//document当前文档对象,jmxPath 为jmx文件路径
public static void outJmx(Document document,String jmxPath ) {
XMLWriter output;
try {
output = new XMLWriter(new FileWriter(jmxPath));
output.write(document);
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @author liujian
* 参数说明
* @param httpmoduleJmxPath jmx 模板位置
* @param siteAllPath 站点全路径用来设置HTTPSamplerProxy的名字,这样不会重名
* @param domain 域名
* @param port 端口
* @param protocol 协议头 http或者https
* @param path 路径,就是端口后面的路径
* @param method 请求方法get、 post
* **/
public static void creatJmx(Document document,String siteAllPath,String domain,String port,String protocol,String path,String method) throws DocumentException {
Element root =document.getRootElement();
Element node= (Element) root.selectSingleNode(hashTree_xpath);
/*说明
* 获取元素列表,这样可以指定位置放入创建的元素,如果直接用node.addElement()会直接追加到末尾,
* 但是jemeter它不识别,会报打不开的错误。
*/
List eleList= node.elements();
Element hashTree = DocumentHelper.createElement("hashTree");//创建一个元素
eleList.add(1, hashTree);//如果原来1的位置有其他元素,放心不会覆盖掉原来的元素,原来1位置上的元素以及它后面对元素位置会自动往后移一位
Element HTTPSamplerProxy= DocumentHelper.createElement("HTTPSamplerProxy")
.addAttribute("guiclass", "HttpTestSampleGui")
.addAttribute("testclass", "HTTPSamplerProxy")
.addAttribute("testname", siteAllPath)
.addAttribute("enabled", "true");
Element elementProp= HTTPSamplerProxy.addElement("elementProp")
.addAttribute("name", "HTTPsampler.Arguments")
.addAttribute("elementType", "Arguments")
.addAttribute("guiclass", "HTTPArgumentsPanel")
.addAttribute("testclass", "Arguments")
.addAttribute("testname", "用户定义的变量")
.addAttribute("enabled", "true");
Element collectionProp= elementProp.addElement("collectionProp").addAttribute("name", "Arguments.arguments");
HTTPSamplerProxy.addElement("stringProp").addAttribute("name", "HTTPSampler.domain").setText(domain);
HTTPSamplerProxy.addElement("stringProp").addAttribute("name", "HTTPSampler.port").setText(port);
HTTPSamplerProxy.addElement("stringProp").addAttribute("name", "HTTPSampler.protocol").setText(protocol);
HTTPSamplerProxy.addElement("stringProp").addAttribute("name", "HTTPSampler.contentEncoding").setText("utf-8");
HTTPSamplerProxy.addElement("stringProp").addAttribute("name", "HTTPSampler.path").setText(path);
HTTPSamplerProxy.addElement("stringProp").addAttribute("name", "HTTPSampler.method").setText(method);
HTTPSamplerProxy.addElement("boolProp").addAttribute("name", "HTTPSampler.follow_redirects").setText("true");
HTTPSamplerProxy.addElement("boolProp").addAttribute("name", "HTTPSampler.auto_redirects").setText("false");
HTTPSamplerProxy.addElement("boolProp").addAttribute("name", "HTTPSampler.use_keepalive").setText("true");
HTTPSamplerProxy.addElement("boolProp").addAttribute("name", "HTTPSampler.DO_MULTIPART_POST").setText("false");
HTTPSamplerProxy.addElement("stringProp").addAttribute("name", "HTTPSampler.embedded_url_re").setText("false");;
HTTPSamplerProxy.addElement("stringProp").addAttribute("name", "HTTPSampler.connect_timeout");
HTTPSamplerProxy.addElement("stringProp").addAttribute("name", "HTTPSampler.response_timeout");
eleList.add(2, HTTPSamplerProxy);//如果原来2的位置有其他元素,放心不会覆盖掉原来的元素,原来的2位置上的元素以及它后面对元素位置会自动往后移一位
}
}