Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add docs for download-paths #1262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
diemol merged 1 commit into SeleniumHQ:trunk from krmahadevan:docs_download_file_to_node
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ pull request updating this page.
| `--drain-after-session-count`| int | `1` | Drain and shutdown the Node after X sessions have been executed. Useful for environments like Kubernetes. A value higher than zero enables this feature. |
| `--hub`| string | `http://localhost:4444` | The address of the Hub in a Hub-and-Node configuration. Can be a hostname or IP address (`hostname`), in which case the Hub will be assumed to be `http://hostname:4444`, the `--grid-url` will be the same `--publish-events` will be `tcp://hostname:4442` and `--subscribe-events` will be `tcp://hostname:4443`. If `hostname` contains a port number, that will be used for `--grid-url` but the URIs for the event bus will remain the same. Any of these default values may be overridden but setting the correct flags. If the hostname has a protocol (such as `https`) that will be used too. |
| `--enable-cdp`| boolean | `true` | Enable CDP proxying in Grid. A Grid admin can disable CDP if the network doesnot allow websockets. True by default. |
| `--downloads-path`| string | `/usr/downloads` | The default location wherein all browser triggered file downloads would be available to be retrieved from. This is usually the directory that you configure in your browser as the default location for storing downloaded files. |

### Relay

Expand Down Expand Up @@ -349,3 +350,92 @@ driver.quit();
```

Set the custom capability to `false` in order to match the Node B.

#### Specifying path from where downloaded files can be retrieved

At times a test may need to access files that were downloaded by it on the Node. To retrieve such files, following can be done.

##### Start the Hub
```
java -jar selenium-server-<version>.jar hub
```

##### Start the Node with downloads path specified
```
java -jar selenium-server-<version>.jar node --downloads-path /usr/downloads
```

##### Sample that retrieves the downloaded file

```java
import static org.openqa.selenium.remote.http.Contents.string;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.io.Zip;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpMethod;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;

public class DownloadsSample {

public static void main(String[] args) throws InterruptedException, IOException {
File dirToCopyTo = new File("/usr/downloads");
URL gridUrl = new URL("http://localhost:4444");
RemoteWebDriver driver = new RemoteWebDriver(gridUrl, firefoxOptions());
driver.get("http://the-internet.herokuapp.com/download");
WebElement element = driver.findElement(By.cssSelector(".example a"));
element.click();

TimeUnit.SECONDS.sleep(10);

// The file can be downloaded by accessing
// curl -X GET "http://localhost:4444/session/<sessionId>/se/file?filename=my_file.pdf"

HttpRequest request = new HttpRequest(
HttpMethod.GET,
String.format("/session/%s/se/file", driver.getSessionId()));
request.addQueryParameter("filename", "my_appointments-1.pdf");
try (HttpClient client = HttpClient.Factory.createDefault().createClient(gridUrl)) {
HttpResponse response = client.execute(request);
Map<String, Object> map = new Json().toType(string(response), Json.MAP_TYPE);
// The returned map would contain 2 keys viz.,
// filename - This represents the name of the file (same as what was provided by the test)
// contents - Base64 encoded String which contains the zipped file.
String encodedContents = map.get("contents").toString();

//The file contents would always be a zip file and has to be unzipped.
Zip.unzip(encodedContents, dirToCopyTo);
} finally {
driver.quit();
}
}

private static FirefoxOptions firefoxOptions() {
FirefoxOptions options = new FirefoxOptions();
options.addPreference("browser.download.manager.showWhenStarting", false);
options.addPreference("browser.helperApps.neverAsk.saveToDisk",
"images/jpeg, application/pdf, application/octet-stream");
options.addPreference("pdfjs.disabled", true);
return options;
}
}
```

##### Points to remember:

* The endpoint to `GET` from is `/session/<sessionId>/se/file?filename=`
* The response contains two keys viz.,
* `filename` - Same as what was specified in the request.
* `contents` - Base64 encoded zipped contents of the file.
* The file contents are Base64 encoded.
* The contents need to be unzipped.
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ Grid の設定には、さまざまなセクションが用意されています
| `--drain-after-session-count` | int | `1` | X 個のセッションが実行された後に、ノードをドレインしてシャットダウンします。 Kubernetes のような環境で有用です。 0 より大きい値を指定すると、この機能が有効になります。 |
| `--hub` | string | `http://localhost:4444` | ハブ・ノード構成におけるハブのアドレスを指定します。ホスト名か IP アドレスが指定できます。この場合、ハブは `http://hostname:4444` とみなされ、 `--grid-url` は同じものになります。 `--publish-events` は `tcp://hostname:4442` 、`--subscribe-events` は `tcp://hostname:4443` となります。 `hostname` にポート番号が含まれている場合は、それが `--grid-url` に使用されますが、イベントバスの URI は変更されません。これらのデフォルト値は、適切なフラグを設定することでオーバーライドすることができます。ホスト名にプロトコル(`https`のような)が含まれる場合もそれが利用されます。 |
| `--enable-cdp` | boolean | `true` | Grid 内で CDP プロキシーを有効にします。もしネットワークが web socket を許可していない場合、Grid 管理者は CDP を無効にできます。デフォルトは true です。 |
| `--downloads-path`| string | `/usr/downloads` | The default location wherein all browser triggered file downloads would be available to be retrieved from. This is usually the directory that you configure in your browser as the default location for storing downloaded files. |

### Relay

Expand Down Expand Up @@ -349,3 +350,93 @@ driver.quit();
```

ノード B とマッチさせるにはカスタム capability を `false` に設定します。


#### Specifying path from where downloaded files can be retrieved

At times a test may need to access files that were downloaded by it on the Node. To retrieve such files, following can be done.

##### Start the Hub
```
java -jar selenium-server-<version>.jar hub
```

##### Start the Node with downloads path specified
```
java -jar selenium-server-<version>.jar node --downloads-path /usr/downloads
```

##### Sample that retrieves the downloaded file

```java
import static org.openqa.selenium.remote.http.Contents.string;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.io.Zip;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpMethod;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;

public class DownloadsSample {

public static void main(String[] args) throws InterruptedException, IOException {
File dirToCopyTo = new File("/usr/downloads");
URL gridUrl = new URL("http://localhost:4444");
RemoteWebDriver driver = new RemoteWebDriver(gridUrl, firefoxOptions());
driver.get("http://the-internet.herokuapp.com/download");
WebElement element = driver.findElement(By.cssSelector(".example a"));
element.click();

TimeUnit.SECONDS.sleep(10);

// The file can be downloaded by accessing
// curl -X GET "http://localhost:4444/session/<sessionId>/se/file?filename=my_file.pdf"

HttpRequest request = new HttpRequest(
HttpMethod.GET,
String.format("/session/%s/se/file", driver.getSessionId()));
request.addQueryParameter("filename", "my_appointments-1.pdf");
try (HttpClient client = HttpClient.Factory.createDefault().createClient(gridUrl)) {
HttpResponse response = client.execute(request);
Map<String, Object> map = new Json().toType(string(response), Json.MAP_TYPE);
// The returned map would contain 2 keys viz.,
// filename - This represents the name of the file (same as what was provided by the test)
// contents - Base64 encoded String which contains the zipped file.
String encodedContents = map.get("contents").toString();

//The file contents would always be a zip file and has to be unzipped.
Zip.unzip(encodedContents, dirToCopyTo);
} finally {
driver.quit();
}
}

private static FirefoxOptions firefoxOptions() {
FirefoxOptions options = new FirefoxOptions();
options.addPreference("browser.download.manager.showWhenStarting", false);
options.addPreference("browser.helperApps.neverAsk.saveToDisk",
"images/jpeg, application/pdf, application/octet-stream");
options.addPreference("pdfjs.disabled", true);
return options;
}
}
```

##### Points to remember:

* The endpoint to `GET` from is `/session/<sessionId>/se/file?filename=`
* The response contains two keys viz.,
* `filename` - Same as what was specified in the request.
* `contents` - Base64 encoded zipped contents of the file.
* The file contents are Base64 encoded.
* The contents need to be unzipped.
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ e esteja à vontade para nos enviar um pull request com alterações a esta pág
| `--drain-after-session-count`| int | `1` | Drain and shutdown the Node after X sessions have been executed. Useful for environments like Kubernetes. A value higher than zero enables this feature. |
| `--hub`| string | `http://localhost:4444` | The address of the Hub in a Hub-and-Node configuration. Can be a hostname or IP address (`hostname`), in which case the Hub will be assumed to be `http://hostname:4444`, the `--grid-url` will be the same `--publish-events` will be `tcp://hostname:4442` and `--subscribe-events` will be `tcp://hostname:4443`. If `hostname` contains a port number, that will be used for `--grid-url` but the URIs for the event bus will remain the same. Any of these default values may be overridden but setting the correct flags. If the hostname has a protocol (such as `https`) that will be used too. |
| `--enable-cdp`| boolean | `true` | Enable CDP proxying in Grid. A Grid admin can disable CDP if the network doesnot allow websockets. True by default. |
| `--downloads-path`| string | `/usr/downloads` | The default location wherein all browser triggered file downloads would be available to be retrieved from. This is usually the directory that you configure in your browser as the default location for storing downloaded files. |


### Relay

Expand Down Expand Up @@ -351,3 +353,93 @@ driver.quit();
```

Set the custom capability to `false` in order to match the Node B.


#### Specifying path from where downloaded files can be retrieved

At times a test may need to access files that were downloaded by it on the Node. To retrieve such files, following can be done.

##### Start the Hub
```
java -jar selenium-server-<version>.jar hub
```

##### Start the Node with downloads path specified
```
java -jar selenium-server-<version>.jar node --downloads-path /usr/downloads
```

##### Sample that retrieves the downloaded file

```java
import static org.openqa.selenium.remote.http.Contents.string;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.io.Zip;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpMethod;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;

public class DownloadsSample {

public static void main(String[] args) throws InterruptedException, IOException {
File dirToCopyTo = new File("/usr/downloads");
URL gridUrl = new URL("http://localhost:4444");
RemoteWebDriver driver = new RemoteWebDriver(gridUrl, firefoxOptions());
driver.get("http://the-internet.herokuapp.com/download");
WebElement element = driver.findElement(By.cssSelector(".example a"));
element.click();

TimeUnit.SECONDS.sleep(10);

// The file can be downloaded by accessing
// curl -X GET "http://localhost:4444/session/<sessionId>/se/file?filename=my_file.pdf"

HttpRequest request = new HttpRequest(
HttpMethod.GET,
String.format("/session/%s/se/file", driver.getSessionId()));
request.addQueryParameter("filename", "my_appointments-1.pdf");
try (HttpClient client = HttpClient.Factory.createDefault().createClient(gridUrl)) {
HttpResponse response = client.execute(request);
Map<String, Object> map = new Json().toType(string(response), Json.MAP_TYPE);
// The returned map would contain 2 keys viz.,
// filename - This represents the name of the file (same as what was provided by the test)
// contents - Base64 encoded String which contains the zipped file.
String encodedContents = map.get("contents").toString();

//The file contents would always be a zip file and has to be unzipped.
Zip.unzip(encodedContents, dirToCopyTo);
} finally {
driver.quit();
}
}

private static FirefoxOptions firefoxOptions() {
FirefoxOptions options = new FirefoxOptions();
options.addPreference("browser.download.manager.showWhenStarting", false);
options.addPreference("browser.helperApps.neverAsk.saveToDisk",
"images/jpeg, application/pdf, application/octet-stream");
options.addPreference("pdfjs.disabled", true);
return options;
}
}
```

##### Points to remember:

* The endpoint to `GET` from is `/session/<sessionId>/se/file?filename=`
* The response contains two keys viz.,
* `filename` - Same as what was specified in the request.
* `contents` - Base64 encoded zipped contents of the file.
* The file contents are Base64 encoded.
* The contents need to be unzipped.
Loading

AltStyle によって変換されたページ (->オリジナル) /