-
-
Notifications
You must be signed in to change notification settings - Fork 582
feat(playwright): handle <select> dropdowns in DropDown action #1113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3d23639
7429480
71d7392
181fb4a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -55,15 +55,23 @@ await locator.ClickAsync(new LocatorClickOptions | |||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
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) | ||||||||||||||||||||||||||||||||||||||||||||||
var tagName = await locator.EvaluateAsync<string>("el => el.tagName.toLowerCase()"); | ||||||||||||||||||||||||||||||||||||||||||||||
if (tagName == "select") | ||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||
Serilog.Log.Error($"Dropdown option not found: {action.Content}"); | ||||||||||||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||||||||||||
await HandleSelectDropDownAsync(page, locator, action); | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
else | ||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||
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(); | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
await optionLocator.First.ClickAsync(); | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
else if (action.Action == BroswerActionEnum.InputText) | ||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -247,4 +255,29 @@ public static List<int> GetVelocityTrack(float distance) | |||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
return track; | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
private async Task HandleSelectDropDownAsync(IPage page, ILocator locator, ElementActionArgs action) | ||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||
if (string.IsNullOrWhiteSpace(action.Content)) | ||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||
throw new InvalidOperationException("Dropdown target option (action.Content) cannot be null or empty when using a <select> element."); | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
var selectedText = await locator.Locator("option:checked").InnerTextAsync(); | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
if (!string.Equals(selectedText?.Trim(), action.Content.Trim(), StringComparison.OrdinalIgnoreCase)) | ||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||
await locator.SelectOptionAsync(new SelectOptionValue | ||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||
Label = action.Content | ||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+270
to
+274
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Selecting by label may fail if the option text doesn't exactly match. Consider also trying to select by value if label selection fails, or use a more robust selection strategy that handles both scenarios.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||||||||||||||||||||||||||
await page.WaitForLoadStateAsync(LoadState.NetworkIdle); | ||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Waiting for NetworkIdle after every select option change may cause unnecessary delays, especially if the select doesn't trigger network requests. Consider making this configurable or adding a timeout to prevent indefinite waiting. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||||||||||||||||||||||||||
_logger.LogInformation($"Dropdown changed to: {action.Content}"); | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
else | ||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||
_logger.LogInformation($"Dropdown already on correct option: {action.Content}"); | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line will throw an exception if no option is currently selected in the select element. Consider using
FirstOrDefaultAsync()
or checking if any options are selected before callingInnerTextAsync()
.Copilot uses AI. Check for mistakes.