問題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}\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**********************/
?
?
[Jmeter]通過批處理調用java,java從CSV動態讀取登錄的用戶名和密碼,并將其作為參數組合成字符串,寫入外部.bat文件,然后通過Java執行這個外部批處理文件
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

