Download per Chrome, Safari i Firefox

This commit is contained in:
Guillem Hernandez Sola
2026-06-12 12:57:20 +02:00
parent fb07c83fe3
commit b6a959d022

View File

@@ -6,6 +6,7 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
@@ -31,46 +32,61 @@ public class DownloadTest extends BaseTest {
File folder; File folder;
void moureUltimFitxerDescarregat(String rutaDestiFinal) { void moureUltimFitxerDescarregat(String rutaDestiFinal) {
// 1. Ruta de la carpeta de descàrregues per defecte del Mac
String rutaDownloadsGeneral = System.getProperty("user.home") + "/Downloads"; String rutaDownloadsGeneral = System.getProperty("user.home") + "/Downloads";
File directori = new File(rutaDownloadsGeneral); File directori = new File(rutaDownloadsGeneral);
File ultimFitxer = null;
// 2. Esperar una mica a que el fitxer s'hagi baixat completament (pots ajustar el temps) // 1. Espera dinàmica (màxim 15 intents, 1 intent per segon)
try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } for (int i = 0; i < 15; i++) {
File[] fitxers = directori.listFiles();
if (fitxers != null && fitxers.length > 0) {
ultimFitxer = Arrays.stream(fitxers)
.filter(File::isFile) // GARANTEIX QUE ÉS UN FITXER (NO UNA FOLDER)
.filter(f -> !f.getName().startsWith(".")) // Ignora fitxers ocults (.DS_Store)
.filter(f -> !f.getName().endsWith(".download")) // Ignora temporals de Safari
.max(Comparator.comparingLong(f -> {
try {
// Llegeix la data exacta de CREACIÓ del fitxer al Mac
BasicFileAttributes attr = Files.readAttributes(f.toPath(), BasicFileAttributes.class);
return attr.creationTime().toMillis();
} catch (IOException e) {
return 0L; // Si falla, el posa a la cua
}
}))
.orElse(null);
}
// Si hem trobat un fitxer real, sortim del bucle d'espera
if (ultimFitxer != null) {
break;
}
// Si no el troba o Safari encara el descarrega, espera 1 segon i torna-ho a intentar
try { Thread.sleep(1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
}
// 3. Buscar el fitxer més recent de la carpeta // 2. Gestionar el fitxer trobat
File[] fitxers = directori.listFiles(); if (ultimFitxer == null) {
if (fitxers != null && fitxers.length > 0) { System.err.println("Error: No s'ha detectat cap fitxer nou creat a la carpeta Downloads.");
Arrays.sort(fitxers, Comparator.comparingLong(File::lastModified).reversed()); return;
File ultimFitxer = fitxers[0]; }
// Evitem agafar fitxers temporals de descàrrega de Safari (com els .download) // 3. Moure el fitxer a la teva destinació final
if (ultimFitxer.getName().endsWith(".download")) { try {
System.out.println("El fitxer encara s'està descarregant..."); Path origen = ultimFitxer.toPath();
return; Path desti = Paths.get(rutaDestiFinal);
if (Files.isDirectory(desti)) {
desti = desti.resolve(ultimFitxer.getName());
} }
// 4. Moure el fitxer a la teva ruta desitjada Files.move(origen, desti, StandardCopyOption.REPLACE_EXISTING);
try { System.out.println("L'últim fitxer CREAT s'ha mogut correctament a: " + desti.toString());
Path origen = ultimFitxer.toPath(); } catch (IOException e) {
Path desti = Paths.get(rutaDestiFinal); System.err.println("Error en moure el fitxer: " + e.getMessage());
// Si la ruta destí és només una carpeta, hi afegim el nom del fitxer original
if (Files.isDirectory(desti)) {
desti = desti.resolve(ultimFitxer.getName());
}
Files.move(origen, desti, StandardCopyOption.REPLACE_EXISTING);
System.out.println("Fitxer mogut correctament a: " + desti.toString());
} catch (IOException e) {
System.err.println("Error en moure el fitxer: " + e.getMessage());
}
} else {
System.err.println("No s'ha trobat cap fitxer a la carpeta de descàrregues.");
} }
} }
@BeforeMethod @BeforeMethod
public void setUp() { public void setUp() {
@@ -144,6 +160,7 @@ public class DownloadTest extends BaseTest {
// Make sure the downloaded file(s) is(are) not empty // Make sure the downloaded file(s) is(are) not empty
assertThat(file.length(), is(not((long) 0))); assertThat(file.length(), is(not((long) 0)));
} }
String[] entries = folder.list(); String[] entries = folder.list();
for (String s : entries) { for (String s : entries) {
File currentFile = new File(folder.getPath(), s); File currentFile = new File(folder.getPath(), s);