自動化測試如何處理檔案下載的視窗? (Python/Java範例)

自動化測試如何處理檔案下載的視窗? (Python/Java範例)

這篇文章主要說明當網站自動化測試遇到檔案下載時應該如何處理?

通常檔案下載時,都會有一個視窗選取檔案下載位置,

Selenium對於這樣檔案下載位置的視窗無法處理,

因此我們要如果自動化測試要做到自動下載檔案並且驗證,

需要透過別的方式!

測試情境

我們主要會利用這個網站 spreadsheetpage.com 到下列網址之後,下載mp3filelister.xls

程式說明

既然 selenium 無法處理檔案下載的視窗那要怎樣自動化呢?

這裡的小訣竅就是在瀏覽器啟動之前,將下載檔案的相關屬性設定好,例如設定好預設的網站儲存位置,設定好不出現檔案儲存的視窗等。

設定這些屬性就透過 FirefoxProfile

FirefoxProfile profile = new FirefoxProfile();

profile.setPreference(“browser.download.dir”, downloadPath);

 

另外一個問題是如何驗證檔案是否有成功下載呢?

有兩個方式,一個是驗證檔案名稱是否存在,另外一個方式比較準確就是驗證該檔案的 MD5。

因為如果檔案下載有問題或是不完整檔案 MD5的驗證是最完整。

Java範例程式

[pastacode lang=”java” message=”” highlight=”” provider=”manual”]



import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.Test;

public class TestDownload {
	public static String downloadPath = "D:\\";
	@Test
	public void testDownload() throws Exception {
		WebDriver driver = new FirefoxDriver(FirefoxDriverProfile());	
		driver.manage().window().maximize();
	    driver.get("http://spreadsheetpage.com/index.php/file/C35/P10/");
	    driver.findElement(By.linkText("mp3filelister.xls")).click();
	    driver.quit();
	}
	
	public static FirefoxProfile FirefoxDriverProfile() throws Exception {
		FirefoxProfile profile = new FirefoxProfile();
		profile.setPreference("browser.download.folderList", 2);
		profile.setPreference("browser.download.manager.showWhenStarting", false);
		profile.setPreference("browser.download.dir", downloadPath);
		profile.setPreference("browser.helperApps.neverAsk.openFile","text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
		profile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
		profile.setPreference("browser.helperApps.alwaysAsk.force", false);
		profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
		profile.setPreference("browser.download.manager.focusWhenStarting", false);
		profile.setPreference("browser.download.manager.useWindow", false);
		profile.setPreference("browser.download.manager.showAlertOnComplete", false);
		profile.setPreference("browser.download.manager.closeWhenDone", false);
		return profile;
	}
}

[/pastacode]

 

Python程式範例

[pastacode lang=”python” message=”” highlight=”” provider=”manual”]

import os

from selenium import webdriver

fp = webdriver.FirefoxProfile()

fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.dir", os.getcwd())
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream")

browser = webdriver.Firefox(firefox_profile=fp)
browser.get("http://pypi.python.org/pypi/selenium")
browser.find_element_by_partial_link_text("selenium-2").click()

[/pastacode]

 

Leave a Reply

Your email address will not be published. Required fields are marked *