Skip to content

Instantly share code, notes, and snippets.

@froop
Created February 20, 2012 14:03
Show Gist options
  • Save froop/1869370 to your computer and use it in GitHub Desktop.
Save froop/1869370 to your computer and use it in GitHub Desktop.
[Java][WebDriver] ファイルダウンロードテスト例
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.apache.commons.io.FileUtils;
public class FileDownloadTest {
private static WebDriver driver;
private static final String DOWNLOAD_DIR = "C:\\work\\download";
private static final String COMPARE_DIR = "C:\\work\\compare";
@BeforeClass
public static void setUpBeforeClass() throws Exception {
FirefoxProfile prop = new FirefoxProfile();
prop.setPreference("browser.download.folderList", 2);
prop.setPreference("browser.download.useDownloadDir", true);
prop.setPreference("browser.download.dir", DOWNLOAD_DIR);
prop.setPreference("browser.helperApps.neverAsk.saveToDisk",
"application/octet-stream");
driver = new FirefoxDriver(prop);
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
driver.quit();
}
@Test
public void testFileDownload() throws Exception {
final String fileName = "example.zip";
File expected = new File(COMPARE_DIR + "/" + fileName);
File actual = new File(DOWNLOAD_DIR + "/" + fileName);
actual.delete();
driver.get("http://example.com/1/download");
assertTrue("exists", actual.exists());
assertTrue("content", FileUtils.contentEquals(expected, actual));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment