Selenium断言文件是否下载成功

项目中,存在点击后下载的业务流程,而selenium本身没有很好的方法去断言文件是否下载成功。此时我们可以通过WatchService去监听目录文件,来确定文件是否下载成功。

//监听所下载的文件名
        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;
    }

确认文件是否下载完成

/确认文件下载是否完成
        public 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;
        }

通过上面的方法即可得到下载后的文件名,然后通过下载的文件和用例中的预期做断言即可

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员的世界你不懂

你的鼓励将是我创造的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值