0

I have one selenium + java scenario which clicks a link in the webpage and that would download the file to the directory.

I have done few things which working fine in my local MAC though I need this only for this particular scenario. However it does not download the file in the jenkins though its clicks the link( i.e docker container linux). I read somewhere that we can start the xvfb programmatically through java and I can start xvfb for this particular scenario only and once scenario finished I can stop xvfb. https://github.com/mike10004/xvfb-manager-java/blob/master/README.md but not much info about how to do exactly. However I can do Xvfb "$DISPLAY" >>/dev/null 2>&1 & apt-get install xvfb through shell script but that would be like xvfb would be started for all scenarios which would be my last option.

  private static HashMap<String, Object> setDownloadPreferences() {
    HashMap<String, Object> prefs = new HashMap<>();
    prefs.put("download.default_directory", System.getProperty("user.dir") + File.separator +"target");
    prefs.put("safebrowsing.enabled", false);
    prefs.put("download.prompt_for_download", false);
    prefs.put("profile.default_content_settings.popups", false);
    prefs.put("credentials_enable_service", false);
    return prefs;
  }

@SneakyThrows
  private static void enableDriverSecurityForFileDownloadInHeadlessMode(ChromeDriverService driverService, WebDriver driver) {
    Map<String, Object> commandParams = new HashMap<>();
    commandParams.put("cmd", "Page.setDownloadBehavior");

    Map<String, String> params = new HashMap<>();
    params.put("behavior", "allow");
    params.put("downloadPath", System.getProperty("user.dir") + File.separator +"target");
    commandParams.put("params", params);

    ObjectMapper objectMapper = new ObjectMapper();
    String body = objectMapper.writeValueAsString(commandParams);

    SessionId session = ((ChromeDriver) driver).getSessionId();
    String url = driverService.getUrl().toString() + "/session/" + session + "/chromium/send_command";
    RestAssured.given().contentType("application/json").body(body).when().post(url);
  }
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("prefs", setDownloadPreferences());
WebDriver driver;
    
ChromeDriverService driverService = ChromeDriverService.createDefaultService();
driver = new ChromeDriver(driverService, chromeOptions);
enableDriverSecurityForFileDownloadInHeadlessMode(driverService, driver);
asinha
  • 337
  • 1
  • 6
  • 24
  • Please elaborate `it does not download the file in the jenkins`. Do you get any exception? – pburgr Mar 13 '22 at 20:44
  • it does click the link but nothing get downloaded.. There is no exception. So the assertion for the next step which checks if the file exist or not gets failed. Have you tried this one and does it work for you ? – asinha Mar 14 '22 at 09:33
  • any update on this please – asinha Mar 15 '22 at 10:52

0 Answers0