使用CodedUI操作.NET弹窗应用时无法执行任何操作的求助
Let's tackle your CodedUI problems head-on, using the code snippet you shared to build a more reliable solution. Here's what you need to know:
Your Core Problems
- You can't perform any CodedUI actions on your .NET-based popup application at all
- Once you launch a browser, even basic interactions like clicking the popup's "OK" button stop working
Breaking Down Your Recorded Code
You provided this generated code for the OK button click:
UIIPVPNRouterCreationWWindow.UIItemWindow11.UIIPVPNRouterCreationWClient.UIItemWindow.UIIPVPNRouterCreationDocument.UIOKButton;
Plus the window class definition:
[GeneratedCode("Coded UITest Builder", "12.0.31101.0")] public class UIIPVPNRouterCreationWWindow :...
The issue here is that the generated code relies on a rigid, hierarchical path to find the button. This is super fragile—especially when a browser is running, since it can shift window focus or alter the UI hierarchy. To make your code generic and reliable, we need to extract unique, stable properties from these elements instead of using the full parent chain.
Step-by-Step Fixes
1. Pull Stable Properties from Your Elements
Open the CodedUI Test Builder and inspect both the popup window (UIIPVPNRouterCreationWWindow) and the OK button (UIOKButton) to grab these key properties:
- Window properties: Look for
WindowTitle,ClassName,AutomationId, orName—these are the most consistent for .NET windows - Button properties: Note
AutomationId,Name,ControlType, orInnerTextfor the OK button
For example, if your popup's title is "IP VPN Router Creation" and the button's name is "OK", these will be your go-to identifiers.
2. Replace Hierarchical Paths with Direct Locators
Instead of chaining every parent control, use direct search properties to target the window and button. Here's a cleaner, more maintainable example:
// Locate the popup window using its title (replace with your actual window title) var vpnPopup = new WinWindow(); vpnPopup.SearchProperties[WinWindow.PropertyNames.WindowTitle] = "IP VPN Router Creation"; // Add the class name for extra precision (get this from your inspector) vpnPopup.SearchProperties[WinWindow.PropertyNames.ClassName] = "WindowsForms10.Window.8.app.0.378734a"; // Locate the OK button inside the popup var okBtn = new WinButton(vpnPopup); okBtn.SearchProperties[WinButton.PropertyNames.Name] = "OK"; okBtn.SearchProperties[WinButton.PropertyNames.ControlType] = ControlType.Button; // Perform the click action Mouse.Click(okBtn);
3. Fix Focus Issues When Browsers Are Running
Browsers often steal focus from other windows, which can block CodedUI from interacting with your popup. Add focus handling and wait steps to ensure the popup is ready:
// Ensure the popup has focus first vpnPopup.SetFocus(); // Wait for the button to be visible and enabled (prevents race conditions) vpnPopup.WaitForControlEnabled(okBtn); vpnPopup.WaitForControlVisible(okBtn); // Now click the button Mouse.Click(okBtn);
4. Confirm UI Automation is Enabled in Your .NET App
Make sure your popup app exposes the necessary properties for CodedUI to detect controls:
- WinForms: Set
AccessibleNameandAccessibleRolefor your controls (this makes them visible to UI automation tools) - WPF: Add
AutomationProperties.AutomationIdto your button in XAML (e.g.,AutomationProperties.AutomationId="OKButton")
Pro Tips for Reliable Tests
- Ditch the generated hierarchical paths in production code—they break instantly if the UI layout changes
- Set
Playback.PlaybackSettings.WaitForReadyLevel = WaitForReadyLevel.AllThreadsto ensure the UI is fully loaded before interactions - For modal popups, use
vpnPopup.WaitForControlExist(10000)to wait up to 10 seconds for the window to appear before acting
内容的提问来源于stack exchange,提问作者Divz

