Merge pull request #976 from visagang/features/vguruparan
Update webdriver to handle fileupload and dropdown selection
This commit is contained in:
commit
a4149a07eb
|
|
@ -7,5 +7,7 @@ public enum BroswerActionEnum
|
||||||
Typing = 3,
|
Typing = 3,
|
||||||
Hover = 4,
|
Hover = 4,
|
||||||
Scroll = 5,
|
Scroll = 5,
|
||||||
DragAndDrop = 6
|
DragAndDrop = 6,
|
||||||
|
DropDown = 7,
|
||||||
|
FileUpload = 8
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,8 @@ public class ElementActionArgs
|
||||||
|
|
||||||
[JsonPropertyName("content")]
|
[JsonPropertyName("content")]
|
||||||
public string? Content { get; set; }
|
public string? Content { get; set; }
|
||||||
|
[JsonPropertyName("file_urls")]
|
||||||
|
public string[] FileUrl { get; set; }
|
||||||
|
|
||||||
public ElementPosition? Position { get; set; }
|
public ElementPosition? Position { get; set; }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,6 @@
|
||||||
|
using System.IO;
|
||||||
|
using System.Net.Http;
|
||||||
|
|
||||||
namespace BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver;
|
namespace BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver;
|
||||||
|
|
||||||
public partial class PlaywrightWebDriver
|
public partial class PlaywrightWebDriver
|
||||||
|
|
@ -50,6 +53,18 @@ public partial class PlaywrightWebDriver
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (action.Action == BroswerActionEnum.DropDown)
|
||||||
|
{
|
||||||
|
await locator.ClickAsync();
|
||||||
|
var optionLocator = page.Locator($"//div[text()='{action.Content}']");
|
||||||
|
var optionCount = await optionLocator.CountAsync(); ;
|
||||||
|
if (optionCount == 0)
|
||||||
|
{
|
||||||
|
Serilog.Log.Error($"Dropdown option not found: {action.Content}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await optionLocator.First.ClickAsync();
|
||||||
|
}
|
||||||
else if (action.Action == BroswerActionEnum.InputText)
|
else if (action.Action == BroswerActionEnum.InputText)
|
||||||
{
|
{
|
||||||
await locator.FillAsync(action.Content);
|
await locator.FillAsync(action.Content);
|
||||||
|
|
@ -63,6 +78,36 @@ public partial class PlaywrightWebDriver
|
||||||
await locator.PressAsync(action.PressKey);
|
await locator.PressAsync(action.PressKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (action.Action == BroswerActionEnum.FileUpload)
|
||||||
|
{
|
||||||
|
if (action.FileUrl.Length == 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var fileChooser = await page.RunAndWaitForFileChooserAsync(async () =>
|
||||||
|
{
|
||||||
|
await locator.ClickAsync();
|
||||||
|
});
|
||||||
|
var guid = Guid.NewGuid().ToString();
|
||||||
|
var directory = Path.Combine(Path.GetTempPath(), guid);
|
||||||
|
if (Directory.Exists(directory))
|
||||||
|
{
|
||||||
|
Directory.Delete(directory, true);
|
||||||
|
}
|
||||||
|
Directory.CreateDirectory(directory);
|
||||||
|
var localPaths = new List<string>();
|
||||||
|
using var httpClient = new HttpClient();
|
||||||
|
foreach (var fileUrl in action.FileUrl)
|
||||||
|
{
|
||||||
|
var bytes = await httpClient.GetByteArrayAsync(fileUrl);
|
||||||
|
var fileName = new Uri(fileUrl).AbsolutePath;
|
||||||
|
var localPath = Path.Combine(directory, Path.GetFileName(fileName));
|
||||||
|
await File.WriteAllBytesAsync(localPath, bytes);
|
||||||
|
await Task.Delay(2000);
|
||||||
|
localPaths.Add(localPath);
|
||||||
|
}
|
||||||
|
await fileChooser.SetFilesAsync(localPaths);
|
||||||
|
}
|
||||||
else if (action.Action == BroswerActionEnum.Typing)
|
else if (action.Action == BroswerActionEnum.Typing)
|
||||||
{
|
{
|
||||||
await locator.PressSequentiallyAsync(action.Content);
|
await locator.PressSequentiallyAsync(action.Content);
|
||||||
|
|
@ -128,7 +173,6 @@ public partial class PlaywrightWebDriver
|
||||||
await Task.Delay(1000 * action.WaitTime);
|
await Task.Delay(1000 * action.WaitTime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
|
|
||||||
|
|
@ -11,12 +11,20 @@
|
||||||
"action": {
|
"action": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "action to perform on element",
|
"description": "action to perform on element",
|
||||||
"enum": [ "Click", "InputText" ]
|
"enum": [ "Click", "InputText", "DropDown", "FileUpload" ]
|
||||||
},
|
},
|
||||||
"content": {
|
"content": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "content to input in element"
|
"description": "content to input in element"
|
||||||
},
|
},
|
||||||
|
"file_urls": {
|
||||||
|
"type": "array",
|
||||||
|
"description": "The output format must be string array of file urls.",
|
||||||
|
"items": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "file url"
|
||||||
|
}
|
||||||
|
},
|
||||||
"press_key": {
|
"press_key": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"enum": [ "Enter" ],
|
"enum": [ "Enter" ],
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue