Update webdriver upload files method

This commit is contained in:
Visagan Guruparan 2025-06-13 11:08:53 -05:00
parent ee5b21cae0
commit a581091248

View file

@ -24,7 +24,7 @@ public partial class PlaywrightWebDriver
} }
else if (count > 1) else if (count > 1)
{ {
if(!action.FirstIfMultipleFound) if (!action.FirstIfMultipleFound)
{ {
Serilog.Log.Error($"Multiple eElements were found: {result.Selector}"); Serilog.Log.Error($"Multiple eElements were found: {result.Selector}");
return; return;
@ -102,26 +102,33 @@ public partial class PlaywrightWebDriver
}); });
var guid = Guid.NewGuid().ToString(); var guid = Guid.NewGuid().ToString();
var directory = Path.Combine(Path.GetTempPath(), guid); var directory = Path.Combine(Path.GetTempPath(), guid);
if (Directory.Exists(directory)) DeleteDirectory(directory);
{
Directory.Delete(directory, true);
}
Directory.CreateDirectory(directory); Directory.CreateDirectory(directory);
var localPaths = new List<string>(); var localPaths = new List<string>();
using var httpClient = new HttpClient(); var http = _services.GetRequiredService<IHttpClientFactory>();
using var httpClient = http.CreateClient();
foreach (var fileUrl in files) foreach (var fileUrl in files)
{ {
var bytes = await httpClient.GetByteArrayAsync(fileUrl); try
var fileName = new Uri(fileUrl).AbsolutePath; {
var localPath = Path.Combine(directory, Path.GetFileName(fileName)); using var fileData = await httpClient.GetAsync(fileUrl);
await File.WriteAllBytesAsync(localPath, bytes); var fileName = new Uri(fileUrl).AbsolutePath;
await Task.Delay(2000); var localPath = Path.Combine(directory, Path.GetFileName(fileName));
localPaths.Add(localPath); await using var fs = new FileStream(localPath, FileMode.Create, FileAccess.Write, FileShare.None);
await fileData.Content.CopyToAsync(fs);
localPaths.Add(localPath);
}
catch (Exception ex)
{
Serilog.Log.Error($"FileUpload failed for {fileUrl}. Message: {ex.Message}");
}
} }
await fileChooser.SetFilesAsync(localPaths); await fileChooser.SetFilesAsync(localPaths);
await Task.Delay(1000 * action.WaitTime);
} }
else if (action.Action == BroswerActionEnum.Typing) else if (action.Action == BroswerActionEnum.Typing)
{ {
await locator.PressSequentiallyAsync(action.Content);
await locator.PressSequentiallyAsync(action.Content); await locator.PressSequentiallyAsync(action.Content);
if (action.PressKey != null) if (action.PressKey != null)
{ {
@ -155,8 +162,8 @@ public partial class PlaywrightWebDriver
// Perform drag-and-move // Perform drag-and-move
// Move mouse to the start position // Move mouse to the start position
var mouse = page.Mouse; var mouse = page.Mouse;
await mouse.MoveAsync(startX, startY); await mouse.MoveAsync(startX, startY);
await mouse.DownAsync(); await mouse.DownAsync();
// Move mouse smoothly in increments // Move mouse smoothly in increments
var tracks = GetVelocityTrack(offsetX); var tracks = GetVelocityTrack(offsetX);
@ -185,6 +192,13 @@ public partial class PlaywrightWebDriver
await Task.Delay(1000 * action.WaitTime); await Task.Delay(1000 * action.WaitTime);
} }
} }
private void DeleteDirectory(string directory)
{
if (Directory.Exists(directory))
{
Directory.Delete(directory, true);
}
}
public static List<int> GetVelocityTrack(float distance) public static List<int> GetVelocityTrack(float distance)
{ {
// Initialize the track list to store the movement distances // Initialize the track list to store the movement distances