TestStack / White

DEPRECATED - no longer actively maintained
Other
1.02k stars 486 forks source link

Casting Custom Control to known Type #303

Open qualityking opened 9 years ago

qualityking commented 9 years ago

Hi Experts, I am facing issues while casting an automation item to known type (Casting a custom control to DevExpress cobo box)

White.UIItems.Custom.CustomUIItem MyDropDown= window.Get<White.UIItems.Custom.CustomUIItem>(White.UIItems.Finders.SearchCriteria.ByClassName("ComboBoxEdit"));

DevExpress.Xpf.Editors.ComboBoxEdit envList = (DevExpress.Xpf.Editors.ComboBoxEdit)MyDropDown;
// Error on above line for casting Error

Can you please help me find the correct way of casting the custom controls. or is there any way to find a row type object and cast it a custom control?

maxinfet commented 9 years ago

You shouldn't cast the MyDropDown to your actual control's type. After finding the control you should then be able to call any of the base functionality of a UIItem on it like clicking. If you need more functionality which I am assuming you do you can create your own class that inherits from CustomUIItem then override methods on UIItem. Here is a rough example of creating a custom control. You would need to find controls that are contained in your UIItem. If those controls are custom as well, eventually you will need to drop down to base UIAutomation work with with the patterns. Hopefully this helps.

using System.Diagnostics;
using System.Windows.Automation;
using TestStack.White.UIItems;
using TestStack.White.UIItems.Custom;
using TestStack.White.UIItems.WPFUIItems;

namespace WpfTodo
{
    public class ComboBoxEdit : CustomUIItem
    {
        //Custom controls made of default controls
        public TextBox EditableText
        {
            get { return this.Get<TextBox>("TextBoxAutomationId"); }
        }

        public override void SetValue(object value)
        {
            EditableText.Text = (string) value;
        }

        //Custom controls made of custom controls that support patterns
        public AutomationElement CustomControlThatSupportsTextPattern
        {
            get
            {
                return this.AutomationElement.FindFirst(TreeScope.Children,
                    new PropertyCondition(AutomationElement.AutomationIdProperty, "TextBoxAutomationId"));
            }
        }

        public void SetCustomControlText(string text)
        {
            var textControl = CustomControlThatSupportsTextPattern;
            ValuePattern pattern = null;

            if (textControl != null)
                pattern = textControl.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;

            if (pattern != null)
                pattern.SetValue(text);
        }
    }
}
williereed commented 9 years ago

I'm in the process of creating my own class inheriting from CustomUIItem to implement IsSelected when the item is a CheckBox... and not comprehending how this is accomplished, any pointers would be greatly appreciated.