0

I have to download generated pdf file to custom directory, but the browser does not download file to the specific path. Below my generic method:

    public void downloadFile() throws InterruptedException, IOException {
        String path = "./FILES";
        folder = new File(path);
        folder.mkdir();
        Map<String, Object> prefs = new HashMap<String, Object>();
        prefs.put("download.default_directory",
                System.getProperty("user.dir") + File.separator + "externalFiles" + File.separator + "downloadFiles");
        ChromeOptions options = new ChromeOptions();
        options.setExperimentalOption("prefs", prefs);
        options.addArguments("start-maximized");
        System.setProperty("webdriver.chrome.driver", folder.getAbsolutePath());
        driver = new ChromeDriver(options);
        Thread.sleep(3000);
        driver.findElement(By.xpath("//button[@title='Generate PDF Report']")).click();
        
    }
tifoso
  • 58
  • 6
  • Exact duplicate: [How to set default download directory in selenium Chrome Capabilities?](https://stackoverflow.com/questions/34515328/how-to-set-default-download-directory-in-selenium-chrome-capabilities) – Randy Casburn Dec 21 '20 at 18:13
  • It is not an duplicate, since it does not have an answer to my question. – tifoso Dec 21 '20 at 18:16
  • It is word-for-word - so if it doesn't answer _your_ question, you need to clarify your question to state _exactly_ what you need to do, what isn't working, and what errors you are experiencing. – Randy Casburn Dec 21 '20 at 18:23
  • Unfortunately I am not able to see the error, but the problem is that Hello.pdf file downloads into my /home/tifoso/Downloads directory and not the desired directory as .//FILES// – tifoso Dec 21 '20 at 18:27
  • 1
    You've created an instance of `ChromeOptions` but didn't use it to initialize the `WebDriver` – Fenio Dec 21 '20 at 18:44
  • if I do so, the new browser will open (new blank window), and the script will fail. I must click to generate the .pdf file on existing browser. There are pre-steps with login in to the application, clicking different buttons to set the values before i generate the .pdf – tifoso Dec 21 '20 at 18:46
  • you need an absolute path... and you need to set your chromeoptions when intializing the driver. – pcalkins Dec 21 '20 at 20:32
  • When passing the absolute path it is not downloading file to desired location, it is still downloading file to default (/home/tifoso/Downloads). when adding a line driver = new ChromeDriver(options); it opens a new browser page and script is failing, the new page is blank, it has 455 test cases before it comes to download generated pdf – tifoso Dec 21 '20 at 20:37
  • @tifoso It will not work in any way until you pass `ChromeOptions` to instance of `WebDriver`. Set it before you initialize WebDriver. It won't work any other way. – Fenio Dec 21 '20 at 21:41
  • I did some modifications for the code, but it is still not downloading the file to eclipse project. And also opens new browser and then clicking on download pdf, which downloads to generic location – tifoso Dec 21 '20 at 21:48
  • I realized my own mistake, i was trying to create a generic method to call it in my framework. But this is not correct, it has to be done within @BeforeTest to initialize the browser and the URL, etc. It worked fine for me it created the folder and downloaded the file in the correct location. (This information for someone like me... I was doing it for the first time!!!) Thank you to all who was trying to help! – tifoso Dec 22 '20 at 00:34

1 Answers1

0

I found my own mistake. If you are fresher do not try to use it as a generic method. This will be your setup method.

This is how it is supposed to be:

@BeforeTest
public void setup() {
    basePage = new BasePage(); 
    prop = basePage.init_properties();
    
    Map<String, Object> prefs = new HashMap<String, Object>();
    prefs.put("download.default_directory",System.getProperty("user.dir") + File.separator + "externalFiles" + File.separator + "downloadFiles");   
    ChromeOptions options = new ChromeOptions();
    options.setExperimentalOption("prefs", prefs);
    WebDriverManager.chromedriver().setup();

    driver = new ChromeDriver(options);
    driver.get("https://your.url.com");
    CS = new Best_Services(driver);
}

@Test(priority = 20)
public void verify_ClickOnUpdateSMITH_test() throws InterruptedException, IOException {
    CS.clickOnPDF();            
}

@Test(priority = 22)
public void verify_readPDF_SearchByCriteriaSMITH_test() throws IOException, InterruptedException  {
    CS.readPDF_SearchByCriteria();       
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
tifoso
  • 58
  • 6