This repository has been archived by the owner on Dec 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added code snippets about downloading files (fixes #743)
- Loading branch information
1 parent
feea91e
commit f469b8e
Showing
1 changed file
with
54 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |