[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}\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执行这个外部批处理文件的更多相关文章
- java的io操作(将字符串写入到txt文件中)
import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java ...
- window 开机重启运行bat文件(运行java的jar包)
1:直接打开电脑的目录:(记得勾选查看隐藏项目) C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp,在里面新建start.bat ...
- 使用java对执行命令行 或 执行bat文件
public class Hellotianhao { public static void main(String[] args) throws Exception{ System.out.prin ...
- java web实现在cookie中保存用户名和密码,用户自动登入
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- 【jmeter】使用csv文件生成用户名和密码列表
介绍 在[jmeter]使用jmeter进行测试-示例 中介绍了jmeter的基本使用,本文将介绍如何使用csv文件生成多个用户名. 应用场景 实际测试中,经常需要模拟多个用户进行负载测试,而用户名和 ...
- 【Jmeter】如何通过文件导入方式对用户名和密码进行参数化设置
JMeter 参数化 注意:param和data body只能用一个.所有任何一个里面有内容,切换都会报错,这不是问题,jmeter是这么设计的 方法一:通过添加CSV Data Set Config ...
- Python数据分析之Pandas读写外部数据文件
1 引言 数据分析.数据挖掘.可视化是Python的众多强项之一,但无论是这几项中的哪一项都必须以数据作为基础,数据通常都存储在外部文件中,例如txt.csv.excel.数据库.本篇中,我们来捋一捋 ...
- AJAX载入外部JS文件到页面并让其执行的方法(附源码)
一. 向HTML页面中动态添加JS文件(从外部载入)并让其执行的两种方法 1.只适用于IE浏览器的简单方法: 先在文档中放置一张JS"空床"并添加ID:<script id= ...
- Java执行批处理.bat文件(有问题???求高手帮忙解答!!!)
Java执行批处理.bat文件(有问题???求高手帮忙解答!!!) 在项目开发中常常都会遇到需要在代码中调用批处理bat脚本,把自己在项目中遇到过的总结下 ...
随机推荐
- filter实例
最近在学习filter,看完理论知识之后,就特地在实际工作中的系统中去验证. SSP系统实例如下: 1.在web.xml中寻找 filter的配置信息 如下: <filter> < ...
- hadoop分布式快速搭建
hadoop分布式快速搭建 1.配置主节点与从节点的ssh互信:[其中在主从节点主机的/etc/hosts文件中需绑定主机名ip间的映射关系; 如,192.168.1.113 node0 192.16 ...
- Xshell图形界面启动
https://blog.csdn.net/qq_27843481/article/details/50539797 增加内存:https://jingyan.baidu.com/article/4d ...
- Virus
c:\windows\syswow64\rundll32.exe C:\Users\Administrator.mt1-PC\AppData\Roaming\FishSearch
- IE浏览器中的加载项怎么删除
IE浏览器中的加载项是一些软件或者浏览器的功能控件,我们可以通过禁用.开启来控制是否使用某些加载项,同时可以将一些加载项删除. 比如当我们遇到了一些不好的加载项,想要将它删除,通过这篇经验,教大家怎么 ...
- as3 连接mysql
http://www.cnblogs.com/yili16438/archive/2011/04/23/2025936.html
- Git reset head revert 回滚
Overview 涉及Git一些日常操作 :) 基础知识 <Pro Git>至少了解branch,commit的概念,及基本的原理 Git常用魔法 存档:master代码回滚方法 我是QA ...
- js中常见的内置对象
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- drupal 用法小结,drupal select ,query ,distinct
https://api.drupal.org/api/drupal/includes%21actions.inc/function/actions_do/7.x addFileds : 这个更全点: ...
- Musle比对软件
下载地址:http://www.drive5.com/muscle/downloads.htm 1)运行: win+R然后输入cmd,然后cd进入muscle目录 2) 比对: muscle3.8.3 ...