TestNG 判断文件下载成功
Posted cheese320
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TestNG 判断文件下载成功相关的知识,希望对你有一定的参考价值。
用WatchService写一个方法放在onTestStart()方法里监听文件夹的变化. 但是判断下载成功还需要写一个方法, 用来判断什么时候文件从.xlsx.rcdownload改成.xlsx才行 (TODO).
package com.tcemo.ui.bean; import static com.tcemo.ui.bean.ScreenShotOnFailure.SCREEN_SHOT_NAME; import static com.tcemo.ui.bean.ScreenShotOnFailure.SCREEN_SHOT_PATH; import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardWatchEventKinds; import java.nio.file.WatchEvent; import java.nio.file.WatchKey; import java.nio.file.WatchService; import java.util.List; import org.testng.ITestContext; import org.testng.ITestListener; import org.testng.ITestResult; public class TestngRetryListener implements ITestListener{ private WatchService watchService; public void onTestFailure(ITestResult result) { try { ScreenShotOnFailure.takeScreenShot(); System.out.println(result.getMethod().getMethodName()+"failed, the screenshot saved in " + SCREEN_SHOT_PATH +" screenshot name: " + SCREEN_SHOT_NAME); } catch (Exception e) { e.printStackTrace(); } } @Override public void onTestStart(ITestResult result) { // TODO Auto-generated method stub // 需要监听的文件目录(只能监听目录) String path = "E:\smartesterlocal\download"; try { watchService = FileSystems.getDefault().newWatchService(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } Path p = Paths.get(path); try { p.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_CREATE); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } Thread thread = new Thread(() -> { try { while(true){ WatchKey watchKey = watchService.take(); List<WatchEvent<?>> watchEvents = watchKey.pollEvents(); for(WatchEvent<?> event : watchEvents){ //TODO 根据事件类型采取不同的操作。。。。。。。 System.out.println("["+path+"/"+event.context()+"]文件发生了["+event.kind()+"]事件"); } watchKey.reset(); } } catch (InterruptedException e) { e.printStackTrace(); } }); thread.setDaemon(false); thread.start(); // 增加jvm关闭的钩子来关闭监听 Runtime.getRuntime().addShutdownHook(new Thread(() -> { try { watchService.close(); } catch (Exception e) { } })); } @Override public void onTestSuccess(ITestResult result) { // TODO Auto-generated method stub } @Override public void onTestSkipped(ITestResult result) { // TODO Auto-generated method stub } @Override public void onTestFailedButWithinSuccessPercentage(ITestResult result) { // TODO Auto-generated method stub } @Override public void onStart(ITestContext context) { // TODO Auto-generated method stub } @Override public void onFinish(ITestContext context) { // TODO Auto-generated method stub } } /** *@author: Created by QianHJ *@date: 2018年4月20日 *@problem: *@answer: *@action: */
package com.tcemo.ui.bean; import static com.tcemo.ui.bean.AntBrowser.browser; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.io.FileUtils; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; public class ScreenShotOnFailure { public static final String SCREEN_SHOT_PATH = "test-output/screen-shot"; public static String SCREEN_SHOT_NAME = null; public static void takeScreenShot() throws IOException { File screenshotDir = new File(SCREEN_SHOT_PATH); if(!screenshotDir.exists()) { screenshotDir.mkdirs(); } SimpleDateFormat smf = new SimpleDateFormat("yyyyMMddHHmmss"); SCREEN_SHOT_NAME = String.valueOf(smf.format(new Date())) + ".jpg"; FileUtils.copyFile(((TakesScreenshot)browser.getWebDriver()).getScreenshotAs(OutputType.FILE), new File(SCREEN_SHOT_PATH + "/"+ SCREEN_SHOT_NAME)); } } /** *@author: Created by QianHJ *@date: 2018年4月20日 *@problem: *@answer: *@action: */
以上是关于TestNG 判断文件下载成功的主要内容,如果未能解决你的问题,请参考以下文章