0

[https://i.stack.imgur.com/Pl3Vq.png]My Scenario where 20 pages(Modules) are there. Opening each page, selecting some values from dropdown, lasso selection etc. and at last i have to click on 'Download' option, which download the image, pdf, powerpoint, crosstab and data file, Please see the image.[https://i.stack.imgur.com/wLrhB.png] which is running successfully but my issue is its downloading at default location and from there i am putting on a specific location manually for every page. I want it to download at specific location. I tried with following code which is just java class and downloading a file at specific location.

public static void main(String[] args) throws InterruptedException {
        
        // Setting chrome driver path
        System.setProperty("webdriver.chrome.driver","C:\\Users\\pdholakia\\Downloads\\Provider\\chromedriver_win32\\chromedriver.exe");
     
         // Setting new download directory path
         Map<String, Object> prefs = new HashMap<String, Object>();
             
         // Use File.separator as it will work on any OS
          prefs.put("download.default_directory", "C:\\Users\\pd\\Desktop\\AHNPTest");
                 
             
            // Adding cpabilities to ChromeOptions
            ChromeOptions options = new ChromeOptions();
            options.setExperimentalOption("prefs", prefs);
             
                        
            // Launching browser with desired capabilities
            ChromeDriver driver= new ChromeDriver(options);
             
            // URL loading
            driver.get("https://www.apache.org/dyn/closer.lua/poi/release/bin/poi-bin-4.1.2-20200217.zip");
             
            // Click on download selenium server jar file
            Thread.sleep(3000);
            WebElement download = driver.findElement(By.xpath("//p[2]//a[1]//strong[1]"));
            Actions act = new Actions(driver);
            act.moveToElement(download).click().build().perform();
             
     
        }
    }

But i am working with testNG framework using POM pattern. Following is my TestBaseClass which is working fine. What changes need to be done in following TestBaseClass to download the file at specific location.

public class TestBaseClass {
    public static WebDriver driver;
    public static void initialization() throws InvocationTargetException {
    try {
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
        
            
        }catch (Exception e) {
            // generic exception handling
            e.printStackTrace();
            }

        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);
                
    }
    public void login() {
        driver.get("https://ahnpttest.com/Account/Login");
        
        driver.findElement(By.id("EmailAddress")).sendKeys("abc.dh@npalvinre.com");
        driver.findElement(By.id("Password")).sendKeys("xyz@123");
        driver.findElement(By.id("LoginButton")).click();
    }
    
}   

I replaced these 2 lines "WebDriverManager.chromedriver().setup(); driver = new ChromeDriver() of Try block of TestBaseClass by entire Download code but giving an error Null Pointer Exception.

Any help will be appreciated.

dh.purvi
  • 47
  • 1
  • 6

1 Answers1

0

You are on the right path. you just have to pass the options in baseclass driver instance creation line.

// Setting new download directory path
         Map<String, Object> prefs = new HashMap<String, Object>();
             
         // Use File.separator as it will work on any OS
          prefs.put("download.default_directory", "C:\\Users\\pd\\Desktop\\AHNPTest");
                 
             
            // Adding cpabilities to ChromeOptions
            ChromeOptions options = new ChromeOptions();
            options.setExperimentalOption("prefs", prefs);
             
                        
            // Launching browser with desired capabilities
            WebDriverManager.chromedriver().setup();
            driver = new ChromeDriver(options); //<====== This is the line
supputuri
  • 13,644
  • 2
  • 21
  • 39
  • i tried as you suggested but giving same error "Null Pointer Exception". Error is "java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html at com.google.common.base.Preconditions.checkState(Preconditions.java:847)" . So removed WebDriverManger line and used System.setProprtey but still same error. Please see the image. – dh.purvi Jul 06 '20 at 20:14
  • Thanks a lot, it worked for me. It has downloaded in AHNPTest folder. In this AHNPTest folder i do have a 20 folders and each folder having 2 subfolders. So i need to change the path everytime in base class (prefs.put("download.default_directory", "C:\\Users\\pdholakia\\Desktop\\AHNTest"); ) there is no solution for this problem, right! – dh.purvi Jul 06 '20 at 22:28
  • You can pass the folder path as part of your mvn command rather hard coding, so that you can decide the folder path when running the test each time. Hope I got your question correctly. – supputuri Jul 06 '20 at 22:44
  • I didn't get, you mean through maven command? But how as here is my Testcase ...@Test public void chapterrLevelTest() throws Exception { New.hoverTest(); New.clickBottomOptions(); New.chapterOption(); New.TopX(); New.ATISlider(); New.conditionSelection(); New.patientsAtRiskForSelection(); New.frailtyTypeSelection(); New.takeScreenshot("Risk Analysis New Chapter Level Image"); New.downloadOptions(); New.isFileDownloaded(); } I do have like 2 testcase for each page. Can i pass the folder path in public void chapterrLevelTest() in this line? – dh.purvi Jul 06 '20 at 22:53
  • Not sure about the folder structure. Let's say your `chapterrLevelTest` is your test case and you can get the current test method name and send it as part of the driver initialization or baseclass, and append it to the path that we mentioned in original answer. By that way, all files downloaded in that test will go into a particular folder related to the test case. – supputuri Jul 06 '20 at 22:57
  • You mean, public static void initialization(........) throws InvocationTargetException { } here i pass page name or testcase name? Also can i pass path in prefs.put("download.default_directory", "C:\\Users\\pdholakia\\Desktop\\AHNTest"); ? – dh.purvi Jul 06 '20 at 23:07
  • Can you please post another question with the details, so that we can be in the same page. – supputuri Jul 07 '20 at 02:52
  • I have posted as new question with details. https://stackoverflow.com/questions/62779586/how-do-i-change-the-download-path-dynamically-during-runtime-using-chrome-in-sel – dh.purvi Jul 07 '20 at 16:16