Commit 517d2040 by Li Yongyu

update

parent 67b2c280
<project name="auto_test" default="testoutput" basedir="."> <project name="auto_test" default="testoutput" basedir=".">
<!-- Define <testng> task --> <!-- Define <testng> task -->
<taskdef name="testng" classname="org.testng.TestNGAntTask" > <taskdef name="testng" classname="org.testng.TestNGAntTask" >
<classpath> <classpath>
<pathelement location="lib/testng-6.8.6.jar" /> <pathelement location="lib/testng-6.8.6.jar" />
</classpath> </classpath>
</taskdef> </taskdef>
<property name="testoutputdir" location="test-output" /> <property name="testoutputdir" location="test-output" />
<property name="srcdir" location="src" /> <property name="srcdir" location="src" />
<property name="libdir" location="lib" /> <property name="libdir" location="lib" />
<property name="full-compile" value="true" /> <property name="full-compile" value="true" />
<property name="basedir" value="D:/workspace/auto_test/" /> <property name="basedir" value="E:\git\study\studycenter_Api_auto_test" />
<path id="classpath.test"> <path id="classpath.test">
<fileset dir="${libdir}"> <fileset dir="${libdir}">
<include name="**/*.jar" /> <include name="**/*.jar" />
</fileset> </fileset>
<pathelement location="${testoutputdir}" /> <pathelement location="${testoutputdir}" />
<pathelement location="${srcdir}" /> <pathelement location="${srcdir}" />
</path> </path>
<target name="clean"> <target name="clean">
<delete dir="${basedir}/bin" /> <delete dir="${basedir}/bin" />
</target> </target>
<target name="compile" depends="clean"> <target name="compile" depends="clean">
<mkdir dir="${basedir}/bin" /> <mkdir dir="${basedir}/bin" />
<javac srcdir="${srcdir}" encoding="UTF-8" destdir="${basedir}/bin" verbose="${full-compile}" classpathref="classpath.test" includeantruntime="off" debug="on" debuglevel="lines,vars,source" /> <javac srcdir="${srcdir}" encoding="UTF-8" destdir="${basedir}/bin" verbose="${full-compile}" classpathref="classpath.test" includeantruntime="off" debug="on" debuglevel="lines,vars,source" />
</target> </target>
<path id="classes"> <path id="classes">
<fileset dir="${libdir}" includes="*jar"/> <fileset dir="${libdir}" includes="*jar"/>
<fileset dir="${libdir}" includes="*zip"/> <fileset dir="${libdir}" includes="*zip"/>
<pathelement location="${basedir}/bin/"/> <pathelement location="${basedir}/bin/"/>
</path> </path>
<!--如果testng里面没有配置reportng监听器,则使用这个方式,如果testng里面配置了 reportng监听器,那么就使用这个配置 <!--如果testng里面没有配置reportng监听器,则使用这个方式,如果testng里面配置了 reportng监听器,那么就使用这个配置
<target name="runtest" depends="compile"> <target name="runtest" depends="compile">
<testng outputdir="${testoutputdir}" classpathref="classes" <testng outputdir="${testoutputdir}" classpathref="classes"
useDefaultListeners="false" useDefaultListeners="false"
delegateCommandSystemProperties="true" haltOnFailure="true" delegateCommandSystemProperties="true" haltOnFailure="true"
listeners="org.uncommons.reportng.HTMLReporter,org.uncommons.reportng.JUnitXMLReporter,org.testng.reporters.FailedReporter"> listeners="org.uncommons.reportng.HTMLReporter,org.uncommons.reportng.JUnitXMLReporter,org.testng.reporters.FailedReporter">
<sysproperty key="file.encoding" value="UTF-8" /> <sysproperty key="file.encoding" value="UTF-8" />
<xmlfileset dir="${basedir}" includes="testng.xml" /> <xmlfileset dir="${basedir}" includes="testng.xml" />
</testng> </testng>
</target>--> </target>-->
<!--如果testng里面配置了 reportng监听器,那么就使用这个配置--> <!--如果testng里面配置了 reportng监听器,那么就使用这个配置-->
<target name="runtest" depends="compile"> <target name="runtest" depends="compile">
<!----> <!---->
<testng outputdir="${testoutputdir}" classpathref="classes" <testng outputdir="${testoutputdir}" classpathref="classes"
useDefaultListeners="false" > useDefaultListeners="false" >
<sysproperty key="file.encoding" value="UTF-8" /> <sysproperty key="file.encoding" value="UTF-8" />
<xmlfileset dir="${basedir}" includes="testng.xml" /> <xmlfileset dir="${basedir}" includes="testng.xml" />
</testng> </testng>
</target> </target>
<target name="testoutput" depends="runtest"> <target name="testoutput" depends="runtest">
<java classname="com.offcn.TestUnti.Mail" classpath="${basedir}/bin"> <java classname="com.offcn.TestUnti.Mail" classpath="${basedir}/bin">
<sysproperty key="file.encoding" value="UTF-8" /> <sysproperty key="file.encoding" value="UTF-8" />
<classpath> <classpath>
<pathelement location="lib/javax.mail.jar" /> <pathelement location="lib/javax.mail.jar" />
</classpath> </classpath>
</java> </java>
<!-- <!--
ui自动化测试报告部分 ui自动化测试报告部分
<xslt in="${testoutputdir}/testng-results.xml" style="${testoutputdir}/testng-results.xsl" out="${testoutputdir}/index.html "> <xslt in="${testoutputdir}/testng-results.xml" style="${testoutputdir}/testng-results.xsl" out="${testoutputdir}/index.html ">
<param name="testNgXslt.outputDir" expression="${basedir}/testoutput/" /> <param name="testNgXslt.outputDir" expression="${basedir}/testoutput/" />
<classpath refid="classpath.test" /> <classpath refid="classpath.test" />
</xslt> </xslt>
--> -->
</target> </target>
</project> </project>
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
<classes> <classes>
<class name="com.puhui.test.RenMai_APITest"> <class name="com.puhui.test.RenMai_APITest">
<methods> <methods>
<include name="f" invocation-numbers="1 2 "/> <include name="f" invocation-numbers=""/>
</methods> </methods>
</class> </class>
</classes> </classes>
......
package com.offcn.TestUnti; package com.offcn.TestUnti;
import javax.activation.DataHandler; import javax.activation.DataHandler;
import javax.activation.FileDataSource; import javax.activation.FileDataSource;
import javax.mail.Session; import javax.mail.Session;
import javax.mail.Transport; import javax.mail.Transport;
import javax.mail.internet.AddressException; import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress; import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeMultipart;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.Properties; import java.util.Properties;
import java.util.StringTokenizer; import java.util.StringTokenizer;
public class Mail { public class Mail {
public static void main(String[] args) { public static void main(String[] args) {
try { try {
Mail.POP3(); Mail.POP3();
} catch (Exception e) { } catch (Exception e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
} }
@Test @Test
public static void mailToAll(){ public static void mailToAll(){
try { try {
Mail.POP3(); Mail.POP3();
} catch (Exception e) { } catch (Exception e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
} }
// 发件人的 邮箱 和 密码(替换为自己的邮箱和密码) // 发件人的 邮箱 和 密码(替换为自己的邮箱和密码)
public static String myEmailAccount = "53055975@qq.com"; public static String myEmailAccount = "lyyazxhhwj@163.com";
public static String myEmailPassword = "qoqfoabljzyzbjdd"; public static String myEmailPassword = "liyyandzxx";
// 发件人邮箱的 SMTP 服务器地址, 必须准确, 不同邮件服务器地址不同, 一般格式为: smtp.xxx.com // 发件人邮箱的 SMTP 服务器地址, 必须准确, 不同邮件服务器地址不同, 一般格式为: smtp.xxx.com
// 网易163邮箱的 SMTP 服务器地址为: smtp.163.com // 网易163邮箱的 SMTP 服务器地址为: smtp.163.com or smtp.qq.com
public static String myEmailSMTPHost = "smtp.qq.com"; public static String myEmailSMTPHost = "smtp.163.com";
// 收件人邮箱(替换为自己知道的有效邮箱) // 收件人邮箱(替换为自己知道的有效邮箱)
// public static String receiveMailAccount = "liyongyu@offcn.com";//rmscrum.list@finupgroup.com // public static String receiveMailAccount = "liyongyu@offcn.com";//rmscrum.list@finupgroup.com
public static String receiveMailAccount = "yaoshuai@offcn.com";//rmscrum.list@finupgroup.com public static String receiveMailAccount = "liyongyu@offcn.com";//rmscrum.list@finupgroup.com
public static void POP3() throws Exception { public static void POP3() throws Exception {
// 1. 创建参数配置, 用于连接邮件服务器的参数配置 // 1. 创建参数配置, 用于连接邮件服务器的参数配置
Properties props = new Properties(); // 参数配置 Properties props = new Properties(); // 参数配置
props.setProperty("mail.transport.protocol", "smtp"); // 使用的协议(JavaMail规范要求) props.setProperty("mail.transport.protocol", "smtp"); // 使用的协议(JavaMail规范要求)
props.setProperty("mail.host", myEmailSMTPHost); // 发件人的邮箱的 SMTP 服务器地址 props.setProperty("mail.host", myEmailSMTPHost); // 发件人的邮箱的 SMTP 服务器地址
props.setProperty("mail.smtp.auth", "true"); // 请求认证,参数名称与具体实现有关 props.setProperty("mail.smtp.auth", "true"); // 请求认证,参数名称与具体实现有关
props.setProperty("mail.stmp.port", "465"); // 端口 props.setProperty("mail.stmp.port", "465"); // 端口
// 2. 根据配置创建会话对象, 用于和邮件服务器交互 // 2. 根据配置创建会话对象, 用于和邮件服务器交互
Session session = Session.getDefaultInstance(props); Session session = Session.getDefaultInstance(props);
session.setDebug(true); // 设置为debug模式, 可以查看详细的发送 log session.setDebug(true); // 设置为debug模式, 可以查看详细的发送 log
// 3. 创建一封邮件 // 3. 创建一封邮件
MimeMessage message = createMimeMessage(session, myEmailAccount, receiveMailAccount); MimeMessage message = createMimeMessage(session, myEmailAccount, receiveMailAccount);
// 4. 根据 Session 获取邮件传输对象 // 4. 根据 Session 获取邮件传输对象
Transport transport = session.getTransport(); Transport transport = session.getTransport();
// 5. 使用 邮箱账号 和 密码 连接邮件服务器 // 5. 使用 邮箱账号 和 密码 连接邮件服务器
// 这里认证的邮箱必须与 message 中的发件人邮箱一致,否则报错 // 这里认证的邮箱必须与 message 中的发件人邮箱一致,否则报错
transport.connect(myEmailAccount, myEmailPassword); transport.connect(myEmailAccount, myEmailPassword);
// 6. 发送邮件, 发到所有的收件地址, message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人 // 6. 发送邮件, 发到所有的收件地址, message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人
transport.sendMessage(message, message.getAllRecipients()); transport.sendMessage(message, message.getAllRecipients());
// 7. 关闭连接 // 7. 关闭连接
transport.close(); transport.close();
} }
/** /**
* 创建一封只包含文本的简单邮件 * 创建一封只包含文本的简单邮件
* *
* @param session 和服务器交互的会话 * @param session 和服务器交互的会话
* @param sendMail 发件人邮箱 * @param sendMail 发件人邮箱
* @param receiveMail 收件人邮箱 * @param receiveMail 收件人邮箱
* @return * @return
* @throws Exception * @throws Exception
*/ */
public static MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail) throws Exception { public static MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail) throws Exception {
// 1. 创建一封邮件 // 1. 创建一封邮件
MimeMessage message = new MimeMessage(session); MimeMessage message = new MimeMessage(session);
// 2. From: 发件人 // 2. From: 发件人
message.setFrom(new InternetAddress(sendMail, "定时任务-接口自动化测试报告", "UTF-8")); message.setFrom(new InternetAddress(sendMail, "定时任务-接口自动化测试报告", "UTF-8"));
// 3. To: 收件人(可以增加多个收件人、抄送、密送) // 3. To: 收件人(可以增加多个收件人、抄送、密送)
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, "", "UTF-8")); message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, "", "UTF-8"));
String overview=ReadHtml.readFile_("overview.html"); String overview=ReadHtml.readFile_("overview.html");
// System.out.println(overview); // System.out.println(overview);
String TestTitle=""; String TestTitle="";
String TestResule=""; String TestResule="";
boolean isTrue=overview.contains("100.00%"); boolean isTrue=overview.contains("100.00%");
int start=overview.indexOf("<td class=\"passRate\">"); int start=overview.indexOf("<td class=\"passRate\">");
System.out.println(start); System.out.println(start);
String Percentage=overview.substring(start+22, start+41); String Percentage=overview.substring(start+22, start+41);
System.out.println(Percentage); System.out.println(Percentage);
if(isTrue){ if(isTrue){
message.setRecipients(MimeMessage.RecipientType.CC, parseAddress("onlyTest")); message.setRecipients(MimeMessage.RecipientType.CC, parseAddress("onlyTest"));
}else{ }else{
message.setRecipients(MimeMessage.RecipientType.CC, parseAddress("all")); message.setRecipients(MimeMessage.RecipientType.CC, parseAddress("all"));
} }
// message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress("wufeifei66504@offcn.com", "", "UTF-8")); // message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress("wufeifei66504@offcn.com", "", "UTF-8"));
// message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress("liyongyu@offcn.com", "", "UTF-8")); // message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress("liyongyu@offcn.com", "", "UTF-8"));
// message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress("lihong60540@offcn.com", "", "UTF-8")); // message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress("lihong60540@offcn.com", "", "UTF-8"));
// message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress("yangfan60144@offcn.com", "", "UTF-8")); // message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress("yangfan60144@offcn.com", "", "UTF-8"));
// message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress("lifeifei58407@offcn.com", "", "UTF-8")); // message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress("lifeifei58407@offcn.com", "", "UTF-8"));
// message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress("tangqingsong@offcn.com", "", "UTF-8")); // message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress("tangqingsong@offcn.com", "", "UTF-8"));
// message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress("zhouyeheng@offcn.com", "", "UTF-8")); // message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress("zhouyeheng@offcn.com", "", "UTF-8"));
// message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress("zhanghua54164@offcn.com", "", "UTF-8")); // message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress("zhanghua54164@offcn.com", "", "UTF-8"));
// 4. Subject: 邮件主题 // 4. Subject: 邮件主题
Date date=new Date(); Date date=new Date();
String time=new SimpleDateFormat("yyyyMMdd").format(date); String time=new SimpleDateFormat("yyyyMMdd").format(date);
message.setSubject("学习中心&在线课堂自动化测试报告-"+time, "UTF-8"); message.setSubject("学习中心&在线课堂自动化测试报告-"+time, "UTF-8");
/* /*
* 下面是邮件内容的创建: * 下面是邮件内容的创建:
*/ */
// message.setContent("定时任务--小雨直播接口自动化测试", "text/html;charset=UTF-8"); // message.setContent("定时任务--小雨直播接口自动化测试", "text/html;charset=UTF-8");
// 5. 创建图片"节点" // 5. 创建图片"节点"
// MimeBodyPart image = new MimeBodyPart(); // MimeBodyPart image = new MimeBodyPart();
// // 读取本地文件 // // 读取本地文件
// DataHandler dh = new DataHandler(new FileDataSource("src\\mailTestPic.png")); // DataHandler dh = new DataHandler(new FileDataSource("src\\mailTestPic.png"));
// // 将图片数据添加到"节点" // // 将图片数据添加到"节点"
// image.setDataHandler(dh); // image.setDataHandler(dh);
// // 为"节点"设置一个唯一编号(在文本"节点"将引用该ID) // // 为"节点"设置一个唯一编号(在文本"节点"将引用该ID)
// image.setContentID("mailTestPic"); // image.setContentID("mailTestPic");
// //
// 6. 创建文本"节点" // 6. 创建文本"节点"
MimeBodyPart text = new MimeBodyPart(); MimeBodyPart text = new MimeBodyPart();
// 获取邮件内容原位置 // 获取邮件内容原位置
// String overview=ReadHtml.readFile_("overview.html"); // String overview=ReadHtml.readFile_("overview.html");
//// System.out.println(overview); //// System.out.println(overview);
// //
// String TestTitle=""; // String TestTitle="";
// String TestResule=""; // String TestResule="";
// //
// boolean isTrue=overview.contains("100.00%"); // boolean isTrue=overview.contains("100.00%");
// int start=overview.indexOf("<td class=\"passRate\">"); // int start=overview.indexOf("<td class=\"passRate\">");
// System.out.println(start); // System.out.println(start);
// String Percentage=overview.substring(start+22, start+41); // String Percentage=overview.substring(start+22, start+41);
// System.out.println(Percentage); // System.out.println(Percentage);
// System.out.println("isTrue="+isTrue); // System.out.println("isTrue="+isTrue);
if(isTrue){ if(isTrue){
TestTitle=" <font size='6' color='#00FF00'>本次测试通过率:"+Percentage+"</font></br></br> "; TestTitle=" <font size='6' color='#00FF00'>本次测试通过率:"+Percentage+"</font></br></br> ";
}else{ }else{
TestTitle=" <font size='6' color='#FF0000'>本次测试通过率:"+Percentage+"</font> </br></br>"; TestTitle=" <font size='6' color='#FF0000'>本次测试通过率:"+Percentage+"</font> </br></br>";
TestResule=ReadHtml.readFile_("suite1_test1_results.html"); TestResule=ReadHtml.readFile_("suite1_test1_results.html");
} }
// 这里添加图片的方式是将整个图片包含到邮件内容中, 实际上也可以以 http 链接的形式添加网络图片+ReadHtml.readFile_("suite1_test1_results.html") // 这里添加图片的方式是将整个图片包含到邮件内容中, 实际上也可以以 http 链接的形式添加网络图片+ReadHtml.readFile_("suite1_test1_results.html")
text.setContent(TestTitle+"</br></br>"+ReadHtml.readFile_("overview.html")+"</br></br>"+TestResule, "text/html;charset=UTF-8"); text.setContent(TestTitle+"</br></br>"+ReadHtml.readFile_("overview.html")+"</br></br>"+TestResule, "text/html;charset=UTF-8");
// //
// // 7. (文本+图片)设置 文本 和 图片"节点"的关系(将 文本 和 图片"节点"合成一个混合"节点") // // 7. (文本+图片)设置 文本 和 图片"节点"的关系(将 文本 和 图片"节点"合成一个混合"节点")
// MimeMultipart mm_text_image = new MimeMultipart(); // MimeMultipart mm_text_image = new MimeMultipart();
// mm_text_image.addBodyPart(text); // mm_text_image.addBodyPart(text);
// mm_text_image.addBodyPart(image); // mm_text_image.addBodyPart(image);
// mm_text_image.setSubType("related"); // 关联关系 // mm_text_image.setSubType("related"); // 关联关系
// //
// // 8. 将 文本+图片 的混合"节点"封装成一个普通"节点" // // 8. 将 文本+图片 的混合"节点"封装成一个普通"节点"
// // 最终添加到邮件的 Content 是由多个 BodyPart 组成的 Multipart, 所以我们需要的是 BodyPart, // // 最终添加到邮件的 Content 是由多个 BodyPart 组成的 Multipart, 所以我们需要的是 BodyPart,
// // 上面的 mailTestPic 并非 BodyPart, 所有要把 mm_text_image 封装成一个 BodyPart // // 上面的 mailTestPic 并非 BodyPart, 所有要把 mm_text_image 封装成一个 BodyPart
// MimeBodyPart text_image = new MimeBodyPart(); // MimeBodyPart text_image = new MimeBodyPart();
// text_image.setContent(mm_text_image); // text_image.setContent(mm_text_image);
MimeBodyPart attachment2 = new MimeBodyPart(); MimeBodyPart attachment2 = new MimeBodyPart();
File directory = new File("."); File directory = new File(".");
String mailpath=""; String mailpath="";
try { try {
mailpath = directory.getCanonicalPath() +File.separator+"test-output"+File.separator+"html\\"; mailpath = directory.getCanonicalPath() +File.separator+"test-output"+File.separator+"html\\";
} catch (IOException e1) { } catch (IOException e1) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e1.printStackTrace(); e1.printStackTrace();
} }
if(ZipUtils.ZipTestResult(mailpath)){ if(ZipUtils.ZipTestResult(mailpath)){
DataHandler dh2 = new DataHandler(new FileDataSource(mailpath+"测试报告详细资料.zip")); DataHandler dh2 = new DataHandler(new FileDataSource(mailpath+"测试报告详细资料.zip"));
attachment2.setDataHandler(dh2); attachment2.setDataHandler(dh2);
attachment2.setFileName("测试报告详细资料.zip"); attachment2.setFileName("测试报告详细资料.zip");
// // 9. 创建附件"节点" // // 9. 创建附件"节点"
// MimeBodyPart attachment3 = new MimeBodyPart(); // MimeBodyPart attachment3 = new MimeBodyPart();
// MimeBodyPart attachment4 = new MimeBodyPart(); // MimeBodyPart attachment4 = new MimeBodyPart();
// MimeBodyPart attachment5 = new MimeBodyPart(); // MimeBodyPart attachment5 = new MimeBodyPart();
// MimeBodyPart attachment6 = new MimeBodyPart(); // MimeBodyPart attachment6 = new MimeBodyPart();
// MimeBodyPart attachment7 = new MimeBodyPart(); // MimeBodyPart attachment7 = new MimeBodyPart();
// // 读取本地文件 // // 读取本地文件
// DataHandler dh2 = new DataHandler(new FileDataSource("test-output\\html\\index.html")); // DataHandler dh2 = new DataHandler(new FileDataSource("test-output\\html\\index.html"));
// DataHandler dh3 = new DataHandler(new FileDataSource("test-output\\html\\overview.html")); // DataHandler dh3 = new DataHandler(new FileDataSource("test-output\\html\\overview.html"));
// DataHandler dh4 = new DataHandler(new FileDataSource("test-output\\html\\reportng.css")); // DataHandler dh4 = new DataHandler(new FileDataSource("test-output\\html\\reportng.css"));
// DataHandler dh5 = new DataHandler(new FileDataSource("test-output\\html\\reportng.js")); // DataHandler dh5 = new DataHandler(new FileDataSource("test-output\\html\\reportng.js"));
// DataHandler dh6 = new DataHandler(new FileDataSource("test-output\\html\\suite1_test1_results.html")); // DataHandler dh6 = new DataHandler(new FileDataSource("test-output\\html\\suite1_test1_results.html"));
// DataHandler dh7 = new DataHandler(new FileDataSource("test-output\\html\\suites.html")); // DataHandler dh7 = new DataHandler(new FileDataSource("test-output\\html\\suites.html"));
// // 将附件数据添加到"节点" // // 将附件数据添加到"节点"
// attachment2.setDataHandler(dh2); // attachment2.setDataHandler(dh2);
// attachment3.setDataHandler(dh3); // attachment3.setDataHandler(dh3);
// attachment4.setDataHandler(dh4); // attachment4.setDataHandler(dh4);
// attachment5.setDataHandler(dh5); // attachment5.setDataHandler(dh5);
// attachment6.setDataHandler(dh6); // attachment6.setDataHandler(dh6);
// attachment7.setDataHandler(dh7); // attachment7.setDataHandler(dh7);
// // 设置附件的文件名(需要编码) // // 设置附件的文件名(需要编码)
// attachment2.setFileName("index.html"); // attachment2.setFileName("index.html");
// attachment3.setFileName("overview.html"); // attachment3.setFileName("overview.html");
// attachment4.setFileName("reportng.css"); // attachment4.setFileName("reportng.css");
// attachment5.setFileName("reportng.js"); // attachment5.setFileName("reportng.js");
// attachment6.setFileName("suite1_test1_results.html"); // attachment6.setFileName("suite1_test1_results.html");
// attachment7.setFileName("suites.html"); // attachment7.setFileName("suites.html");
} }
// 10. 设置(文本+图片)和 附件 的关系(合成一个大的混合"节点" / Multipart ) // 10. 设置(文本+图片)和 附件 的关系(合成一个大的混合"节点" / Multipart )
MimeMultipart mm = new MimeMultipart(); MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(text); mm.addBodyPart(text);
mm.addBodyPart(attachment2); // 如果有多个附件,可以创建多个多次添加 mm.addBodyPart(attachment2); // 如果有多个附件,可以创建多个多次添加
// mm.addBodyPart(attachment3); // 如果有多个附件,可以创建多个多次添加 // mm.addBodyPart(attachment3); // 如果有多个附件,可以创建多个多次添加
// mm.addBodyPart(attachment4); // 如果有多个附件,可以创建多个多次添加 // mm.addBodyPart(attachment4); // 如果有多个附件,可以创建多个多次添加
// mm.addBodyPart(attachment5); // 如果有多个附件,可以创建多个多次添加 // mm.addBodyPart(attachment5); // 如果有多个附件,可以创建多个多次添加
// mm.addBodyPart(attachment6); // 如果有多个附件,可以创建多个多次添加 // mm.addBodyPart(attachment6); // 如果有多个附件,可以创建多个多次添加
// mm.addBodyPart(attachment7); // 如果有多个附件,可以创建多个多次添加 // mm.addBodyPart(attachment7); // 如果有多个附件,可以创建多个多次添加
mm.setSubType("mixed"); // 混合关系 mm.setSubType("mixed"); // 混合关系
// 11. 设置整个邮件的关系(将最终的混合"节点"作为邮件的内容添加到邮件对象) // 11. 设置整个邮件的关系(将最终的混合"节点"作为邮件的内容添加到邮件对象)
message.setContent(mm); message.setContent(mm);
//设置邮件的发送时间,默认立即发送 //设置邮件的发送时间,默认立即发送
message.setSentDate(new Date()); message.setSentDate(new Date());
return message; return message;
} }
public static InternetAddress[] parseAddress(String personnel){ public static InternetAddress[] parseAddress(String personnel){
String addr=""; String addr="";
if("all".equals(personnel)){ if("all".equals(personnel)){
addr="wufeifei66504@offcn.com;wenshuang@offcn.com;liyongyu@offcn.com;" addr="liyongyu@offcn.com;"
+ "zhanghua54164@offcn.com;liwendong@offcn.com;cuimengzhen@offcn.com;" + "lyyazxhhwj@163.com;553834369@qq.com"; //失败接收人
+ "zhangshijie62302@offcn.com;baimanbin@offcn.com"; }else{
}else{ addr="553834369@qq.com;liyongyu@offcn.com"; // 成功接收人
addr="wufeifei66504@offcn.com;wenshuang@offcn.com;liyongyu@offcn.com"; }
}
StringTokenizer token = new StringTokenizer(addr, ";");
StringTokenizer token = new StringTokenizer(addr, ";"); InternetAddress[] addrArr = new InternetAddress[token.countTokens()];
InternetAddress[] addrArr = new InternetAddress[token.countTokens()]; int i = 0;
int i = 0; while (token.hasMoreTokens()){
while (token.hasMoreTokens()){ try{
try{ addrArr[i] = new InternetAddress(token.nextToken().toString());
addrArr[i] = new InternetAddress(token.nextToken().toString()); }catch (AddressException e1){
}catch (AddressException e1){ e1.printStackTrace();
e1.printStackTrace(); return null;
return null; }
} i++;
i++; }
} return addrArr;
return addrArr; }
}
} }
\ No newline at end of file
package com.offcn.TestUnti; package com.offcn.TestUnti;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
/** /**
* ZipUtils * ZipUtils
* @author ZENG.XIAO.YAN * @author ZENG.XIAO.YAN
* @date 2017年11月19日 下午7:16:08 * @date 2017年11月19日 下午7:16:08
* @version v1.0 * @version v1.0
*/ */
public class ZipUtils { public class ZipUtils {
private static final int BUFFER_SIZE = 2 * 1024; private static final int BUFFER_SIZE = 2 * 1024;
/** /**
* 压缩成ZIP 方法1 * 压缩成ZIP 方法1
* @param srcDir 压缩文件夹路径 * @param srcDir 压缩文件夹路径
* @param out 压缩文件输出流 * @param out 压缩文件输出流
* @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构; * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构;
* false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败) * false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
* @throws RuntimeException 压缩失败会抛出运行时异常 * @throws RuntimeException 压缩失败会抛出运行时异常
*/ */
public static void toZip(String srcDir, OutputStream out, boolean KeepDirStructure) public static void toZip(String srcDir, OutputStream out, boolean KeepDirStructure)
throws RuntimeException{ throws RuntimeException{
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
ZipOutputStream zos = null ; ZipOutputStream zos = null ;
try { try {
zos = new ZipOutputStream(out); zos = new ZipOutputStream(out);
File sourceFile = new File(srcDir); File sourceFile = new File(srcDir);
compress(sourceFile,zos,sourceFile.getName(),KeepDirStructure); compress(sourceFile,zos,sourceFile.getName(),KeepDirStructure);
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
System.out.println("压缩完成,耗时:" + (end - start) +" ms"); System.out.println("压缩完成,耗时:" + (end - start) +" ms");
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("zip error from ZipUtils",e); throw new RuntimeException("zip error from ZipUtils",e);
}finally{ }finally{
if(zos != null){ if(zos != null){
try { try {
zos.close(); zos.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
} }
/** /**
* 压缩成ZIP 方法2 * 压缩成ZIP 方法2
* @param srcFiles 需要压缩的文件列表 * @param srcFiles 需要压缩的文件列表
* @param out 压缩文件输出流 * @param out 压缩文件输出流
* @throws RuntimeException 压缩失败会抛出运行时异常 * @throws RuntimeException 压缩失败会抛出运行时异常
*/ */
public static void toZip(List<File> srcFiles , OutputStream out)throws RuntimeException { public static void toZip(List<File> srcFiles , OutputStream out)throws RuntimeException {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
ZipOutputStream zos = null ; ZipOutputStream zos = null ;
try { try {
zos = new ZipOutputStream(out); zos = new ZipOutputStream(out);
for (File srcFile : srcFiles) { for (File srcFile : srcFiles) {
byte[] buf = new byte[BUFFER_SIZE]; byte[] buf = new byte[BUFFER_SIZE];
zos.putNextEntry(new ZipEntry(srcFile.getName())); zos.putNextEntry(new ZipEntry(srcFile.getName()));
int len; int len;
FileInputStream in = new FileInputStream(srcFile); FileInputStream in = new FileInputStream(srcFile);
while ((len = in.read(buf)) != -1){ while ((len = in.read(buf)) != -1){
zos.write(buf, 0, len); zos.write(buf, 0, len);
} }
zos.closeEntry(); zos.closeEntry();
in.close(); in.close();
} }
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
System.out.println("压缩完成,耗时:" + (end - start) +" ms"); System.out.println("压缩完成,耗时:" + (end - start) +" ms");
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("zip error from ZipUtils",e); throw new RuntimeException("zip error from ZipUtils",e);
}finally{ }finally{
if(zos != null){ if(zos != null){
try { try {
zos.close(); zos.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
} }
/** /**
* 递归压缩方法 * 递归压缩方法
* @param sourceFile 源文件 * @param sourceFile 源文件
* @param zos zip输出流 * @param zos zip输出流
* @param name 压缩后的名称 * @param name 压缩后的名称
* @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构; * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构;
* false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败) * false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
* @throws Exception * @throws Exception
*/ */
private static void compress(File sourceFile, ZipOutputStream zos, String name, private static void compress(File sourceFile, ZipOutputStream zos, String name,
boolean KeepDirStructure) throws Exception{ boolean KeepDirStructure) throws Exception{
byte[] buf = new byte[BUFFER_SIZE]; byte[] buf = new byte[BUFFER_SIZE];
if(sourceFile.isFile()){ if(sourceFile.isFile()){
// 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字 // 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
zos.putNextEntry(new ZipEntry(name)); zos.putNextEntry(new ZipEntry(name));
// copy文件到zip输出流中 // copy文件到zip输出流中
int len; int len;
FileInputStream in = new FileInputStream(sourceFile); FileInputStream in = new FileInputStream(sourceFile);
while ((len = in.read(buf)) != -1){ while ((len = in.read(buf)) != -1){
zos.write(buf, 0, len); zos.write(buf, 0, len);
} }
// Complete the entry // Complete the entry
zos.closeEntry(); zos.closeEntry();
in.close(); in.close();
} else { } else {
File[] listFiles = sourceFile.listFiles(); File[] listFiles = sourceFile.listFiles();
if(listFiles == null || listFiles.length == 0){ if(listFiles == null || listFiles.length == 0){
// 需要保留原来的文件结构时,需要对空文件夹进行处理 // 需要保留原来的文件结构时,需要对空文件夹进行处理
if(KeepDirStructure){ if(KeepDirStructure){
// 空文件夹的处理 // 空文件夹的处理
zos.putNextEntry(new ZipEntry(name + "/")); zos.putNextEntry(new ZipEntry(name + "/"));
// 没有文件,不需要文件的copy // 没有文件,不需要文件的copy
zos.closeEntry(); zos.closeEntry();
} }
}else { }else {
for (File file : listFiles) { for (File file : listFiles) {
// 判断是否需要保留原来的文件结构 // 判断是否需要保留原来的文件结构
if (KeepDirStructure) { if (KeepDirStructure) {
// 注意:file.getName()前面需要带上父文件夹的名字加一斜杠, // 注意:file.getName()前面需要带上父文件夹的名字加一斜杠,
// 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了 // 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
compress(file, zos, name + "/" + file.getName(),KeepDirStructure); compress(file, zos, name + "/" + file.getName(),KeepDirStructure);
} else { } else {
compress(file, zos, file.getName(),KeepDirStructure); compress(file, zos, file.getName(),KeepDirStructure);
} }
} }
} }
} }
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
/** 测试压缩方法1 */ /** 测试压缩方法1 */
// FileOutputStream fos1 = new FileOutputStream(new File("c:/mytest01.zip")); // FileOutputStream fos1 = new FileOutputStream(new File("c:/mytest01.zip"));
// //
// ZipUtils.toZip("D:/log", fos1,true); // ZipUtils.toZip("D:/log", fos1,true);
/** 测试压缩方法2 */ /** 测试压缩方法2 */
// List<File> fileList = new ArrayList<>(); // List<File> fileList = new ArrayList<>();
// //
// fileList.add(new File(mailpath+"index.html")); // fileList.add(new File(mailpath+"index.html"));
// fileList.add(new File(mailpath+"overview.html")); // fileList.add(new File(mailpath+"overview.html"));
// fileList.add(new File(mailpath+"reportng.css")); // fileList.add(new File(mailpath+"reportng.css"));
// fileList.add(new File(mailpath+"reportng.js")); // fileList.add(new File(mailpath+"reportng.js"));
// fileList.add(new File(mailpath+"suite1_test1_results.html")); // fileList.add(new File(mailpath+"suite1_test1_results.html"));
// fileList.add(new File(mailpath+"suites.html")); // fileList.add(new File(mailpath+"suites.html"));
// //
// //
// FileOutputStream fos2 = new FileOutputStream(new File(mailpath+"mytest02.zip")); // FileOutputStream fos2 = new FileOutputStream(new File(mailpath+"mytest02.zip"));
// //
// ZipUtils.toZip(fileList, fos2); // ZipUtils.toZip(fileList, fos2);
} }
public static boolean ZipTestResult(String mailpath){ public static boolean ZipTestResult(String mailpath){
List<File> fileList = new ArrayList<>(); List<File> fileList = new ArrayList<>();
fileList.add(new File(mailpath+"index.html")); fileList.add(new File(mailpath+"index.html"));
fileList.add(new File(mailpath+"overview.html")); fileList.add(new File(mailpath+"overview.html"));
fileList.add(new File(mailpath+"reportng.css")); fileList.add(new File(mailpath+"reportng.css"));
fileList.add(new File(mailpath+"reportng.js")); fileList.add(new File(mailpath+"reportng.js"));
fileList.add(new File(mailpath+"suite1_test1_results.html")); fileList.add(new File(mailpath+"suite1_test1_results.html"));
fileList.add(new File(mailpath+"suites.html")); fileList.add(new File(mailpath+"suites.html"));
FileOutputStream fos2=null; FileOutputStream fos2=null;
try { try {
fos2 = new FileOutputStream(new File(mailpath+"测试报告详细资料.zip")); fos2 = new FileOutputStream(new File(mailpath+"测试报告详细资料.zip"));
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
return false; return false;
} }
ZipUtils.toZip(fileList, fos2); ZipUtils.toZip(fileList, fos2);
return true; return true;
} }
} }
...@@ -68,6 +68,7 @@ public class moment extends STUDY implements API { ...@@ -68,6 +68,7 @@ public class moment extends STUDY implements API {
@Override @Override
public Response SendRequest(HashMap<String, Object> data, String Url, public Response SendRequest(HashMap<String, Object> data, String Url,
String Request) { String Request) {
//form 格式参数
/* Map<String,String> m=new HashMap<String,String>(); /* Map<String,String> m=new HashMap<String,String>();
m.put("user_id", user_id); m.put("user_id", user_id);
m.put("image", user_id); m.put("image", user_id);
...@@ -95,6 +96,7 @@ public class moment extends STUDY implements API { ...@@ -95,6 +96,7 @@ public class moment extends STUDY implements API {
MyRequest myRequest=new MyRequest(); MyRequest myRequest=new MyRequest();
myRequest.setParameter(parameter); myRequest.setParameter(parameter);
myRequest.setUrl(Url); myRequest.setUrl(Url);
System.out.println("Url====" +Url); System.out.println("Url====" +Url);
myRequest.setRequest(Request); myRequest.setRequest(Request);
myRequest.setHeaders(ticket); myRequest.setHeaders(ticket);
......
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none"> <suite name="Suite" parallel="none">
<listeners> <listeners>
<listener class-name="org.uncommons.reportng.HTMLReporter" /> <listener class-name="org.uncommons.reportng.HTMLReporter" />
<listener class-name="org.uncommons.reportng.JUnitXMLReporter" /> <listener class-name="org.uncommons.reportng.JUnitXMLReporter" />
</listeners> <!-- --> </listeners> <!-- -->
<test name="Test"> <test name="Test">
<classes> <classes>
<class name="com.offcn.test.APITest_nwn"/> <class name="com.offcn.test.APITest_study"/>
</classes> </classes>
</test> </test>
<!-- <!--
<test name="Test1"> <test name="Test1">
<classes> <classes>
<class name="com.offcn.TestUnti.Mail"/> <class name="com.offcn.TestUnti.Mail"/>
</classes> </classes>
</test> Test --> </test> Test -->
</suite> <!-- Suite --> </suite> <!-- Suite -->
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment