[Jmeter]通过批处理调用java,java从CSV动态读取登录的用户名和密码,并将其作为参数组合成字符串,写入外部.bat文件,然后通过Java执行这个外部批处理文件

[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*****************/

@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************/

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}ScriptSmokeTest" 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}ScriptSmokeTest" 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. &lt;br /&gt;
            Please check the automation test report by the link below.  &lt;br /&gt;
            If there are any questions, please contact with Jenny Zhang. Thank you! &lt;br /&gt;&lt;br /&gt;
            http://${ComputerName}/${ReportName}${time}.html
            </message> -->
        </mail>
    </target>
</project>

/************CIM_US_TP_SmokeTest.xml**********************/