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.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++) {
|
||||||
|
|
||||||
// 3. Buscar el fitxer més recent de la carpeta
|
|
||||||
File[] fitxers = directori.listFiles();
|
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 (fitxers != null && fitxers.length > 0) {
|
||||||
if (ultimFitxer.getName().endsWith(".download")) {
|
ultimFitxer = Arrays.stream(fitxers)
|
||||||
System.out.println("El fitxer encara s'està descarregant...");
|
.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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. Moure el fitxer a la teva ruta desitjada
|
// 3. Moure el fitxer a la teva destinació final
|
||||||
try {
|
try {
|
||||||
Path origen = ultimFitxer.toPath();
|
Path origen = ultimFitxer.toPath();
|
||||||
Path desti = Paths.get(rutaDestiFinal);
|
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)) {
|
if (Files.isDirectory(desti)) {
|
||||||
desti = desti.resolve(ultimFitxer.getName());
|
desti = desti.resolve(ultimFitxer.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
Files.move(origen, desti, StandardCopyOption.REPLACE_EXISTING);
|
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) {
|
} catch (IOException e) {
|
||||||
System.err.println("Error en moure el fitxer: " + e.getMessage());
|
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);
|
||||||
|
|||||||
Reference in New Issue
Block a user