1 package testNG; 2 3 import java.awt.Desktop; 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.FileOutputStream; 7 import java.io.FileWriter; 8 import java.io.IOException; 9 import java.io.FileNotFoundException; 10 import java.io.Writer; 11 import java.text.SimpleDateFormat; 12 import java.util.Date; 13 import java.util.regex.Matcher; 14 import java.util.regex.Pattern; 15 16 import javax.xml.crypto.Data; 17 18 import org.apache.commons.io.FileUtils; 19 import org.apache.poi.ss.usermodel.Cell; 20 import org.apache.poi.xssf.usermodel.XSSFRow; 21 import org.apache.poi.xssf.usermodel.XSSFSheet; 22 import org.apache.poi.xssf.usermodel.XSSFWorkbook; 23 import org.openqa.selenium.By; 24 import org.openqa.selenium.OutputType; 25 import org.openqa.selenium.TakesScreenshot; 26 import org.openqa.selenium.WebDriver; 27 import org.openqa.selenium.chrome.ChromeDriver; 28 import org.openqa.selenium.firefox.FirefoxDriver; 29 import org.openqa.selenium.ie.InternetExplorerDriver; 30 import org.openqa.selenium.remote.DesiredCapabilities; 31 32 33 @SuppressWarnings("unused") 34 35 public class BaseClass { 36 /*** 37 * 基础类,包括方法:启动webdriver、获取基础数据、获取测试数据、返回当前时间、写入结果文件、 38 * 截图、打开结果文件、去除空格换行及制表符的字符串 39 * 40 * @param args 41 * @throws IOException 42 * @throws InterruptedException 43 */ 44 public static WebDriver startBrowser(String browserType){ 45 /** 46 * FunName: startBrowser 47 * Description : 启动浏览器,支持ie/fireFox/chrome 48 * @param: String browserType 49 * @return WebDriver: driver 50 * @Author: Bingo 51 * @Create Date: 2015-07-21 52 **/ 53 WebDriver wd = null; 54 if (browserType == "ie"){ 55 System.setProperty("webdriver.ie.driver", "drivers\\IEDriverServer.exe");//ie驱动路径 56 DesiredCapabilities dc = DesiredCapabilities.internetExplorer(); 57 dc.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); 58 //关闭IE浏览器保护模式 59 dc.setCapability("ignoreProtectedModeSettings", true); 60 wd = new InternetExplorerDriver(); 61 }else if(browserType == "fireFox"){ 62 System.setProperty("webdriver.firefox.bin", //firefox的安装路径 63 "D:/Program Files/Mozilla Firefox/firefox.exe"); 64 wd = new FirefoxDriver(); 65 }else if(browserType == "chrome"){ 66 System.setProperty("webdriver.chrome.driver", "drivers\\chromedriver.exe");//驱动路径 67 wd = new ChromeDriver(); 68 } 69 return wd; 70 71 } 72 73 74 //读取测试URL、测试数据文件路径和结果文件路径 75 public String getBaseData(String testInfo, String baseDataPath) throws IOException{ 76 /** 77 * FunName: getBaseData 78 * Description : 获取测试URL、测试数据文件路径和结果文件路径 79 * @param: String testInfo 80 * @return String: 返回基础数据字符串; 81 * @Author: Bingo 82 * @Create Date: 2015-06-29 83 **/ 84 String testString = null; 85 FileInputStream file = new FileInputStream(baseDataPath); 86 @SuppressWarnings("resource") 87 XSSFWorkbook wb = new XSSFWorkbook(file); 88 XSSFSheet sheet = wb.getSheetAt(0); 89 XSSFRow rowinf = sheet.getRow(1); 90 Cell cell = null; 91 switch(testInfo){ 92 case "inPath" : cell = rowinf.getCell(0);break; 93 case "outPath" : cell = rowinf.getCell(1);break; 94 case "baseURL" : cell = rowinf.getCell(2);break; 95 case "firefoxPath" : cell = rowinf.getCell(3);break; 96 case "screenShotDir" : cell = rowinf.getCell(4);break; 97 case "snapshotDir" : cell = rowinf.getCell(5);break; 98 case "xiaoWeiIssuerURL" : cell = rowinf.getCell(6);break; 99 case "Tesseract-OCRPath" : cell = rowinf.getCell(7);break; 100 default: System.out.println("no testInfo found!"); 101 } 102 cell.setCellType(Cell.CELL_TYPE_STRING); 103 testString= cell.getStringCellValue(); 104 105 return testString; 106 } 107 108 109 //读取信息,被测试方法调用,参数为方法名、列名colNm、文件路径inPath 110 public String getData_xlsx(String funNm, String colNm, String inPath) throws FileNotFoundException, IOException{ 111 /** 112 * FunName: getData_xlsx 113 * Description : 读取信息,供被测试方法调用 114 * @param: String funNm, String colNm, String inPath 115 * @return String: 返回测试数据; 116 * @Author: Bingo 117 * @Create Date: 2015-06-29 118 **/ 119 String str1 = null; 120 FileInputStream file = new FileInputStream(inPath); 121 @SuppressWarnings("resource") 122 XSSFWorkbook wb = new XSSFWorkbook(file); 123 XSSFSheet sheet = null; 124 Cell cell = null; 125 //根据方法名,读取对应的sheet 126 if (funNm == "Login"){ 127 sheet = wb.getSheetAt(1); 128 } else if (funNm == "QueryApp"){ 129 sheet = wb.getSheetAt(2); 130 } 131 XSSFRow row1 = sheet.getRow(1); 132 //根据不同的列名,读取对应的列 133 if(colNm == "orgz"||colNm == "appNo") 134 cell = row1.getCell(0); 135 136 else if(colNm == "userId"||colNm == "holName") 137 cell = row1.getCell(1); 138 else if(colNm == "pwd"||colNm == "cerType") 139 cell = row1.getCell(2); 140 else if(colNm == "cerNo"||colNm == "xiaoWeiUserId") 141 cell = row1.getCell(3); 142 else if(colNm == "xiaoWeiUserPwd") 143 cell = row1.getCell(4); 144 else System.out.println(nowDate()+" err funcName or colNume"); 145 cell.setCellType(Cell.CELL_TYPE_STRING); 146 str1= cell.getStringCellValue(); 147 return str1; 148 } 149 //写入测试结果文件,参数为方法名、是否通过、文件路径和执行时间 150 public void outCome_xlsx(String funNm, String outCome, String outPath,String date) throws IOException 151 { 152 /** 153 * FunName: outCome_xlsx 154 * Description : 写入测试结果 155 * @param: String funNm, String outCome, String outPath,String date 156 * @return void: 无返回数据; 157 * @Author: Bingo 158 * @Create Date: 2015-06-29 159 **/ 160 FileInputStream file = new FileInputStream(outPath); 161 @SuppressWarnings("resource") 162 XSSFWorkbook wb = new XSSFWorkbook(file); 163 XSSFSheet sheet = wb.getSheetAt(0); 164 int trLength = sheet.getLastRowNum(); 165 XSSFRow newrow = sheet.createRow((short)(trLength+1)); 166 newrow.createCell(0).setCellValue(funNm); 167 newrow.createCell(1).setCellValue(outCome); 168 newrow.createCell(2).setCellValue(date); 169 FileOutputStream fout = new FileOutputStream(outPath); 170 wb.write(fout); 171 file.close(); 172 fout.close(); 173 174 } 175 176 // public File createLogFile(String fileName, String logDir){ 177 // File file = null; 178 // 179 // return file; 180 // } 181 // 182 183 //写入日志文件信息,文件名为yyyy-mm-dd.log 184 public static File writeLog(String logMsg) throws IOException 185 { 186 /** 187 * FunName: writeLog 188 * Description : 记录日志 189 * @param: File file, String msg 190 * @return file: 返回文件; 191 * @Author: Bingo 192 * @Create Date: 2015-07-21 193 **/ 194 //FileInputStream file = new FileInputStream(outPath); 195 String logDir = System.getProperty("user.dir")+"\\log\\"; 196 String time = null; 197 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式 198 time = sdf.format(new Date()).toString(); 199 File file = new File(logDir+time+".log"); 200 if(!file.exists()) { 201 file.createNewFile(); 202 } 203 @SuppressWarnings("resource") 204 Writer txtWriter = new FileWriter(file,true); 205 txtWriter.write(nowDate() +"\t"+logMsg+"\n"); 206 txtWriter.flush(); 207 return file; 208 } 209 210 public void openOutCome(String outPath){ 211 /** 212 * FunName: openOutCome 213 * Description : 打开测试结果文件 214 * @param: 文件URL 215 * @return void: 无返回数据; 216 * @Author: Bingo 217 * @Create Date: 2015-06-30 218 **/ 219 Desktop desk=Desktop.getDesktop(); 220 try 221 { 222 File file1=new File(outPath);//创建一个java文件系统 223 if (!file1.canWrite())//判断文件是否被占用 224 { 225 desk.open(file1); //调用open(File f)方法打开文件 226 }else { 227 FileInputStream in=new FileInputStream(file1); 228 in.close(); 229 desk.open(file1); 230 } 231 }catch(Exception e) 232 { 233 System.out.println(e.toString()); 234 } 235 236 } 237 238 public void screenShot(String screenOutDir,WebDriver driver,String funNm) throws InterruptedException{ 239 /** 240 * FunName: openOutCome 241 * Description : 截图 242 * @param: 文件URL 243 * @return void: 无返回数据; 244 * @Author: Bingo 245 * @Create Date: 2015-06-30 246 **/ 247 //Thread.sleep(3000); 248 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HHmmss-SSS"); 249 String time = sdf.format(new Date()); 250 251 String fileName = funNm+time +".png"; 252 try {// 执行屏幕截图,默认会把截图保存到temp目录 253 File source_file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 254 // 这里将截图另存到我们需要保存的目录,例如screenshot\20150406 165210-333.png 255 FileUtils.copyFile(source_file, new File(screenOutDir + "/" + fileName)); 256 } catch (IOException e) { 257 e.printStackTrace(); 258 } 259 } 260 261 public static String replaceBlank(String str) { 262 /** 263 * 返回STR中去除空格、换行制表符的内容 264 * 265 */ 266 267 String dest = ""; 268 if (str!=null) { 269 Pattern p = Pattern.compile("\\s*|\t|\r|\n"); 270 Matcher m = p.matcher(str); 271 dest = m.replaceAll(""); 272 } 273 return dest; 274 } 275 //获取当前系统时间 276 public static String nowDate(){ 277 String nowDate = null; 278 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式 279 nowDate =df.format(new Date()).toString(); 280 return nowDate; 281 } 282 283 //判断字符串是否纯数字 284 public boolean isDigitStr(String s){ 285 for(int i =0; i<s.length()-1;i++){ 286 if(!Character.isDigit(s.charAt(i))){ 287 return false; 288 } 289 } 290 return true; 291 } 292 293 //根据byType及值,返回by,访问方式:driver.findElement(getBy("id","kw")) 294 public static By getBy(String byType, String byValue){ 295 By by = null; 296 switch(byType){ 297 case "id": by = By.id(byValue);break; 298 case "xpath": by = By.xpath(byValue);break; 299 case "name": by = By.xpath(byValue);break; 300 case "className": by = By.className(byValue);break; 301 case "linkText": by = By.linkText(byValue);break; 302 case "cssSelector": by = By.cssSelector(byValue);break; 303 case "tagName": by = By.tagName(byValue);break; 304 case "partialLinkText": by = By.partialLinkText(byValue);break; 305 default:break; 306 307 } 308 return by; 309 } 310 public static void println(Object obj){ 311 /** 312 * FunName: println 313 * Description : 简版打印,代替System.out.print(object) 314 * @param: object 315 * @return String: none 316 * @Author: Bingoreo 317 * @Create Date: 2015-07-15 318 **/ 319 System.out.println(obj); 320 } 321 322 public static void print(Object obj){ 323 /** 324 * FunName: println 325 * Description : 简版打印,代替System.out.print(object) 326 * @param: object 327 * @return String: none 328 * @Author: Bingoreo 329 * @Create Date: 2015-07-15 330 **/ 331 System.out.print(obj); 332 333 } 334 335 public static void println(){ 336 /** 337 * FunName: println 338 * Description : 简版打印,代替System.out.println() 339 * @param: none 340 * @return String: none 341 * @Author: Bingoreo 342 * @Create Date: 2015-07-15 343 **/ 344 System.out.println(); 345 } 346 public static void main(String[] args) throws IOException, InterruptedException { 347 // TODO 自动生成的方法存根 348 // BaseClass baseClass = new BaseClass(); 349 // String baseDatePath = "about:blank"; 350 // String inPath = baseClass.getBaseData("inPath",baseDatePath); 351 // String outPath = baseClass.getBaseData("outPath",baseDatePath); 352 // String baseURL = baseClass.getBaseData("baseURL",baseDatePath); 353 // String firefoxPath = baseClass.getBaseData("firefoxPath",baseDatePath); 354 // String orgz = baseClass.getData_xlsx("Login", "orgz", inPath); 355 // String userId = baseClass.getData_xlsx("Login", "userId", inPath); 356 // String pwd = baseClass.getData_xlsx("Login", "pwd", inPath); 357 // String appNo = baseClass.getData_xlsx("QueryApp", "appNo", inPath); 358 // String holName = baseClass.getData_xlsx("QueryApp", "holName", inPath); 359 // String cerType = baseClass.getData_xlsx("QueryApp", "cerType", inPath); 360 // String cerNo = baseClass.getData_xlsx("QueryApp", "cerNo", inPath); 361 // SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式 362 // String date =df.format(new Date()); 363 // baseClass.outCome_xlsx("测试方法", "是否通过", outPath, date); 364 // System.out.println("inPath:"+inPath+"\noutPath:"+outPath+"\nbaseURL:"+baseURL+"\nfirefoxPath"+firefoxPath); 365 // System.out.println("\norgz:"+orgz+"\nuserId:"+userId+"\npwdL:"+pwd+"\nappNo"+appNo); 366 // System.out.println("\nholName:"+holName+"\ncerType:"+cerType+"\ncerNo:"+cerNo); 367 // baseClass.openOutCome(outPath); 368 // WebDriver driver = new FirefoxDriver(); 369 // driver.get("https://www.baidu.com"); 370 // driver.findElement(getBy("id","kw")).sendKeys("11"); 371 // Thread.sleep(3000); 372 // String screenShotDir = baseClass.getBaseData("screenShotDir",baseDatePath); 373 // Thread.sleep(3000); 374 // baseClass.screenShot(screenShotDir, driver,"testFunNm"); 375 // driver.close(); 376 // System.out.println(System.getProperty("user.dir")) ; 377 // writeLog("4st msg"); 378 // writeLog("5nd msg"); 379 // writeLog("6rd msg"); 380 WebDriver ieDriver = startBrowser("ie"); 381 ieDriver.get("about:blank"); 382 // startBrowser("chrome"); 383 WebDriver fireFoxdriver = startBrowser("fireFox"); 384 fireFoxdriver.get("http://www.baidu.com"); 385 println("tt"); 386 387 } 388 389 }