selenium自动化测试Java--判断文件是否下载成功

1.设置谷歌浏览器的下载路径,代码如下

 public static  WebDriver  setDownloadsPath() {
    	HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
        chromePrefs.put("download.default_directory", downloadsPath);
        ChromeOptions options = new ChromeOptions();
        options.setExperimentalOption("prefs", chromePrefs);
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability(ChromeOptions.CAPABILITY, options);
     WebDriver driver = new ChromeDriver(caps);
                return driver;
     
    }

2.获取下载的文件名,代码如下

public static String getDownloadedDocumentName(String filepath, String filename)
    {	
    	String downloadedFileName = null;
    	boolean valid = true;
    	boolean found = false;

    	//default timeout in seconds
    	long timeOut = 30; 
    	try 
    	{					
    		Path downloadFolderPath = Paths.get(filepath);
    		WatchService watchService = FileSystems.getDefault().newWatchService();
    		downloadFolderPath.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);
    		long startTime = System.currentTimeMillis();
    		do 
    		{
    			WatchKey watchKey;
    			watchKey = watchService.poll(timeOut,TimeUnit.SECONDS);
    			long currentTime = (System.currentTimeMillis()-startTime)/1000;
    			if(currentTime>timeOut)
    			{
    				System.out.println("Download operation timed out.. Expected file was not downloaded");
    				return downloadedFileName;
    			}
    			
    			for (WatchEvent<?> event: watchKey.pollEvents())
    			{
    				WatchEvent.Kind<?> kind = event.kind(); 
    				if (kind.equals(StandardWatchEventKinds.ENTRY_CREATE)) 
    				{
    					String fileName = event.context().toString();
    					System.out.println("New File Created:" + fileName);
    					if(fileName.endsWith(filename))
    					{
    						downloadedFileName = fileName;
    						System.out.println("Downloaded file found with extension " + filename + ". File name is " + 

    fileName);
    						Thread.sleep(500);
    						found = true;
    						break;
    					}
    				}
    			}
    			if(found)
    			{
    				return downloadedFileName;
    			}
    			else
    			{
    				currentTime = (System.currentTimeMillis()-startTime)/1000;
    				if(currentTime>timeOut)
    				{
    					System.out.println("Failed to download expected file");
    					return downloadedFileName;
    				}
    				valid = watchKey.reset();
    			}
    		} while (valid);
    	} 
    	
    	catch (InterruptedException e) 
    	{
    		System.out.println("Interrupted error - " + e.getMessage());
    		e.printStackTrace();
    	}
    	catch (NullPointerException e) 
    	{
    		System.out.println("Download operation timed out.. Expected file was not downloaded");
    	}
    	catch (Exception e)
    	{
    		System.out.println("Error occured - " + e.getMessage());
    		e.printStackTrace();
    	}
    	return downloadedFileName;
}

调用该方法如下

String fileName = getDownloadedDocumentName(downloadsPath,".xlsx");

3.判断指定文件夹下是否存在下载的文件,代码如下

private static Boolean isFileDownloaded_Ext(String filepath, String filename) {
    	boolean flag=false;
        File dir = new File(filepath);
        File[] files = dir.listFiles();
        if (files == null || files.length == 0) {
            flag = false;
        }
        
        for (int i = 0; i <files.length; i++) {
        	if(files[i].getName().contains(filename)) {
        		flag=true;
        	}
        }
        return flag;
	}

调用该方法如下

Boolean bt=isFileDownloaded_Ext(downloadsPath,fileName);
  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值