Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
Added code snippets about downloading files (fixes #743)
Browse files Browse the repository at this point in the history
  • Loading branch information
vania-pooh committed Oct 26, 2019
1 parent feea91e commit f469b8e
Showing 1 changed file with 54 additions and 3 deletions.
57 changes: 54 additions & 3 deletions docs/file-download.adoc
Original file line number Diff line number Diff line change
@@ -1,13 +1,64 @@
== Accessing Files Downloaded with Browser
== Downloading Files From Browser

=== Downloading Files in Different Browsers

==== Chrome

.Downloading files with Java
[source,java]
----
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("prefs", new HashMap<String, Object>(){
{
put("profile.default_content_settings.popups", 0);
put("download.default_directory", "/home/selenium/Downloads");
put("download.prompt_for_download", false);
put("download.directory_upgrade", true);
put("safebrowsing.enabled", false);
put("plugins.always_open_pdf_externally", true);
put("plugins.plugins_disabled", new ArrayList<String>(){
{
add("Chrome PDF Viewer");
}
});
}
});
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), chromeOptions);
driver.navigate().to("http://example.com/myfile.odt");
----

==== Firefox

.Downloading files with Java
[source,java]
----
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setCapability("moz:firefoxOptions", new HashMap<String, Object>(){
{
put("prefs", new HashMap<String, Object>(){
{
put("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream");
}
});
}
});
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), firefoxOptions);
driver.navigate().to("http://example.com/myfile.odt");
----

=== Accessing Files Downloaded with Browser

[NOTE]
====
. This feature only works when browsers are run in containers.
. This feature only works when browsers are being run in containers.
. Files are accessible only when browser session is running.
====

Your tests may need to download files with browsers. To analyze these files a common requirement is then to somehow extract downloaded files from browser containers. A possible solution can be dealing with container volumes. But Selenoid provides a `/download` API and dramatically simplifies downloading files. Having a running session `f2bcd32b-d932-4cdc-a639-687ab8e4f840` you can access all downloaded files using an URL:
```
http://selenoid-host.example.com:4444/download/f2bcd32b-d932-4cdc-a639-687ab8e4f840/myfile.txt
```
In order for this feature to work an HTTP file server should be listening inside browser container on port `8080`. Download directory inside container to be used in tests is usually `~/Downloads`.
In order for this feature to work an HTTP file server should be listening inside browser container on port `8080`. Download directory inside container to be used in tests is usually `~/Downloads`.

0 comments on commit f469b8e

Please sign in to comment.