测试是否已下载文件Selenium / C#(Google Chrome)

Nau*_*nja 5 c# selenium

我想单击一个单击click即可下载文件的按钮,并测试是否已经下载了预期的文件。

我已经用谷歌搜索过,但是不幸的是没有找到关于这个主题的任何具体答案,我发现的很多帖子已经过时了(2014年),我敢打赌Selenium现在必须已经更改了API定义。

Shu*_*ain 5

使用以下代码:-

import org.openqa.selenium.By;
import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class FileDownloadVerify {

 private WebDriver driver;

 private static String downloadPath = "D:\\siva";
 private String URL="http://all-free-download.com/free-photos/download/in_love_cosmos_flower_garden_220378.html";

 @BeforeClass
 public void testSetup() throws Exception{
  driver = new FirefoxDriver(firefoxProfile()); 
  driver.manage().window().maximize();
 }

  @Test
 public void example_VerifyExpectedFileName() throws Exception {
  driver.get(URL);
     driver.findElement(By.xpath(".//*[@id='detail_content']/div[2]/a")).click();

     Thread.sleep(10000);
     File getLatestFile = getLatestFilefromDir(downloadPath);
     String fileName = getLatestFile.getName();
     Assert.assertTrue(fileName.equals("in_love_cosmos_flower_garden_220378.zip"), "Downloaded file name is not matching with expected file name");
 }


 @AfterClass
 public void tearDown() {
  driver.quit();
 }
public static FirefoxProfile firefoxProfile() throws Exception {

  FirefoxProfile firefoxProfile = new FirefoxProfile();
  firefoxProfile.setPreference("browser.download.folderList",2);
  firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
  firefoxProfile.setPreference("browser.download.dir",downloadPath);
  firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/zip");

  return firefoxProfile;
 }
public boolean isFileDownloaded(String downloadPath, String fileName) {
 boolean flag = false;
    File dir = new File(downloadPath);
    File[] dir_contents = dir.listFiles();

    for (int i = 0; i < dir_contents.length; i++) {
        if (dir_contents[i].getName().equals(fileName))
            return flag=true;
            }

    return flag;
}

private boolean isFileDownloaded_Ext(String dirPath, String ext){
 boolean flag=false;
    File dir = new File(dirPath);
    File[] files = dir.listFiles();
    if (files == null || files.length == 0) {
        flag = false;
    }

    for (int i = 1; i < files.length; i++) {
     if(files[i].getName().contains(ext)) {
      flag=true;
     }
    }
    return flag;
}

private File getLatestFilefromDir(String dirPath){
    File dir = new File(dirPath);
    File[] files = dir.listFiles();
    if (files == null || files.length == 0) {
        return null;
    }

    File lastModifiedFile = files[0];
    for (int i = 1; i < files.length; i++) {
       if (lastModifiedFile.lastModified() < files[i].lastModified()) {
           lastModifiedFile = files[i];
       }
    }
    return lastModifiedFile;
}
}
Run Code Online (Sandbox Code Playgroud)

希望它能对您有所帮助:)


Sud*_*thi 5

此代码验证文件是否已下载,然后将其删除。

private static bool CheckFileDownloaded(string filename)
{
    bool exist = false;
    string Path = System.Environment.GetEnvironmentVariable("USERPROFILE") + "\\Downloads";
    string[] filePaths = Directory.GetFiles(Path);
    foreach (string p in filePaths)
    {
        if(p.Contains(filename))
        {
            FileInfo thisFile = new FileInfo(p);
            //Check the file that are downloaded in the last 3 minutes
            if (thisFile.LastWriteTime.ToShortTimeString() == DateTime.Now.ToShortTimeString() ||
            thisFile.LastWriteTime.AddMinutes(1).ToShortTimeString() == DateTime.Now.ToShortTimeString() ||
            thisFile.LastWriteTime.AddMinutes(2).ToShortTimeString() == DateTime.Now.ToShortTimeString() ||
            thisFile.LastWriteTime.AddMinutes(3).ToShortTimeString() == DateTime.Now.ToShortTimeString())
            exist = true;
            File.Delete(p);
            break;
        }
    }
    return exist;
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。C#中的代码。