码迷,mamicode.com
首页 > 其他好文 > 详细

TestNG 判断文件下载成功

时间:2018-06-07 20:52:40      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:over   ide   exception   try   creat   shutdown   xlsx   new   文件下载   

用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:
*/
Screenshot代码

 

TestNG 判断文件下载成功

标签:over   ide   exception   try   creat   shutdown   xlsx   new   文件下载   

原文地址:https://www.cnblogs.com/cheese320/p/9152583.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!