In the below code we are trying to capture the information when the chrome opens the file, but it is failing in some cases as some log files are getting downloaded and it's not viewable in chrome.
Let me know if any preference has to be added so that all files (irrespective of size) can be either viewed or downloaded.
public class DownloadFile {
String FILE_ADD = "file:///";
String LOG_PATH = "path";
String LOG_FILE = "sample.log";
String OUTPUT_FILE = "output.txt";
public static RemoteWebDriver driver;
@BeforeEach
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver","chromedriver.exe");
ChromeOptions options = new ChromeOptions();
//Map<String, Object> prefs = new HashMap<String, Object>();
//prefs.put("download.prompt_for_download", false);
//prefs.put("log.disabled", true);
//options.setExperimentalOption("prefs", prefs);
//driver = new ChromeDriver(options);
//driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
//driver.manage().window().maximize();
String fileDownloadPath = "downnloadpath";
Map<String, Object> prefsMap = new HashMap<String, Object>();
prefsMap.put("profile.default_content_settings.popups", 0);
prefsMap.put("download.default_directory", fileDownloadPath);
prefsMap.put("txtjs.disabled", true);
ChromeOptions option = new ChromeOptions();
option.setExperimentalOption("prefs", prefsMap);
option.addArguments("--test-type");
option.addArguments("--disable-extensions");
//Any preference to be added to download all the files instead of viewing some files in chrome
driver = new ChromeDriver(option);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
@Test
public void fileDownload() throws AWTException, InterruptedException, IOException {
driver.get(FILE_ADD+LOG_PATH+LOG_FILE);
//String output = driver.findElement(By.xpath("/html/body/pre")).getText();
//String final_output = output.replaceAll("User.*>", "testing");
//FileUtils.writeFile(LOG_PATH+OUTPUT_FILE, output);
Thread.sleep(7000);
}
@AfterEach
public void tearDown() throws Exception {
driver.quit();
}
}