|
19 | 19 | using System;
|
20 | 20 | using System.Collections.Generic;
|
21 | 21 | using System.Collections.ObjectModel;
|
| 22 | +using System.IO; |
| 23 | +using System.IO.Compression; |
| 24 | +using System.Linq; |
22 | 25 | using System.Threading.Tasks;
|
23 | 26 | using OpenQA.Selenium.DevTools;
|
24 |
| -using OpenQA.Selenium.Internal; |
25 | 27 |
|
26 | 28 | namespace OpenQA.Selenium.Remote
|
27 | 29 | {
|
@@ -58,7 +60,7 @@ namespace OpenQA.Selenium.Remote
|
58 | 60 | /// }
|
59 | 61 | /// </code>
|
60 | 62 | /// </example>
|
61 |
| - public class RemoteWebDriver : WebDriver, IDevTools |
| 63 | + public class RemoteWebDriver : WebDriver, IDevTools, IHasDownloads |
62 | 64 | {
|
63 | 65 | /// <summary>
|
64 | 66 | /// The name of the Selenium grid remote DevTools end point capability.
|
@@ -468,6 +470,73 @@ public DevToolsSession GetDevToolsSession(int protocolVersion)
|
468 | 470 | return this.devToolsSession;
|
469 | 471 | }
|
470 | 472 |
|
| 473 | + /// <summary> |
| 474 | + /// Retrieves the downloadable files as a map of file names and their corresponding URLs. |
| 475 | + /// </summary> |
| 476 | + /// <returns>A list containing file names as keys and URLs as values.</returns> |
| 477 | + public List<string> GetDownloadableFiles() |
| 478 | + { |
| 479 | + var enableDownloads = this.Capabilities.GetCapability(CapabilityType.EnableDownloads); |
| 480 | + if (enableDownloads == null || !(bool) enableDownloads) { |
| 481 | + throw new WebDriverException("You must enable downloads in order to work with downloadable files."); |
| 482 | + } |
| 483 | + |
| 484 | + Response commandResponse = this.Execute(DriverCommand.GetDownloadableFiles, null); |
| 485 | + Dictionary<string, object> value = (Dictionary<string, object>)commandResponse.Value; |
| 486 | + object[] namesArray = (object[])value["names"]; |
| 487 | + return namesArray.Select(obj => obj.ToString()).ToList(); |
| 488 | + } |
| 489 | + |
| 490 | + /// <summary> |
| 491 | + /// Downloads a file with the specified file name. |
| 492 | + /// </summary> |
| 493 | + /// <param name="fileName">the name of the file to be downloaded</param> |
| 494 | + public void DownloadFile(string fileName, string targetDirectory) |
| 495 | + { |
| 496 | + var enableDownloads = this.Capabilities.GetCapability(CapabilityType.EnableDownloads); |
| 497 | + if (enableDownloads == null || !(bool) enableDownloads) { |
| 498 | + throw new WebDriverException("You must enable downloads in order to work with downloadable files."); |
| 499 | + } |
| 500 | + |
| 501 | + Dictionary<string, object> parameters = new Dictionary<string, object> |
| 502 | + { |
| 503 | + { "name", fileName } |
| 504 | + }; |
| 505 | + |
| 506 | + Response commandResponse = this.Execute(DriverCommand.DownloadFile, parameters); |
| 507 | + string contents = ((Dictionary<string, object>)commandResponse.Value)["contents"].ToString(); |
| 508 | + byte[] fileData = Convert.FromBase64String(contents); |
| 509 | + |
| 510 | + Directory.CreateDirectory(targetDirectory); |
| 511 | + string tempFile = Path.Combine(targetDirectory, "temp.zip"); |
| 512 | + File.WriteAllBytes(tempFile, fileData); |
| 513 | + |
| 514 | + using (ZipArchive archive = ZipFile.OpenRead(tempFile)) |
| 515 | + { |
| 516 | + foreach (ZipArchiveEntry entry in archive.Entries) |
| 517 | + { |
| 518 | + string destinationPath = Path.Combine(targetDirectory, entry.FullName); |
| 519 | + |
| 520 | + entry.ExtractToFile(destinationPath); |
| 521 | + } |
| 522 | + } |
| 523 | + |
| 524 | + File.Delete(tempFile); |
| 525 | + } |
| 526 | + |
| 527 | + /// <summary> |
| 528 | + /// Deletes all downloadable files. |
| 529 | + /// </summary> |
| 530 | + public void DeleteDownloadableFiles() |
| 531 | + { |
| 532 | + var enableDownloads = this.Capabilities.GetCapability(CapabilityType.EnableDownloads); |
| 533 | + if (enableDownloads == null || !(bool) enableDownloads) { |
| 534 | + throw new WebDriverException("You must enable downloads in order to work with downloadable files."); |
| 535 | + } |
| 536 | + |
| 537 | + this.Execute(DriverCommand.DeleteDownloadableFiles, null); |
| 538 | + } |
| 539 | + |
471 | 540 | /// <summary>
|
472 | 541 | /// Closes a DevTools session.
|
473 | 542 | /// </summary>
|
|
0 commit comments