2017-04-06 94 views
0

我在具有IP的遠程機器上安裝了Selenium Grid。我的一個測試用例需要下載文件,並且在聲明中我想比較下載文件的名稱,在另一個測試用例中,我必須將文件從Windows導入到應用程序。 如何在Java中做到這一點? Selenium Grid在Windows Server 2008上。Selenium Grid如何使用Java從文件中獲取文件?

回答

0

據我所知,硒是不可能的。您可能能夠獲取瀏覽器日誌,但我所做的是啓用對共享服務器的文件訪問,並檢查文件是否已下載到此處。首先,我將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"); 
+0

非常感謝你,它對我來說非常合適,我認爲我必須在機器之間配置膩子和ssh連接。 – robmax

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或機器人類的,如果上傳的鏈接打開的窗口對話框。

相關問題