1

I have 20 pages and each page have 2 testcases and each testcase download a number of files. I want to change the download directory for each test case at runtime.

Here is the 'TestBaseClass' code which downloading all the files in one particular folder from where I have to separate them as per category and place them into a particular folder. There are 20 folders and each folder is having 2 subfolders 'ChapterLevel' & 'PracticeLevel' in which I do have to place it manually.

Is it possible to change the download directory by passing a variable during runtime?

My TestBaseClass code:

public static WebDriver driver;

public static void initialization() throws InvocationTargetException {
    try {

        // 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\\AHNPTTest");

        // Adding cpabilities to ChromeOptions
        ChromeOptions options = new ChromeOptions();
        options.setExperimentalOption("prefs", prefs);

        // Launching browser with desired capabilities
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver(options);

    } 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);
}

Here is my testcase:

public class ANA_TC16_RiskAnalysisNewTest extends TestBaseClass {
    ANA_RiskAnalysisNewPage New;

    @BeforeMethod
    public void setUp() {
        try {
            initialization();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        login();
        New = new ANA_RiskAnalysisNewPage();
    }

    @Test
    public void chapterrLevelTest() throws Exception {
        New.hoverTest();
        New.clickBottomOptions();
        New.chapterOption();
        New.TopX();
        New.ATISlider();
        New.conditionSelection();
        New.takeScreenshot("Risk Analysis New Chapter Level Image");
        New.downloadOptions();
        New.isFileDownloaded();
    }

    @Test
    public void practiceLevelTest() throws Exception {
        New.hoverTest();
        New.clickBottomOptions();
        New.providerOption();
        New.TopX();
        New.ATISlider();
        New.conditionSelection();
        New.takeScreenshot("Risk Analysis New Practice Level Image");
        New.downloadOptions();
        New.isFileDownloaded();
    }
}
samabcde
  • 6,988
  • 2
  • 25
  • 41
dh.purvi
  • 47
  • 1
  • 6
  • You may change the download folder in each test case by adding parameter `initialization(String downloadPath)`, and then call `prefs.put("download.default_directory", downloadPath)`, To solve the sub folder problem, you may move the downloaded files to sub folder using `File.renameTo`. – samabcde Jul 08 '20 at 05:22
  • @samabcde, will you please elaborate more about subfolder, how do i implement in my scenario? – dh.purvi Jul 08 '20 at 14:42

1 Answers1

0

Suppose you want to specify download folder for each test method.

  1. Add parameter for downloadPath in initialization in TestBaseClass.
  2. Add parameter for downloadPath in setup in ANA_TC16_RiskAnalysisNewTest, remove the @BerforMethod annotation and update each test method to call setup in begin with desired downloadPath.
public class TestBaseClass {
    public static void initialization(String downloadPath) throws InvocationTargetException {
        try {
                 
               // 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", downloadPath);
        ...
public class ANA_TC16_RiskAnalysisNewTest extends TestBaseClass {
    ANA_RiskAnalysisNewPage New;
    
    // @BeforeMethod
    public void setUp(String downloadPath) {
        try {
            initialization(downloadPath);
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        login();
        New = new ANA_RiskAnalysisNewPage();
    }

    @Test
    public void chapterrLevelTest() throws Exception {
        setUp("C:\\Users\\pd\\Desktop\\AHNPTTest\\ANA_TC16_RiskAnalysis\\ChapterLevel");
        New.hoverTest();
        ...
    }

    @Test
    public void practiceLevelTest() throws Exception {
        setUp("C:\\Users\\pd\\Desktop\\AHNPTTest\\ANA_TC16_RiskAnalysis\\PracticeLevel");
        New.hoverTest();
        ...
    }
    ...
samabcde
  • 6,988
  • 2
  • 25
  • 41
  • Tried as you suggested but giving an error " org.testng.TestNGException: Method setUp requires 1 parameters but 0 were supplied in the @Configuration annotation. at org.testng.internal.Parameters.checkParameterTypes(Parameters.java:191) at org.testng.internal.Parameters.createParameters(Parameters.java:127) at org.testng.internal.Parameters.createParameters(Parameters.java:376) at org.testng.internal.Parameters.createConfigurationParameters(Parameters.java:84) – dh.purvi Jul 08 '20 at 14:53
  • Sorry i didn't remove the @BeforeMethod, I let you know..... – dh.purvi Jul 08 '20 at 14:59
  • Is there any way, i can overwrite these files, as everyday i have to execute all these scripts. – dh.purvi Jul 08 '20 at 17:16
  • Maybe [delete the files in downloadPath](https://stackoverflow.com/questions/13195797/delete-all-files-in-directory-but-not-directory-one-liner-solution) in `initialization` method? – samabcde Jul 09 '20 at 01:44
  • Sorry not getting an idea, how to implement it in my scenario. – dh.purvi Jul 09 '20 at 15:48
  • In begin of `initialization` method, delete all previous downloaded files in `downloadPath`, try to follow the link provided. Then the folder will be empty, and you don't need to overwrite the files. – samabcde Jul 09 '20 at 15:52
  • I added the line setUp(FileUtils.cleanDirectory("Chapter Level")); setUp("C:\\Users\\pd\\Downloads\\8thJuly2020\\Quality Measure\\Chapter Level"); but giving red underline 'cleanDirectory ' and when hover it, it saying 'change to 'checkDirecotry(...)' or 'change to clearDirectoryOnExit(...);. – dh.purvi Jul 09 '20 at 21:11
  • And If i placed in setUp() method before initialization(downloadPath) , FileUtils.cleanDirectory(directory); then also on hovering giving 2 options. – dh.purvi Jul 09 '20 at 21:16
  • Try `cleanDirectory(new File("Chapter Level"))`. You need to check the method signature, it is accepting `File` instead of `String` – samabcde Jul 10 '20 at 01:32
  • Will try soon and let you know, Thanks. – dh.purvi Jul 13 '20 at 18:43