Reading up on silverlight unittesting from Justin Angel’s post. I ran into some startup problems like not iheriting from SilverlightTest in my test class (took me two hours ;)) and adding the right assemblies to our project. But the biggest challenge was figuring out how each control could be addressed.
- TextBox, text input with Value pattern
// create an automationpeer for the control var textBoxPeer = new TextBoxAutomationPeer(ctrl); // create a valueprovider to fill the control var textBoxProvider = (IValueProvider)textBoxPeer.GetPattern(PatternInterface.Value); // fill some text textBoxProvider.SetValue(fieldvalue);
// create an automationpeer for the control var comboBoxPeer = new ComboBoxAutomationPeer(ctrl); // find the child with the value to set var comboBoxSelectionItem = (ISelectionItemProvider)comboBoxPeer.GetChildren() .Where(x => x.GetName().Equals(valueToSelect)) .First() // will fail if valueToSelect is not found .GetPattern(PatternInterface.SelectionItem); // select the value hoursComboBoxSelectionItem.Select();
// create automationpeer for the control var radioButtonPeer = new RadioButtonAutomationPeer(ctrl); // create a selectionitemprovider to set the selection var radioButtonProvider = (ISelectionItemProvider)radioButtonPeer.GetPattern(PatternInterface.SelectionItem); // select the value // this will NOT raise the Click event, but the CHECKED event radioButtonProvider.Select();
// create an automationpeer for the button var buttonPeer = new ButtonAutomationPeer(ctrl); // create am invokeprovider to click the control var buttonInvoker = (IInvokeProvider)buttonPeer.GetPattern(PatternInterface.Invoke); // check button is available Assert.IsTrue(buttonPeer.IsEnabled(), "Button was not enabled"); // click the button buttonInvoker.Invoke();
My next post is about custom AutomationPeer classes.