wpfhyperlinkwhite-framework

WPF White/UIAutomation hyperlink click


I am trying to use White framework to test WPF .NET4 application. XAML has a hyperlink declared as

<Hyperlink Command="{Binding Path=CmdOpenFile}" Name="hlOpenFile" >
    <TextBlock Text="Load file.." Name="txtLoadFileLabel" />
</Hyperlink>

CmdOpenFile creates and displays OpenFileDialog. When I test application manually it works as expected. I have written a unit test using White that get's an AutomationElement corresponding to this hyperlink and then it tries to click this link:

AutomationElement automationElement = someParentUIItem.GetElement(SearchCriteria.ByAutomationId("hlOpenFile"));
Hyperlink openFileLink = new Hyperlink(automationElement, window.ActionListener);
openFileLink.Click();

When I run this test - application starts, mouse pointer moves to this hyperlink and that's all - open file dialog does not appear. What can be the reason of this and how it can be fixed?


Solution

  • Looks like I've found some solution - not sure that it is the best one, but it works:

    AutomationElement automationElement = someParentUIItem.GetElement(SearchCriteria.ByAutomationId("hlOpenFile"));
    var invokePattern = automationElement.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
    invokePattern.Invoke();
    

    PS:

    The above solution just moved me one step further, but then I've faced with a blocker that code execution stops on opening dialog. Even if it was done in a separate thread - then it stops on the attempt to find this dialog, until dialog will be closed manually. With help of this question I have finally found what was the issue. I am using Win7x64, application was build for x86, but tests for AnyCPU. Changing test target to x86 and running it from x86 nunit console make test execution work as expected.