La única forma segura de cargar y descargar archivos en las pruebas de Selenium

imagen


Selenium WebDriver fue creado como una herramienta multiplataforma para administrar navegadores web. Y durante casi 14 años ha estado haciendo este trabajo muy, muy bien. Sin embargo, las pruebas automáticas del mundo real crean situaciones en las que Selenium no tiene poder. Por ejemplo, según un script, debe cargar o descargar un archivo. Después de hacer clic en el botón "Descargar" o "Descargar", aparece una ventana del administrador de archivos del sistema operativo en la parte superior de la ventana del navegador a la que Selenium ya no tiene acceso. La prueba se detiene.


Escuché recomendaciones para usar utilidades como AutoIt o Sikuli para trabajar con dichas ventanas del sistema. Mi consejo es que nunca hagas esto, esta es una práctica viciosa que conduce a pruebas inestables:


  • -. .
  • . , .
  • headless

. 15 8 . .


— , .



, . — "". , ( input file) sendKeys() "" :


By fileInput = By.cssSelector("input[type=file]");
String filePath = "/home/selenium/files/upload.txt";

driver.findElement(fileInput).sendKeys(filePath);

. Selenium . . . , , , .


, ? , , DOM . $$("input[type=file]")


imagen


, findElement() . . , . , .


imagen


Selenium

, . , Selenium , . — . (barancev) . , .



, . - , Downloads, Selenium ( Internet Explorer), "" .


, , :


imagen


, . . :


imagen


, . href . , , http . Java :


//Get download link 
String downloadLink = driver
         .findElement(By.cssSelector("main#content a.btn"))
         .getAttribute("href");

//Set file to save
File fileToSave = new File("/path/to/file.zip");

//Download file using default org.apache.http client 
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(downloadLink);
HttpResponse response = httpClient.execute(httpGet, new BasicHttpContext());

//Save file on disk
copyInputStreamToFile(response.getEntity().getContent(), fileToSave);

http

, . , JavaScript . , . , , , . , . , - . , , Selenium. BrowserUpProxy BrowserMobProxy.


, Selenide. DownloadFileWithProxyServer.


, , . https, .


, , . , . — .



Para que las pruebas se mantengan estables, se deben eliminar los factores que las hacen frágiles. Los cuadros de diálogo de un sistema operativo, es uno de esos factores. En los siguientes artículos planeo revelar otros aspectos importantes que afectan en gran medida el funcionamiento estable de las pruebas de selenio. Escucha el podcast QA Guild y una buena automatización.


All Articles