自動的に指定したフォルダにファイルをダウンロードするように設定する。
PhantomJS ではできそうにないので Firefox Driver で。
// プロファイルを設定してドライバを作る
private def createDriver(outputDirPath: String): FirefoxDriver = {
val profile = new FirefoxProfile()
profile.setPreference("browser.download.folderList", 2)
profile.setPreference("browser.download.dir", outputDirPath)
profile.setPreference("browser.download.useDownloadDir", true)
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream")
new FirefoxDriver(profile)
}
// ダウンロード待ち(waitSecondsごとにダウンロードが完了したかチェック)
private def waitDownloading(waitSeconds: Int, outputDirPath: String, downloadFileName: String): Unit = {
val downloadFilePath = Paths.get(outputDirPath, downloadFileName)
while (!downloadFilePath.toFile.exists || Files.size(downloadFilePath) == 0) {
(1 to waitSeconds).foreach { _ =>
Thread.sleep(1 * 1000)
print(".")
}
println()
}
}