I’m trying to automate some UI testing. I have a window that calculates something based on some textboxes once you click a button. I’ve got the button correctly being clicked, but I can’t correctly get the result of the claculation because the calculate() method is being called after I check for the correct answer in the test.
InvokePattern pattern = element.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
pattern.Invoke();
Calling Invoke() doesn’t block, so it immediately returns and then starts checking if calculate() worked even though it hasn’t been called yet. How can I change the Invoke() call so that it waits until after calculate() has been called?
element is the AutomationElement for my button.
private void Button_Click(object sender, RoutedEventArgs e)
{
double v1 = 0;
double v2 = 0;
if(Double.TryParse(tbVal1.Text, out v1) && Double.TryParse(tbVal2.Text, out v2))
{
double output = v1 + v2;
tbAnswer.Text = "The answer is " + output.ToString();
}
}
EDIT:
I ended up taking a different route to my solution by subscribing the the InvokePattern.InvokedEvent event. This allowed me to put whatever logic I needed into the event handler to react to the invoking of the AutomationElement.
InvokePattern pattern = element.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
Automation.AddAutomationEventHandler(InvokePattern.InvokedEvent, element, TreeScope.Element,
new AutomationEventHandler(OnUIAutomationEvent));
pattern.Invoke();
Let me know if I need to add anything else. Thanks!
According to these guidelines, the InvokedEvent should be raised by the control: