Path: blob/trunk/java/src/org/openqa/selenium/HasDownloads.java
4004 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617package org.openqa.selenium;1819import java.io.IOException;20import java.nio.file.Path;21import java.util.List;2223/** Indicates that a driver supports downloading remote files. */24public interface HasDownloads {2526/**27* Requires downloads to be enabled.28*29* <p>TODO: Create an example in the documentation and provide a link to it.30*31* @param capabilities the capabilities object32* @throws WebDriverException if capability to enable downloads is not set33*/34default void requireDownloadsEnabled(Capabilities capabilities) {35if (!isDownloadsEnabled(capabilities)) {36throw new WebDriverException(37"You must enable downloads in order to work with downloadable files.");38}39}4041/**42* Checks if downloads are enabled43*44* @return true if this webdriver has capability "se:downloadsEnabled" = true45*/46boolean isDownloadsEnabled();4748static boolean isDownloadsEnabled(Capabilities capabilities) {49return capabilities.is("se:downloadsEnabled");50}5152/**53* Gets the downloadable files.54*55* @return a list of downloadable files for each key56* @deprecated Use method {@link #getDownloadedFiles()} instead57*/58@Deprecated59List<String> getDownloadableFiles();6061/**62* Gets all files downloaded by browser.63*64* @return a list of files with their name, size and time.65*/66List<DownloadedFile> getDownloadedFiles();6768/**69* Downloads a file to a given location.70*71* @param fileName the name of the file to be downloaded72* @param targetLocation the location where the file will be downloaded to73* @throws IOException if an I/O error occurs while downloading the file74*/75void downloadFile(String fileName, Path targetLocation) throws IOException;7677/** Deletes the downloadable files. */78void deleteDownloadableFiles();7980class DownloadedFile {81private final String name;82private final long creationTime;83private final long lastModifiedTime;84private final long size;8586public DownloadedFile(String name, long creationTime, long lastModifiedTime, long size) {87this.name = name;88this.creationTime = creationTime;89this.lastModifiedTime = lastModifiedTime;90this.size = size;91}9293public String getName() {94return name;95}9697public boolean hasExtension(String extension) {98return extension.startsWith(".") ? name.endsWith(extension) : name.endsWith('.' + extension);99}100101public long getCreationTime() {102return creationTime;103}104105public long getLastModifiedTime() {106return lastModifiedTime;107}108109public long getSize() {110return size;111}112}113}114115116