Skip to content

Commit 2e2f389

Browse files
authored
Merge pull request #1077 from visagang/features/vguruparan
Update webdriver upload files method
2 parents ee5b21c + 351d65b commit 2e2f389

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.DoAction.cs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public async Task DoAction(MessageInfo message, ElementActionArgs action, Browse
2424
}
2525
else if (count > 1)
2626
{
27-
if(!action.FirstIfMultipleFound)
27+
if (!action.FirstIfMultipleFound)
2828
{
2929
Serilog.Log.Error($"Multiple eElements were found: {result.Selector}");
3030
return;
@@ -102,23 +102,29 @@ await locator.ClickAsync(new LocatorClickOptions
102102
});
103103
var guid = Guid.NewGuid().ToString();
104104
var directory = Path.Combine(Path.GetTempPath(), guid);
105-
if (Directory.Exists(directory))
106-
{
107-
Directory.Delete(directory, true);
108-
}
105+
DeleteDirectory(directory);
109106
Directory.CreateDirectory(directory);
110107
var localPaths = new List<string>();
111-
using var httpClient = new HttpClient();
108+
var http = _services.GetRequiredService<IHttpClientFactory>();
109+
using var httpClient = http.CreateClient();
112110
foreach (var fileUrl in files)
113111
{
114-
var bytes = await httpClient.GetByteArrayAsync(fileUrl);
115-
var fileName = new Uri(fileUrl).AbsolutePath;
116-
var localPath = Path.Combine(directory, Path.GetFileName(fileName));
117-
await File.WriteAllBytesAsync(localPath, bytes);
118-
await Task.Delay(2000);
119-
localPaths.Add(localPath);
112+
try
113+
{
114+
using var fileData = await httpClient.GetAsync(fileUrl);
115+
var fileName = new Uri(fileUrl).AbsolutePath;
116+
var localPath = Path.Combine(directory, Path.GetFileName(fileName));
117+
await using var fs = new FileStream(localPath, FileMode.Create, FileAccess.Write, FileShare.None);
118+
await fileData.Content.CopyToAsync(fs);
119+
localPaths.Add(localPath);
120+
}
121+
catch (Exception ex)
122+
{
123+
Serilog.Log.Error($"FileUpload failed for {fileUrl}. Message: {ex.Message}");
124+
}
120125
}
121126
await fileChooser.SetFilesAsync(localPaths);
127+
await Task.Delay(1000 * action.WaitTime);
122128
}
123129
else if (action.Action == BroswerActionEnum.Typing)
124130
{
@@ -155,8 +161,8 @@ await locator.ClickAsync(new LocatorClickOptions
155161
// Perform drag-and-move
156162
// Move mouse to the start position
157163
var mouse = page.Mouse;
158-
await mouse.MoveAsync(startX, startY);
159-
await mouse.DownAsync();
164+
await mouse.MoveAsync(startX, startY);
165+
await mouse.DownAsync();
160166

161167
// Move mouse smoothly in increments
162168
var tracks = GetVelocityTrack(offsetX);
@@ -185,6 +191,13 @@ await locator.ClickAsync(new LocatorClickOptions
185191
await Task.Delay(1000 * action.WaitTime);
186192
}
187193
}
194+
private void DeleteDirectory(string directory)
195+
{
196+
if (Directory.Exists(directory))
197+
{
198+
Directory.Delete(directory, true);
199+
}
200+
}
188201
public static List<int> GetVelocityTrack(float distance)
189202
{
190203
// Initialize the track list to store the movement distances

0 commit comments

Comments
 (0)