-->

How to enable a button using UIAutomation or White

2019-08-27 15:10发布

问题:

I'm trying to automate an installation wizard and I needed to click on a button that is not enabled.

I know that this can be done using Windows Messages, but I was wondering if there is already support in White or UIAutomation for this

For reference this doesn't work:

var invoke =  (System.Windows.Automation.InvokePattern)setup.button("Next >").AutomationElement.GetCurrentPattern(System.Windows.Automation.InvokePattern.Pattern);  
invoke.Invoke();

neither does this

var guiAutomation = new API_GuiAutomation("msiexec");  
var setup = guiAutomation.windows()[0]; 
setup .bringToFront(); 
setup .button("Next >").mouse().click();  // this will work
setup .button("Next >").mouse().click();  // this will not work since the button is not enabled

The example above uses the White based API I added to the O2 Platform (see here an example of automating notepad)

回答1:

I don't believe you can do this with UIAutomation: what you're asking for is something that goes beyond the remit of the UIAutomation framework. It is intended to let you do with code the same that you could otherwise do with mouse and keyboard. It is not designed as a general means of manipulating UI state.



回答2:

You can directly call the Win32 function EnableWindow, which sends a Windows Message, just like you said.

I looked for that functionality in UIAutomation, but did not find it.



回答3:

If you're using White

Mouse.Instance.Location = this.ClickablePoint;

Mouse.LeftDown(); Mouse.Instance.Location = point; Mouse.LeftUp();



回答4:

Clicking onto an enabled button is sooner related to fault injection techniques, rather than normal execution of an application. Is this button disabled for the rest of the application's life cycle or will be enabled later? If the latter, why don't wait for enabling? By using do/while on element.Current.IsEnabled or the Wait-UIAButton cmdlet.