Silverlight unittest / automation

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);
    
  • ComboBox, find child with value to select and use SelectionItem pattern
  • // 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();
    
  • RadioButton, select with the SelectionItem pattern. This raises the (Un)Checked event, not the Click event.
  • // 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();
    
  • Button, click it with Invoke pattern
  • // 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.

About erictummers

My work as a recruited developer changes almost every month. I like challenges and sharing the solutions with others. On my blog I’ll mostly post about my work, but expect an occasional home project, productivity tip and tooling review.
This entry was posted in Test and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s