![图片](https://habrastorage.org/webt/h0/dm/r4/h0dmr476ri52gbtxfkx0wnhehry.png)
Selenium WebDriver被创建为用于管理Web浏览器的跨平台工具。在将近14年的时间里,他一直做得非常好。但是,现实世界中的自动测试会导致Selenium变得无能为力。例如,根据脚本,您需要上载或下载文件。单击“下载”或“下载”按钮后,操作系统文件管理器的窗口将显示在浏览器窗口的顶部,Selenium不再可以访问该窗口。测试停止。
我听说过建议使用诸如AutoIt或Sikuli之类的实用程序来处理此类系统窗口。我的建议是永远不要这样做,这是一种恶性行为,会导致测试不稳定:
. 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]")
![图片](https://habrastorage.org/webt/nn/n4/w7/nnn4w732-9j7ey1rs6sti37amd4.png)
, findElement()
. . , . , .
![图片](https://habrastorage.org/webt/k6/ko/09/k6ko09f8xvqlxp5keubqhzkj4us.png)
Selenium, . , Selenium , . — . (barancev) . , .
, . - , Downloads, Selenium ( Internet Explorer), "" .
, , :
![图片](https://habrastorage.org/webt/3_/gq/xq/3_gqxqxj9kjhj7wizuwmu78viqe.png)
, . . :
![图片](https://habrastorage.org/webt/gx/-d/vw/gx-dvwwzmvwgrqzwhdlazhjlaie.png)
, . href
. , , http . Java :
String downloadLink = driver
.findElement(By.cssSelector("main#content a.btn"))
.getAttribute("href");
File fileToSave = new File("/path/to/file.zip");
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(downloadLink);
HttpResponse response = httpClient.execute(httpGet, new BasicHttpContext());
copyInputStreamToFile(response.getEntity().getContent(), fileToSave);
, . , JavaScript . , . , , , . , . , - . , , Selenium. BrowserUpProxy BrowserMobProxy.
, Selenide. DownloadFileWithProxyServer.
, , . https, .
, , . , . — .
为了使测试保持稳定,必须删除使它们易碎的因素。操作系统的对话框,正是这种因素之一。在接下来的文章中,我计划揭示对硒测试的稳定运行产生重大影响的其他重要方面。收听质量检查协会的播客和良好的自动化。