[Jmeter]通过批处理调用java,java从CSV动态读取登录的用户名和密码,并将其作为参数组合成字符串,写入外部.bat文件,然后通过Java执行这个外部批处理文件
问题1:怎样通过批处理调用java代码?
问题2:怎样通过java从CSV文件获取到用户名和密码存入变量?
问题3:怎样将获取到的用户名和密码组合成字符串,写入外部批处理文件?
问题4:怎样在批处理文件调用ANT的时候,将用户名和密码作为参数传进去?
问题5:怎样通过java调用.bat文件?
问题6:怎样保证java在调用.bat的时候不出现闪退?
问题7:怎样让java在执行.bat的时候,.bat的控制台输出日志?
问题8:怎样让java执行.bat完成之后不会有残余的cmd.exe进程?
/*************CIM_US_TP_SmokeTest.bat*****************/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | @echo off color 0a echo autotest beginning,Please Wait... ... set AutoPath=%~dp0 %AutoPath:~ 0 , 2 % pushd %AutoPath% cd /d %AutoPath% set JmeterPath=..\..\..\ echo AutoPath=%AutoPath% echo JmeterPath=%JmeterPath% forfiles /p %AutoPath%Result /m *.jtl -d - 7 /c "cmd /c del /f @path" >nul 2 >nul forfiles /p %JmeterPath%extras /m *.html -d - 7 /c "cmd /c del /f @path" >nul 2 >nul javac getUserAccount_US_TP_SmokeTest.java java getUserAccount_US_TP_SmokeTest exit |
/*************CIM_US_TP_SmokeTest.bat*****************/
/************getUserAccount_US_TP_SmokeTest.java************/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | import java.io.*; public class getUserAccount_US_TP_SmokeTest { public static String AutomationPath = System.getProperty( "user.dir" ); public static void main( String[] args ) throws InterruptedException { System.out.println( "AutomationPath:" + AutomationPath ); getUserAccount_US_TP_SmokeTest generator = new getUserAccount_US_TP_SmokeTest(); String userName = generator.getUserName(); // System.out.println( "userName:" + userName ); String password = generator.getPassWord(); // System.out.println( "password:" + password ); generator.runAnt( userName, password ); } private void runAnt( String userName, String password ) throws InterruptedException { Process p; String cmd = "ant -f " +AutomationPath+ "\\CIM_US_TP_SmokeTest.xml" + " -DuserName=" + userName + " -Dpassword=" + password; String path=AutomationPath+ "\\cmd_US_TP_Smoke.bat" ; fileWrite(cmd,path); try { System.out.println( "Execute in command line:: " + path ); p =Runtime.getRuntime().exec(path); //取得命令结果的输出流 InputStream fis=p.getInputStream(); //用一个读输出流类去读 InputStreamReader isr= new InputStreamReader(fis); //用缓冲器读行 BufferedReader br= new BufferedReader(isr); String line= null ; //直到读完为止 while ((line=br.readLine())!= null ) { System.out.println(line); } Thread.sleep( 10000 ); System.out.println( "Execute cmd over. " ); } catch ( IOException e ) { e.printStackTrace(); } } public void fileWrite(String cmd,String path){ File file = new File(path); try { if (!file.exists()){ file.createNewFile(); } FileWriter fw= new FileWriter(file, false ); PrintWriter pw= new PrintWriter(fw); pw.println(cmd); pw.close(); fw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public String getUserName() { String userName= "" ; try { BufferedReader reader = new BufferedReader( new FileReader(AutomationPath+ "\\Data\\AccountInfo\\AutoTestAccount_Office_US_TP.csv" )); //换成你的文件名 reader.readLine(); //第一行,为标题信息 String line=reader.readLine(); //读取第二行 reader.close(); String item[] = line.split( "," ); //CSV格式文件为逗号分隔符文件,这里根据逗号切分 userName = item[ 0 ]; //这就是你要的数据了 //System.out.println("Get Login Email: "+userName); } catch (Exception e) { e.printStackTrace(); } return userName; } public String getPassWord() { String password= "" ; try { BufferedReader reader = new BufferedReader( new FileReader(AutomationPath+ "\\Data\\AccountInfo\\AutoTestAccount_Office_US_TP.csv" )); //换成你的文件名 reader.readLine(); //第一行,为标题信息 String line=reader.readLine(); //读取第二行 reader.close(); String item[] = line.split( "," ); //CSV格式文件为逗号分隔符文件,这里根据逗号切分 password= item[ 1 ]; //这就是你要的数据了 //System.out.println("Get Login Password: "+password); } catch (Exception e) { e.printStackTrace(); } return password; } } |
/************getUserAccount_US_TP_SmokeTest.java************/
/************CIM_US_TP_SmokeTest.xml**********************/
<?xml version="1.0"?>
<project name="morningstar" default="all" basedir=".\">
<property name="JMeter.home" value=".\..\..\..\"/>
<property name="mail_to"
value="lemon.li@morningstar.com,na.gong@morningstar.com,sandy.zhou@morningstar.com,jenny.zhang@morningstar.com"/>
<property name="to_me_only" value="jenny.zhang@morningstar.com"/>
<property name="ReportName" value="CIMUSSmokeTestReport"/>
<property name="ComputerName" value="SZOTWIN2K801"/>
<property name="LoginEmail" value="${userName}"/>
<echo>${userName}</echo>
<property name="LoginPassword" value="${password}"/>
<echo>${password}</echo>
<tstamp>
<format property="time" pattern="yyyyMMddhhmm"/>
</tstamp>
<target name="all" >
<antcall target="runCWP" />
<antcall target="runUDF" />
<antcall target="transferAll"/>
<antcall target="transferFailure"/>
<antcall target="sendEmail"/>
</target>
<target name="runCWP" depends="">
<taskdef name="jmeter" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask"/>
<jmeter jmeterhome="${JMeter.home}" resultlog="${basedir}/Result/${ReportName}${time}.jtl">
<testplans dir="${basedir}\Script\SmokeTest" includes="CIM_US_TP_CWP_SmokeTest.jmx"/>
</jmeter>
</target>
<target name="runUDF" depends="">
<taskdef name="jmeter" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask"/>
<jmeter jmeterhome="${JMeter.home}" resultlog="${basedir}/Result/${ReportName}${time}.jtl">
<testplans dir="${basedir}\Script\SmokeTest" includes="CIM_US_TP_UDF_SmokeTest.jmx"/>
</jmeter>
</target>
<target name = "transferAll" depends = "">
<xslt in="${basedir}/Result/${ReportName}${time}.jtl"
out="${JMeter.home}/extras/${ReportName}${time}.html"
style="${JMeter.home}/extras/JMeter-results-detail-report_21.xsl"/>
</target>
<target name = "transferFailure" depends = "">
<xslt in="${basedir}/Result/${ReportName}${time}.jtl"
out="${JMeter.home}/extras/${ReportName}${time}_failure.html"
style="${JMeter.home}/extras/JMeter-results-detail-report_21_failure.xsl"/>
</target>
<target name="sendEmail">
<mail mailhost="internalmail.morningstar.com" mailport="25"
subject="CIM US Automation Test Report(Smoke Test)!"
messagefile="${JMeter.home}/extras/${ReportName}${time}_failure.html"
messagemimetype="text/html" tolist="${mail_to}">
<from address="jenny.zhang@morningstar.com"/>
<!-- <fileset dir="${JMeter.home}/extras/">
<include name="${ReportName}${time}.html"/>
<include name="expand.png"/>
</fileset> -->
<!-- <message>This email was sent automatically by ANT. <br />
Please check the automation test report by the link below. <br />
If there are any questions, please contact with Jenny Zhang. Thank you! <br /><br />
http://${ComputerName}/${ReportName}${time}.html
</message> -->
</mail>
</target>
</project>
/************CIM_US_TP_SmokeTest.xml**********************/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现