在Selenium测试中上载和下载文件的唯一确定方法

图片


Selenium WebDriver被创建为用于管理Web浏览器的跨平台工具。在将近14年的时间里,他一直做得非常好。但是,现实世界中的自动测试会导致Selenium变得无能为力。例如,根据脚本,您需要上载或下载文件。单击“下载”或“下载”按钮后,操作系统文件管理器的窗口将显示在浏览器窗口的顶部,Selenium不再可以访问该窗口。测试停止。


我听说过建议使用诸如AutoItSikuli之类的实用程序来处理此类系统窗口。我的建议是永远不要这样做,这是一种恶性行为,会导致测试不稳定:


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


图片


, findElement() . . , . , .


图片


Selenium

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



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


, , :


图片


, . . :


图片


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


, , . , . — .



为了使测试保持稳定,必须删除使它们易碎的因素。操作系统的对话框,正是这种因素之一。在接下来的文章中,我计划揭示对硒测试的稳定运行产生重大影响的其他重要方面。收听质量检查协会的播客和良好的自动化。


All Articles