1 package Utils; 2 3 import java.io.File; 4 import java.util.Date; 5 6 import org.apache.commons.lang3.time.DateFormatUtils; 7 import org.apache.log4j.Logger; 8 import org.testng.Reporter; 9 10 public class ReportUtils { 11 12 private static Logger logger = Logger.getLogger(ReportUtils.class.getName()); 13 14 public ReportUtils() { 15 System.setProperty("org.uncommons.reportng.escape-output", "false"); 16 } 17 18 /********************************************************************************************* 19 * 写log和报告操作 20 *********************************************************************************************/ 21 /** 22 * 写日志和报告 23 * 24 * @param comm 25 */ 26 public void log(String... comm) { 27 String time = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"); 28 if (comm.length == 0) { 29 Reporter.log("[" + time + "] <br />"); 30 logger.info(""); 31 } else { 32 Reporter.log("[" + time + "] " + comm[0] + "<br />"); 33 logger.info(comm[0]); 34 } 35 } 36 37 /** 38 * 写日志和报告 39 * 40 * @param comm 41 * @comment 如果selenium.properties中【isPrintDebugMsg】值为0,则只打印looger,不打印report 42 */ 43 public void debug(String... comm) { 44 if (1 == 1) { 45 log("[debug]" + comm[0]); 46 } else { 47 logger.info(comm[0]); 48 } 49 } 50 51 /** 52 * 写错误日志和报告 53 * 54 * @param comm 55 */ 56 public void error(String comm) { 57 String time = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"); 58 Reporter.log("<span style="color:#FF0000"><b>[" + time + "] [ERROR] " + comm + "</b></span><br />"); 59 logger.error(comm); 60 } 61 62 /** 63 * 写警告日志和报告 64 * 65 * @param comm 66 */ 67 public void warn(String comm) { 68 String time = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"); 69 Reporter.log("<span style="color:#FF7F27"><b>[" + time + "] [WARNING] " + comm + "</b></span><br />"); 70 logger.warn(comm); 71 } 72 73 /** 74 * 写重要的日志和报告 75 * 76 * @param comm 77 */ 78 public void highLight(String comm) { 79 log("<span style='background-color:#FFE500;'>" + comm + "</span>"); 80 } 81 82 /** 83 * 写绿色高亮的的日志和报告 84 * 85 * @param comm 86 */ 87 public void greenLight(String comm) { 88 log("<span style='background-color:#CFFFBA;'>" + comm + "</span>"); 89 } 90 91 /** 92 * 参数高亮 93 * 94 * @param comm 95 */ 96 public void paraLight(String comm) { 97 if (1 == 1) { 98 log("<span style='background-color:#E4FFD9;'>" + comm + "</span>"); 99 } else { 100 logger.info(comm); 101 } 102 } 103 104 /** 105 * 给MCDB用的日志和报告 106 * 107 * @param comm 108 */ 109 public void mcdbLight(String comm) { 110 if (1 == 1) { 111 log("<span style='background-color:#C1E7F7;'>[MCDB]" + comm + "</span>"); 112 } else { 113 logger.info("[MCDB]" + comm); 114 } 115 } 116 117 /** 118 * 写醒目的标题 119 * 120 * @param comm 121 */ 122 public void title(String comm) { 123 String str; 124 str = "<p style="color:#0068BD;margin-top:25px;margin-bottom:8px"><b>"; 125 str = str + "**********************************************************************************************<br>"; 126 str = str + "* " + comm + "<br>"; 127 str = str + "**********************************************************************************************</b>"; 128 str = str + "</p>"; 129 Reporter.log(str); 130 } 131 132 /** 133 * 用于截图的log写入 134 * 135 * @param comm 136 * @param file 137 */ 138 public void screenShotLog(String comm, File file) { 139 int width = 350; 140 String absolute = "file:" + file.getAbsolutePath(); 141 142 Reporter.log("<a target='_blank' href="" + absolute + "">"); 143 Reporter.log("<img width="" + width + "" src="" + absolute + "" /> " + comm); 144 Reporter.log("</a><br />"); 145 } 146 }