Skip to content

Commit

Permalink
adds file APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
bmarroquin committed Sep 26, 2024
1 parent 8298ee8 commit faac6d7
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 0 deletions.
97 changes: 97 additions & 0 deletions src/FlaUI.WebDriver.UITests/DeviceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using FlaUI.WebDriver.UITests.TestUtil;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Remote;
using System.IO;
using System.IO.Compression;
using System.Text;

namespace FlaUI.WebDriver.UITests
{
[TestFixture]
public class DeviceTests
{
[Test]
public void PushPullFile()
{
var driverOptions = FlaUIAppiumDriverOptions.RootApp();
using var driver = new WindowsDriver(WebDriverFixture.WebDriverUrl, driverOptions);

var file = Path.GetTempFileName();
var data_to_write = "hello world";

// file was upload to temp directory
driver.PushFile(file, data_to_write);
Assert.That(File.ReadAllText(file) == data_to_write);

// pull the file back
var pulled_data = Encoding.UTF8.GetString(driver.PullFile(file));
Assert.That(pulled_data == data_to_write);
File.Delete(file);
}

[Test]
public void PullFolder()
{
var driverOptions = FlaUIAppiumDriverOptions.RootApp();
using var driver = new WindowsDriver(WebDriverFixture.WebDriverUrl, driverOptions);

var dir = Path.Join(Path.GetTempPath(), "FlaUiWDPullDir");
for (int i = 0; i < 10; i++)
{
driver.PushFile(Path.Join(dir, i.ToString()), $"{i} data");
}
var zip_bytes = driver.PullFolder(dir);
using var memoryStream = new MemoryStream(zip_bytes);
using var zip = new ZipArchive(memoryStream);

for (int i = 0; i < 10; i++)
{
var entry = zip.GetEntry(i.ToString());
using var entry_stream = new StreamReader(entry.Open());
Assert.That(entry_stream.ReadToEnd() == $"{i} data");
}
Directory.Delete(dir, true);
}

[Test]
public void PushFile_PathEmpty()
{
var driverOptions = FlaUIAppiumDriverOptions.RootApp();
using var driver = new WindowsDriver(WebDriverFixture.WebDriverUrl, driverOptions);

Assert.That(
() => driver.PushFile("", "hello world"),
Throws.TypeOf<WebDriverException>().With.Message.EqualTo("Parameter path must be provided in the request.")
);

}
[Test]
public void PullFile_PathEmpty()
{
var driverOptions = FlaUIAppiumDriverOptions.RootApp();
using var driver = new WindowsDriver(WebDriverFixture.WebDriverUrl, driverOptions);

Assert.That(
() => driver.PullFile(""),
Throws.TypeOf<WebDriverException>().With.Message.EqualTo("Parameter path must be provided in the request.")
);

}
[Test]
public void PullFolder_PathEmpty()
{
var driverOptions = FlaUIAppiumDriverOptions.RootApp();
using var driver = new WindowsDriver(WebDriverFixture.WebDriverUrl, driverOptions);

Assert.That(
() => driver.PullFolder(""),
Throws.TypeOf<WebDriverException>().With.Message.EqualTo("Parameter path must be provided in the request.")
);

}

}
}
82 changes: 82 additions & 0 deletions src/FlaUI.WebDriver/Controllers/DeviceController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using FlaUI.WebDriver.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Diagnostics;
using System.Buffers.Text;
using System.IO;
using System.IO.Compression;
using System.Security.Cryptography;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace FlaUI.WebDriver.Controllers
{

[Route("session/{sessionId}/appium/device")]
[ApiController]
public class DeviceController : ControllerBase
{


private static ActionResult MissingParameter(string parameterName)
{
return WebDriverResult.NotFound(new ErrorResponse()
{
ErrorCode = "Missing JSON Parameter",
Message = $"Parameter {parameterName} must be provided in the request."
});
}

[HttpPost("push_file")]
public async Task<ActionResult> PushFile([FromBody] PushFileRequest request)
{
if (request.Data == null)
{
// data could be an empty string, so just a simple null check
return MissingParameter("data");
}
if (string.IsNullOrEmpty(request.Path))
{
return MissingParameter("path");
}
var parent = Path.GetDirectoryName(request.Path)!;
if (!Directory.Exists(parent))
{
Directory.CreateDirectory(parent);
}
var data = Convert.FromBase64String(request.Data);
await System.IO.File.WriteAllBytesAsync(request.Path, data);
return WebDriverResult.Success();
}


[HttpPost("pull_file")]
public async Task<ActionResult> PullFile([FromBody] PullFileRequest request)
{
if (string.IsNullOrEmpty(request.Path))
{
return MissingParameter("path");
}
var data = await System.IO.File.ReadAllBytesAsync(request.Path);
return WebDriverResult.Success(Convert.ToBase64String(data));
}

[HttpPost("pull_folder")]
public ActionResult PullFolder([FromBody] PullFileRequest request)
{
if (string.IsNullOrEmpty(request.Path))
{
return MissingParameter("path");
}
byte[] bytes;
using (var ms = new MemoryStream())
{
ZipFile.CreateFromDirectory(request.Path, ms);
ms.Seek(0, SeekOrigin.Begin);
bytes = ms.ToArray();
}

return WebDriverResult.Success(Convert.ToBase64String(bytes));
}


}
}
7 changes: 7 additions & 0 deletions src/FlaUI.WebDriver/Models/PullFileRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace FlaUI.WebDriver.Models
{
public class PullFileRequest
{
public string? Path { get; set; }
}
}
8 changes: 8 additions & 0 deletions src/FlaUI.WebDriver/Models/PushFileRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace FlaUI.WebDriver.Models
{
public class PushFileRequest
{
public string? Path { get; set; }
public string? Data { get; set; }
}
}

0 comments on commit faac6d7

Please sign in to comment.