Download per Chrome, Safari i Firefox
This commit is contained in:
@@ -6,6 +6,7 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
@@ -31,46 +32,61 @@ public class DownloadTest extends BaseTest {
|
||||
File folder;
|
||||
|
||||
void moureUltimFitxerDescarregat(String rutaDestiFinal) {
|
||||
// 1. Ruta de la carpeta de descàrregues per defecte del Mac
|
||||
String rutaDownloadsGeneral = System.getProperty("user.home") + "/Downloads";
|
||||
File directori = new File(rutaDownloadsGeneral);
|
||||
File ultimFitxer = null;
|
||||
|
||||
// 2. Esperar una mica a que el fitxer s'hagi baixat completament (pots ajustar el temps)
|
||||
try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); }
|
||||
|
||||
// 3. Buscar el fitxer més recent de la carpeta
|
||||
// 1. Espera dinàmica (màxim 15 intents, 1 intent per segon)
|
||||
for (int i = 0; i < 15; i++) {
|
||||
File[] fitxers = directori.listFiles();
|
||||
if (fitxers != null && fitxers.length > 0) {
|
||||
Arrays.sort(fitxers, Comparator.comparingLong(File::lastModified).reversed());
|
||||
File ultimFitxer = fitxers[0];
|
||||
|
||||
// Evitem agafar fitxers temporals de descàrrega de Safari (com els .download)
|
||||
if (ultimFitxer.getName().endsWith(".download")) {
|
||||
System.out.println("El fitxer encara s'està descarregant...");
|
||||
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(); }
|
||||
}
|
||||
|
||||
// 2. Gestionar el fitxer trobat
|
||||
if (ultimFitxer == null) {
|
||||
System.err.println("Error: No s'ha detectat cap fitxer nou creat a la carpeta Downloads.");
|
||||
return;
|
||||
}
|
||||
|
||||
// 4. Moure el fitxer a la teva ruta desitjada
|
||||
// 3. Moure el fitxer a la teva destinació final
|
||||
try {
|
||||
Path origen = ultimFitxer.toPath();
|
||||
Path desti = Paths.get(rutaDestiFinal);
|
||||
|
||||
// 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());
|
||||
System.out.println("L'últim fitxer CREAT s'ha 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
|
||||
public void setUp() {
|
||||
|
||||
@@ -144,6 +160,7 @@ public class DownloadTest extends BaseTest {
|
||||
// Make sure the downloaded file(s) is(are) not empty
|
||||
assertThat(file.length(), is(not((long) 0)));
|
||||
}
|
||||
|
||||
String[] entries = folder.list();
|
||||
for (String s : entries) {
|
||||
File currentFile = new File(folder.getPath(), s);
|
||||
|
||||
Reference in New Issue
Block a user