serenity-bdd / serenity-cucumber

Cucumber integration for the Serenity BDD Reporting library
Other
78 stars 74 forks source link

Unable to download file in headless mode #210

Open Dinh263 opened 5 years ago

Dinh263 commented 5 years ago

Hi, I try to download file in headless mode but it does not work. This is what i config in the serenity.property file

chrome_preferences.download.default_directory = ${user.dir}/src/test/resources/download chrome_preferences.safebrowsing.disable_download_protection = True chrome_preferences.safebrowsing.enabled = False chrome_preferences.behavior = allow

Any one has experience in download headless mode using java. Pleas help. many thanks.

srivathsa29 commented 4 years ago

Hi,

Any solution found for this issue?

Dinh263 commented 4 years ago

Hello

Hi,

Any solution found for this issue?

Hi, The solution for above issue is:

  1. you have to create new web driver implements Datasource and you have override the newWebDriver(). in the body of this method, you will create new profile and option.... and return new webdriver.
  2. next step is you have to make Serenity using your customized Webdriver (on step 1) instead of using the default webdriver of Serenity by making changes in the Serenity.property file like below webdriver.driver = provided webdriver.provided.type = mydriver webdriver.provided.mydriver = --- you have to change this : it will be the full path of the class you create on step 1. thucydides.driver.capabilities = mydriver

All you can find the serenity doc : http://thucydides.info/docs/serenity/#_custom_webdriver_implementations

Screen Shot 2019-08-22 at 10 50 52 AM Screen Shot 2019-08-22 at 10 58 50 AM

For detail of step 1 : use the code like below:

ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<>();
//String downloadFilepath = System.getProperty("user.dir");
prefs.put("safebrowsing.enabled", false);
prefs.put("download.default_directory", System.getProperty("user.dir")); prefs.put("download.prompt_for_download", false);
prefs.put("download.directory_upgrade", true);
prefs.put("cmd", "Page.setDownloadBehavior"); prefs.put("profile.default_content_settings.popups", 0);
prefs.put("behavior", "allow"); options.setExperimentalOption("prefs", prefs); options.addArguments("--disable-notifications");
options.addArguments("--start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-gpu");
options.addArguments("--headless");
options.addArguments("window-size=1980,1080");
options.addArguments("--allow-running-insecure-content");
options.addArguments("--disable-extensions");
options.addArguments("--no-sandbox");
options.addArguments("--test-type");
options.addArguments("--disable-web-security");
//options.setBinary("C:\Program Files (x86)\Google\Chrome");
String chromedriverpath_1 = System.getProperty("user.dir");
String chromedriverpath = chromedriverpath_1+"//chromedriver_2_44.exe"; //System.out.println(chromedriverpath);
System.setProperty("webdriver.chrome.driver", chromedriverpath); //System.setProperty("user.dir","//chromedriver_2.44.exe");
ChromeDriverService driverService = ChromeDriverService.createDefaultService(); ChromeDriver driver = new ChromeDriver(driverService, options);
ObjectMapper objectMapper = new ObjectMapper();
HttpClient httpClient = HttpClientBuilder.create().build();
String command = null;
try {
command = objectMapper.writeValueAsString(prefs);
} catch (JsonProcessingException e) { e.printStackTrace(); }
String u = driverService.getUrl().toString() + "/session/" + driver.getSessionId() + "/chromium/send_command";
HttpPost request = new HttpPost(u);
request.addHeader("content-type", "application/json");
request.setEntity(new StringEntity(command));
httpClient.execute(request);
return driver; }