How do i change the download path dynamically during runtime using chrome in Selenium

痴心易碎 提交于 2020-07-10 10:26:23

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/62779586/how-do-i-change-the-download-path-dynamically-during-runtime-using-chrome-in-sel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!