SingleAtomicCase运行
来源:互联网 发布:美的面试 知乎 编辑:程序博客网 时间:2024/05/21 11:02
目录
- 目录
- 业务分析
- 代码块
- Controller层
- BO层
- DAO层
- 知识梳理
- java调用bat脚本
- 批处理部分
- xml文件解析部分
- 判断进程是否结束
- java调用在浏览器中打开本地HTML文件
- 文件遍历及删除知识
本篇文章包含如下知识点:
SingleAtomicCase的三层代码块
其中所涉及到的一些知识点的解读
业务分析
目前项目框架中提供了run all test cases方法,但users在用户界面上无法直接运行单个atomicCase,提供一个“运行”方法,让使用者可以直接在界面上跑单个case并生成相应的报告。
代码块
实际工作代码牵涉庞大,此处只给出修改部分的代码并给出解读。
Controller层
//runSingleAtomicaCase @RequestMapping(value = "/update", method = RequestMethod.POST) @ResponseBody public void runSingleTest( @RequestParam(value="node", required = true) String node) throws Exception{ AtomicCaseBo atomicCaseBo = new AtomicCaseBo(); atomicCaseBo.runSingleAtomicCase(node); }
BO层
public void runSingleAtomicCase(String node) throws Exception{ try { Process p1 = Runtime.getRuntime().exec("cmd /c start "+Constant.ATOMIC_PROJECT_PATH+"APIGen.bat"); BufferedReader in = new BufferedReader(new InputStreamReader(p1.getInputStream())); String s = "";// if ((s = in.readLine()) == null) {// //看看有没有想要的进程// Process p2 = Runtime.getRuntime().exec("cmd /c start D:/xiangmu/sample/APITest/APIRun.bat");// in = new BufferedReader(new InputStreamReader(p2.getInputStream()));// s = ""; if ((s = in.readLine()) == null) { AtomicCaseBo atomicCaseBo = new AtomicCaseBo(); boolean b1 = false; b1 = atomicCaseBo.modifyFromTestngXML(node); if(b1){ Process p3 = Runtime.getRuntime().exec("cmd /c start "+Constant.ATOMIC_PROJECT_PATH+"APIRun2.bat"); in = new BufferedReader(new InputStreamReader(p3.getInputStream())); s = ""; if ((s = in.readLine()) == null){ AtomicCaseController atomicCaseController = new AtomicCaseController(); atomicCaseController.openHtmlInChrome(new URL("file:///"+Constant.ATOMIC_PROJECT_PATH+"Report/index.html")); } } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }// try {// Runtime.getRuntime().exec("cmd /c start D:/xiangmu/sample/APITest/APIRun.bat");// } catch (IOException e) {// e.printStackTrace();// } } public static boolean modifyFromTestngXML(String node) throws Exception{ String testngPath = Constant.ATOMIC_PROJECT_PATH+"TestProject/src/test/resources/testng/";//将目前TestProject生成的文件夹写死 File file = new File(testngPath); System.out.println(file); System.out.println("跑杂项文件删除程序"); for(File f:file.listFiles()){ if(!(f.getName().contains("testng.xml")||f.getName().contains("testng-all.xml"))){// 删除除testng.xml和testng-all.xml之外的文件 System.out.println("删除"+f+"文件"); f.delete(); }else{ if(f.getName().contains("testngPath.xml")){ AtomicCaseDAO atomicCaseDAO = new AtomicCaseDAO(); AtomicCaseBo atomicCaseBo = new AtomicCaseBo(); atomicCaseBo.deleteFromXml(f,atomicCaseDAO.selectFromDB(node)); } } } System.out.println("结束杂项文件删除程序"); return true; }// public static void main(String[] args){// AtomicCaseBo atomicCaseBo = new AtomicCaseBo();// try {// atomicCaseBo.Parse("D:/xiangmu/autoTest/Doraemon/APITest/TestProject/src/test/resources/testng/testng.xml");// } catch (Exception e) {// System.out.println("yichangpaochu");// // TODO Auto-generated catch block// e.printStackTrace();// }// } public void deleteFromXml(File f, String node) throws Exception {// 删除testng.xml文件中不符合要求的部分标签 AtomicCaseBo p = new AtomicCaseBo(); Document d = p.Parse(f); System.out.println("跑删除XML中不符合要求标签程序"); NodeList nlist = d.getElementsByTagName("test"); System.out.println(nlist.getLength()); System.out.println(nlist+"1"); for(int i=0;i<nlist.getLength();){ Element l = (Element) nlist.item(i); System.out.println("删除 the nodes"); if(!l.getAttribute("name").equals(node)){ System.out.println(nlist+"大白痴"); System.out.println("delete the useless node"+l); l.getParentNode().removeChild(l); }else{ i++; } } System.out.println("结束删除XML中不符合要求标签程序"); System.out.println(d.getElementsByTagName("test").getLength()); p.updateXml(d, f.getPath()); //Transformer t = TransformerFactory.newInstance().newTransformer(); //t.transform(new DOMSource(d), new StreamResult("src/resource/test.xml")); }// public void remainOneNode(Document doc,String str,String path){// // NodeList nlist = doc.getElementsByTagName("test"); //得到总的用例// System.out.println("原子case共有:" + nlist.getLength() + "个");// for(int i=0;i<nlist.getLength();){// Element testElement = (Element) nlist.item(i);// String tmp = testElement.getAttribute("name");// if(!tmp.equals(str)){// testElement.getParentNode().removeChild(testElement);// System.out.println("已删除原子case:"+ tmp); //删除时不移动指针// }else// {// System.out.println("保留原子case: "+ tmp);// ++i; //指针移到// }// }// //更新到xml// updateXml(doc,path);// // } public void updateXml(Document doc,String path){ System.out.println("跑更新程序"); Transformer trans = null; try { trans = TransformerFactory.newInstance().newTransformer(); //创建trans } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { e.printStackTrace(); } try { trans.transform(new DOMSource(doc), new StreamResult(path)); //dom到xml } catch (TransformerException e) { e.printStackTrace(); } System.out.println("跑结束更新程序"); } public Document Parse(File f) throws Exception{// InputStream input = getClass().getResourceAsStream(f.getParent()); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(f); return document; } public void openHtmlInChrome(URL url){ try { browse(url); } catch (Exception e) { } } private static void browse(URL url) throws Exception { //获取操作系统的名字 String osName = System.getProperty("os.name", ""); if (osName.startsWith("Windows")) { //windows的打开方式。 Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url); } else if (osName.startsWith("Mac OS")) { //Mac的打开方式 Class fileMgr = Class.forName("com.apple.eio.FileManager"); Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] { String.class }); openURL.invoke(null, new Object[] { url }); } else { // Unix or Linux的打开方式 String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" }; String browser = null; for (int count = 0; count < browsers.length && browser == null; count++) //执行代码,在brower有值后跳出, //这里是如果进程创建成功了,==0是表示正常结束。 if (Runtime.getRuntime().exec(new String[] { "which", browsers[count] }).waitFor() == 0) browser = browsers[count]; if (browser == null) throw new Exception("Could not find web browser"); // else // //这个值在上面已经成功的得到了一个进程。 // String s = "" ;// url.getPath();// Runtime.getRuntime().exec(new String[] { browser, url }); } } public void openHtml(URL url){// URL url=new URL("file:///D:/xiangmu/sample/APITest/Report/index.html"); BufferedReader in2=null; try{ in2 = new BufferedReader(new java.io.InputStreamReader(url.openStream())); for(String line;(line=in2.readLine())!=null;){ System.out.println(line); } }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(in2!=null){ try{ in2.close(); }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
DAO层
public String selectFromDB(String caseID) { ResultSet result=daocon.executeQuery(String.format("select name from AtomicCase where caseID='%s'", caseID)); String name = null; try { while(result.next()) { name= result.getString(1); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return name; }
层级逻辑:controller接收到前台传来的node(string caseID)作为atomic case的唯一标识符,调用BO的runSingleAtomicCase方法并将node参数传入。
runSingleAtomicCase()先跑APIGen,进程结束后调modifyFromTestngXML()方法
modifyFromTestngXML()先将生成的xml文件非testng.xml testng-all.xml文件删除,然后从DAO层调selectName方法,取出node对应的name,并将testng.xml中不需要跑的testname删除。并更新testng.xml文件。完成后跑APIRUN脚本。并生成测试报告以网页的形式打开。
知识梳理
各块知识总览
java调用bat脚本
Runtime.getRuntime().exec("cmd /c start "+Constant.ATOMIC_PROJECT_PATH+"APIGen.bat");
批处理部分
cd指令,到指定文件目录@echo off md "E:\My documents\Newfolder1" 到指定文件目录下新建文件夹mkdir Reportcopy TestProject\target\surefire-reports\html\* Report 例1:打开指定目录,如C:\Test start C:\Test例2:执行C盘下的WinWord.exe程序 start C:\WinWord.exe例3:用某个程序打开某个文件,如用TheWorld.exe打开一个htm文件 start d:\TheWorld\TheWorld.EXE C:\我有一个恋爱.htmpause停顿exit离开
批处理http://blog.csdn.net/linda1000/article/details/10221285
xml文件解析部分
public Document Parse(File f) throws Exception{// InputStream input = getClass().getResourceAsStream(f.getParent()); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(f); return document; }
判断进程是否结束
<Runtime runtime = Runtime.getRuntime(); Process process = null; try { process = runtime.exec("cmd /c Tasklist"); BufferedReader in = new BufferedReader(new InputStreamReader( process.getInputStream())); String s = ""; if((s = in.readLine()) == null) { //进程结束,进行其他处理 } }
java调用在浏览器中打开本地HTML文件
if (osName.startsWith("Windows")) { //windows的打开方式。 Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url); }
文件遍历及删除知识
//文件遍历for(File f: file.list()){ if(!(f.getName().contains("testng.xml")||f.getName().contains("testng-all.xml")){ f.delete(); }else{ if(f.getName().contains("testng.xml")){ AtomicCaseBO atomicCaseBo = new AtomicCaseBo(); atomicCaseBo.deleteFromXml(node); } }}
0 0
- SingleAtomicCase运行
- 运行
- 运行jar
- 运行OpenLdap
- 开始--->运行
- 运行命令
- 运行命令
- 运行命令
- 运行ruby
- 运行命令
- 运行命令
- 运行命令
- 运行库
- 运行代码
- 运行ns2
- 运行不成功
- service 运行
- 运行信息
- maven搭建
- jquery 全选设置
- 关于用深度学习做answer selection的论文
- APACHE开启GZIP提升网站JS加载速度
- 数组去掉重复值、删除元素、添加元素
- SingleAtomicCase运行
- leetcode:单链表之Reverse Linked List II
- js中 日期天数加1
- spring注解的详细说明
- POJ 3159 Candies
- openstack 工作流workflows使用原理讲解
- Category 命名冲突检测
- 网络编程学习笔记一:Socket编程
- hdu 5791 two 多校联赛第五场