Selenium Grid how to get file from it with Java?

时间:2017-04-06 17:01:27

标签: java selenium selenium-grid

I have Selenium Grid on remote machine with IP. One of my test cases has to download the file and in assertion I want to compare the name of downloaded file, also in another test case I have to import file to application from Windows. How to do that in Java? Selenium Grid is on Windows Server 2008.

2 个答案:

答案 0 :(得分:1)

据我所知,单独使用硒是不可能的。您可能能够获取浏览器日志,但我所做的是启用对共享服务器的文件访问,并检查该文件是否已下载到该服务器。首先,我设置了Chrome的下载目录:

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("download.default_directory", "\\remote-ip\path\to\download\directory");
options.setExperimentalOption("prefs", prefs);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new RemoteWebDriver(new URL("http://gridhubhost:4444/wd/hub"), capabilities);

然后在测试使浏览器下载文件后,我检查远程服务器上的文件系统:

File downloadedFile = new File("\\remote-ip\path\to\download\directory\file");
assertEquals(downloadedFile.getName(), "expected-name");

[edit]:你可能最好断言文件存在,例如:

assertTrue(downloadedFile.exists());

答案 1 :(得分:0)

您可以使用java.io.File

检查下载的文件是否存在于路径中
File f = new File(filePathString);
if(f.exists() && !f.isDirectory()) { 
   Sustem.out.printLn("File is downloaded");
}

另外,如果要导入,则必须检查文件导入是否存在可编辑的输入字段,如果是,则可以直接使用sendkeys,如下所示:

  driver.findElement(By.xpath("upload input path")).sendKeys("C:/Users/1.pdf");

否则,如果上传链接打开窗口对话框,您将不得不使用AutoIt或Robot类。