Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/java/src/org/openqa/selenium/HasDownloads.java
4004 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
package org.openqa.selenium;
19
20
import java.io.IOException;
21
import java.nio.file.Path;
22
import java.util.List;
23
24
/** Indicates that a driver supports downloading remote files. */
25
public interface HasDownloads {
26
27
/**
28
* Requires downloads to be enabled.
29
*
30
* <p>TODO: Create an example in the documentation and provide a link to it.
31
*
32
* @param capabilities the capabilities object
33
* @throws WebDriverException if capability to enable downloads is not set
34
*/
35
default void requireDownloadsEnabled(Capabilities capabilities) {
36
if (!isDownloadsEnabled(capabilities)) {
37
throw new WebDriverException(
38
"You must enable downloads in order to work with downloadable files.");
39
}
40
}
41
42
/**
43
* Checks if downloads are enabled
44
*
45
* @return true if this webdriver has capability "se:downloadsEnabled" = true
46
*/
47
boolean isDownloadsEnabled();
48
49
static boolean isDownloadsEnabled(Capabilities capabilities) {
50
return capabilities.is("se:downloadsEnabled");
51
}
52
53
/**
54
* Gets the downloadable files.
55
*
56
* @return a list of downloadable files for each key
57
* @deprecated Use method {@link #getDownloadedFiles()} instead
58
*/
59
@Deprecated
60
List<String> getDownloadableFiles();
61
62
/**
63
* Gets all files downloaded by browser.
64
*
65
* @return a list of files with their name, size and time.
66
*/
67
List<DownloadedFile> getDownloadedFiles();
68
69
/**
70
* Downloads a file to a given location.
71
*
72
* @param fileName the name of the file to be downloaded
73
* @param targetLocation the location where the file will be downloaded to
74
* @throws IOException if an I/O error occurs while downloading the file
75
*/
76
void downloadFile(String fileName, Path targetLocation) throws IOException;
77
78
/** Deletes the downloadable files. */
79
void deleteDownloadableFiles();
80
81
class DownloadedFile {
82
private final String name;
83
private final long creationTime;
84
private final long lastModifiedTime;
85
private final long size;
86
87
public DownloadedFile(String name, long creationTime, long lastModifiedTime, long size) {
88
this.name = name;
89
this.creationTime = creationTime;
90
this.lastModifiedTime = lastModifiedTime;
91
this.size = size;
92
}
93
94
public String getName() {
95
return name;
96
}
97
98
public boolean hasExtension(String extension) {
99
return extension.startsWith(".") ? name.endsWith(extension) : name.endsWith('.' + extension);
100
}
101
102
public long getCreationTime() {
103
return creationTime;
104
}
105
106
public long getLastModifiedTime() {
107
return lastModifiedTime;
108
}
109
110
public long getSize() {
111
return size;
112
}
113
}
114
}
115
116