Satu-satunya cara pasti untuk mengunggah dan mengunduh file dalam uji Selenium

gambar


Selenium WebDriver diciptakan sebagai alat lintas platform untuk mengelola browser web. Dan selama hampir 14 tahun sekarang dia telah melakukan pekerjaan ini dengan sangat baik. Namun, autotest dunia nyata menciptakan situasi di mana Selenium tidak berdaya. Misalnya, menurut skrip, Anda perlu mengunggah atau mengunduh file. Setelah mengklik tombol "Unduh" atau "Unduh", jendela pengelola file sistem operasi muncul di bagian atas jendela peramban tempat Selenium tidak lagi memiliki akses. Tes berhenti.


Saya mendengar rekomendasi untuk menggunakan utilitas seperti AutoIt atau Sikuli untuk bekerja dengan sistem windows tersebut. Saran saya adalah jangan pernah melakukan ini, ini adalah praktik jahat yang mengarah pada tes yang tidak stabil:


  • -. .
  • . , .
  • 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]")


gambar


, findElement() . . , . , .


gambar


Selenium

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



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


, , :


gambar


, . . :


gambar


, . 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, .


, , . , . — .



Agar tes tetap stabil, faktor yang membuatnya rapuh harus dihilangkan. Dialog sistem operasi, itu adalah salah satu faktor tersebut. Dalam artikel-artikel berikut ini, saya berencana untuk mengungkapkan aspek-aspek penting lainnya yang sangat memengaruhi operasi tes Selenium yang stabil. Dengarkan podcast QA Guild dan otomatisasi yang baik.


All Articles