Skip to content

Commit a4149a0

Browse files
authored
Merge pull request #976 from visagang/features/vguruparan
Update webdriver to handle fileupload and dropdown selection
2 parents 463a829 + 2a6463b commit a4149a0

4 files changed

Lines changed: 59 additions & 3 deletions

File tree

src/Infrastructure/BotSharp.Abstraction/Browsing/Enums/BroswerActionEnum.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ public enum BroswerActionEnum
77
Typing = 3,
88
Hover = 4,
99
Scroll = 5,
10-
DragAndDrop = 6
10+
DragAndDrop = 6,
11+
DropDown = 7,
12+
FileUpload = 8
1113
}

src/Infrastructure/BotSharp.Abstraction/Browsing/Models/ElementActionArgs.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public class ElementActionArgs
1212

1313
[JsonPropertyName("content")]
1414
public string? Content { get; set; }
15+
[JsonPropertyName("file_urls")]
16+
public string[] FileUrl { get; set; }
1517

1618
public ElementPosition? Position { get; set; }
1719

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

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
using System.IO;
2+
using System.Net.Http;
3+
14
namespace BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver;
25

36
public partial class PlaywrightWebDriver
@@ -50,6 +53,18 @@ await locator.ClickAsync(new LocatorClickOptions
5053
});
5154
}
5255
}
56+
else if (action.Action == BroswerActionEnum.DropDown)
57+
{
58+
await locator.ClickAsync();
59+
var optionLocator = page.Locator($"//div[text()='{action.Content}']");
60+
var optionCount = await optionLocator.CountAsync(); ;
61+
if (optionCount == 0)
62+
{
63+
Serilog.Log.Error($"Dropdown option not found: {action.Content}");
64+
return;
65+
}
66+
await optionLocator.First.ClickAsync();
67+
}
5368
else if (action.Action == BroswerActionEnum.InputText)
5469
{
5570
await locator.FillAsync(action.Content);
@@ -63,6 +78,36 @@ await locator.ClickAsync(new LocatorClickOptions
6378
await locator.PressAsync(action.PressKey);
6479
}
6580
}
81+
else if (action.Action == BroswerActionEnum.FileUpload)
82+
{
83+
if (action.FileUrl.Length == 0)
84+
{
85+
return;
86+
}
87+
var fileChooser = await page.RunAndWaitForFileChooserAsync(async () =>
88+
{
89+
await locator.ClickAsync();
90+
});
91+
var guid = Guid.NewGuid().ToString();
92+
var directory = Path.Combine(Path.GetTempPath(), guid);
93+
if (Directory.Exists(directory))
94+
{
95+
Directory.Delete(directory, true);
96+
}
97+
Directory.CreateDirectory(directory);
98+
var localPaths = new List<string>();
99+
using var httpClient = new HttpClient();
100+
foreach (var fileUrl in action.FileUrl)
101+
{
102+
var bytes = await httpClient.GetByteArrayAsync(fileUrl);
103+
var fileName = new Uri(fileUrl).AbsolutePath;
104+
var localPath = Path.Combine(directory, Path.GetFileName(fileName));
105+
await File.WriteAllBytesAsync(localPath, bytes);
106+
await Task.Delay(2000);
107+
localPaths.Add(localPath);
108+
}
109+
await fileChooser.SetFilesAsync(localPaths);
110+
}
66111
else if (action.Action == BroswerActionEnum.Typing)
67112
{
68113
await locator.PressSequentiallyAsync(action.Content);
@@ -128,7 +173,6 @@ await locator.ClickAsync(new LocatorClickOptions
128173
await Task.Delay(1000 * action.WaitTime);
129174
}
130175
}
131-
132176
public static List<int> GetVelocityTrack(float distance)
133177
{
134178
// Initialize the track list to store the movement distances

src/Plugins/BotSharp.Plugin.WebDriver/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions/util-web-action_on_element.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,20 @@
1111
"action": {
1212
"type": "string",
1313
"description": "action to perform on element",
14-
"enum": [ "Click", "InputText" ]
14+
"enum": [ "Click", "InputText", "DropDown", "FileUpload" ]
1515
},
1616
"content": {
1717
"type": "string",
1818
"description": "content to input in element"
1919
},
20+
"file_urls": {
21+
"type": "array",
22+
"description": "The output format must be string array of file urls.",
23+
"items": {
24+
"type": "string",
25+
"description": "file url"
26+
}
27+
},
2028
"press_key": {
2129
"type": "string",
2230
"enum": [ "Enter" ],

0 commit comments

Comments
 (0)