我在selenium java中编写了一个自动化测试,用于检测页面是否重定向(自动检测是否打开了新页面,页面重定向到其他页面,打开了新选项卡以及是否打开了警报窗口)< / p>
现在问题。 其中一个重定向我找不到任何方法来检测是一个自动下载的文件(你进入一个网站,网站自动下载文件,没有任何来自用户的触发器)
P.S。
我知道每个浏览器的下载过程可能不同, 我需要它主要用于chrome
谢谢
答案 0 :(得分:3)
我有同样的问题,这是我在互联网上找到的地方(也许是在stackoverflow,我不记得了)。我刚刚添加了一行来删除文件,以便在测试开始时调用此方法,我确保在尝试再次下载时该文件不再存在。
public boolean isFileDownloaded(String downloadPath, String fileName) {
File dir = new File(downloadPath);
File[] dirContents = dir.listFiles();
for (int i = 0; i < dirContents.length; i++) {
if (dirContents[i].getName().equals(fileName)) {
// File has been found, it can now be deleted:
dirContents[i].delete();
return true;
}
}
return false;
}
你只需要用这一行来打电话: isFileDownloaded(&#34; C:\ Path \ To \ Your \ Folder&#34;,&#34; yourPdfFile.abc&#34;);
希望这有帮助!
答案 1 :(得分:2)
我的解决方案是在打开页面之前和之后计算下载目录中的文件。
我很高兴知道是否有人知道找到下载触发器的方法
答案 2 :(得分:1)
我使用了“存在”方法
public boolean isFileDownloaded() throws Exception {
final int SLEEP_TIME_MILLIS = 1000;
File file = new File(filePath);
final int timeout = 60* SLEEP_TIME_MILLIS;
int timeElapsed = 0;
while (timeElapsed<timeout){
if (file.exists()) {
System.out.println(fileName + " is present");
return true;
} else {
timeElapsed +=SLEEP_TIME_MILLIS;
Thread.sleep(SLEEP_TIME_MILLIS);
}
}
return false;
}
答案 3 :(得分:0)
我在自动化中处理类似的情况。
步骤1:使用chrome首选项
在chrome中设置下载路径ChromeOptions options = new ChromeOptions();
HashMap<String, Object> chromePref = new HashMap<>();
chromePref.put("download.default_directory", <Directory to download file>);
options.setExperimentalOption("prefs", chromePref);
确保您下载的文件夹中没有包含预期文件名的文件。
第2步:导航到chrome中的url,文件将自动下载到指定的文件夹中。
步骤3:检查下载文件夹中是否存在文件。
答案 4 :(得分:0)
我使用了来自不同来源的组合变体:
覆盖默认下载文件夹:
ChromeOptions options = new ChromeOptions();
HashMap<String, Object> chromePref = new HashMap<>();
chromePref.put("download.default_directory", System.getProperty("java.io.tmpdir"));
options.setExperimentalOption("prefs", chromePref);
WebDriver driver = new ChromeDriver(options);
方法正文:
WebDriverWait wait = new WebDriverWait(driver, 5);
String tmpFolderPath = System.getProperty("java.io.tmpdir");
String expectedFileName = "Some_file_name.ext";
File file = new File(tmpFolderPath + expectedFileName);
if (file.exists())
file.delete();
// Start downloading here.
wait.until((ExpectedCondition<Boolean>) webDriver -> file.exists());
// Do what you need.
答案 5 :(得分:0)
我开发了library,在这种情况下处理得更清楚。 您可以使用给定的下载文件夹生成ChromeOptions对象,并使用单行方法调用来下载文件并验证继承:
private SeleniumDownloadKPI seleniumDownloadKPI;
@BeforeEach
void setUpTest() {
seleniumDownloadKPI =
new SeleniumDownloadKPI("/tmp/downloads");
ChromeOptions chromeOptions =
seleniumDownloadKPI.generateDownloadFolderCapability();
driver = new ChromeDriver(chromeOptions);
}
@Test
void downloadAttachTest() throws InterruptedException {
adamInternetPage.navigateToPage(driver);
seleniumDownloadKPI.fileDownloadKPI(
adamInternetPage.getFileDownloadLink(), "SpeedTest_16MB.dat");
waitBeforeClosingBrowser();
}
答案 6 :(得分:0)
这种方法对我来说成功了
/**
* This method will wait until the folder is having any downloads
* @throws InterruptedException
*/
public static void waitUntilFileToDownload(String folderLocation) throws InterruptedException {
File directory = new File(folderLocation);
boolean downloadinFilePresence = false;
File[] filesList =null;
LOOP:
while(true) {
filesList = directory.listFiles();
for (File file : filesList) {
downloadinFilePresence = file.getName().contains(".crdownload");
}
if(downloadinFilePresence) {
for(;downloadinFilePresence;) {
sleep(5);
continue LOOP;
}
}else {
break;
}
}
}
答案 7 :(得分:0)
这非常适合我:
public static void waitForTheExcelFileToDownload(String fileName, int timeWait)
throws IOException, InterruptedException {
String downloadPath = getSystemDownloadPath();
File dir = new File(downloadPath);
File[] dirContents = dir.listFiles();
for (int i = 0; i < 3; i++) {
if (dirContents[i].getName().equalsIgnoreCase(fileName)) {
break;
}else {
Thread.sleep(timeWait);
}
}
}
答案 8 :(得分:0)
希望这会有所帮助!
public static Boolean isFileDownloaded(String fileName) {
boolean flag = false;
//paste your directory path below
//eg: C:\\Users\\username\\Downloads
String dirPath = "";
File dir = new File(dirPath);
File[] files = dir.listFiles();
if (files.length == 0 || files == null) {
System.out.println("The directory is empty");
flag = false;
} else {
for (File listFile : files) {
if (listFile.getName().contains(fileName)) {
System.out.println(fileName + " is present");
break;
}
flag = true;
}
}
return flag;
}
答案 9 :(得分:0)
在当前用户的下载文件夹中的脚本执行文件下载过程中。
使用下面的代码检查和下载文件
public void delte_file(String filename)
{
String home = System.getProperty("user.home");
String file_name = filename;
String file_with_location = home + "\\Downloads\\" + file_name;
System.out.println("Function Name ===========================" + home + "\\Downloads\\" + file_name);
File file = new File(file_with_location);
if (file.exists()) {
System.out.println(file_with_location + " is present");
if (file.delete()) {
System.out.println("file deleted");
} else {
System.out.println("file not deleted");
}
} else {
System.out.println(file_with_location + " is not present");
}
}
要检查文件是否存在,请使用以下代码:
public static String check_file_exist(String filename)
{
String home = System.getProperty("user.home");
String file_name = filename;
String file_with_location = home + "\\Downloads\\" + file_name;
System.out.println("Function Name ===========================" + home + "\\Downloads\\" + file_name);
File file = new File(file_with_location);
if (file.exists()) {
System.out.println(file_with_location + " is present");
String result = "File Present";
return result;
} else {
System.out.println(file_with_location + " is not present");
String result = "File not Present";
String result1 = result;
return result1;
}
}
答案 10 :(得分:-1)
String downloadPath = "C:\\Users\\Updoer\\Downloads";
File getLatestFile = getLatestFilefromDir(downloadPath);
String fileName = getLatestFile.getName();
Assert.assertTrue(fileName.equals("Inspections.pdf"), "Downloaded file
name is not matching with expected file name");
----------------每次您需要删除下载的文件时,请添加 此代码也---------
File file = new File("C:\\Users\\Updoer\\Downloads\\Inspections.pdf");
if(file.delete())
System.out.println("file deleted");
System.out.println("file not deleted");
-----在此代码下添加方法-------
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;
}