[Silverlight] UI 測試/UI 自動化相關知識

(摘要自:http://blogs.msdn.com/gisenberg/archive/2008/07/12/ui-automation-in-silverlight-simulating-user-interactions.aspx

在 Silverlight 中,UI 自動化(UIA)的相關內容在下列名稱空間中:

System.Windows.Automation
System.Windows.Automation.Peers
System.Windows.Automation.Provider

要開放某個自定義控件爲支持 UI 自動化的,則需要覆蓋 OnCreateAutomationPeer 這個方法(定義在 Control 類中),來返回一個與該控件對應的 AutomationPeer 子類,實質是一個面向 COM 的 wrapper,用來描述和操作目標控件的一些信息,以及設值/取值等操作(用於模擬鍵盤輸入)。

那麼,這樣做了之後,就可以在 UI Test 的代碼裏通過自動化的接口,來取得這個 wrapper 對象,並進行相應的自動化測試。

可以用 Windows SDK 裏的一個工具 UISpy.exe 來查看 UI 結構信息(http://blogs.msdn.com/windowssdk/archive/2008/02/18/where-is-uispy-exe.aspx

相關一些代碼摘錄如下:
    public partial class SearchBar : Control
    {
       dot.gif
 
        
public SearchBar()
        {
            
this.GotFocus += (sender, args)
                
=>
                {
                    
this.SearchText.Focus();
                };
 
            InitializeComponent();
        }
 
       
protected override AutomationPeer OnCreateAutomationPeer()
       {
           
return new SearchBarAutomationPeer(this);
       }
    }


    public class SearchBarAutomationPeer : FrameworkElementAutomationPeer, IValueProvider
    {
        
public SearchBarAutomationPeer(SearchBar searchBar) : base(searchBar)
        {
        }

       
protected override string GetAutomationIdCore()
        {
            
return "SearchBar"// You're going to want to make this unique. ;)
        }
 
        
protected override string GetClassNameCore()
        {
            
return "SearchBar";
        }
 
        
protected override bool IsKeyboardFocusableCore()
        {
            
return true;
        }

       
public SearchBar SearchBar
        {
            
get
            {
                
return (SearchBar)base.Owner;
            }
        }
 
        
#region IValueProvider Members
 
        
public bool IsReadOnly
        {
            
get
            {
                
return this.SearchBar.SearchText.IsReadOnly;
            }
        }
 
        
public void SetValue(string value)
        {
            
this.SearchBar.SearchText.Text = value;
        }
 
        
public string Value
        {
            
get
            {
                
return this.SearchBar.SearchText.Text;
            }
        }
 
        
#endregion

測試代碼:
1.
        [TestMethod]
        
public void TestMethod1()
        {
            Process process 
= System.Diagnostics.Process.GetProcessesByName("iexplore").First();
 
            AutomationElement browserInstance 
= System.Windows.Automation.AutomationElement.FromHandle(process.MainWindowHandle);
            TreeWalker tw 
= new TreeWalker(new PropertyCondition(AutomationElement.ClassNameProperty, "SearchBar"));
            AutomationElement searchBar 
= tw.GetFirstChild(browserInstance);
 
            myElement.SetFocus();
            Thread.Sleep(
1000);

            searchBar.SetFocus();
            Thread.Sleep(
1000);
 
            SendKeys.SendWait(
"Hello, world!");
        }

2.
        [TestMethod]
        
public void TestMethod1()
        {
            Process process 
= System.Diagnostics.Process.GetProcessesByName("iexplore").First();
 
            AutomationElement myElement 
= System.Windows.Automation.AutomationElement.FromHandle(process.MainWindowHandle);
            TreeWalker tw 
= new TreeWalker(new PropertyCondition(AutomationElement.ClassNameProperty, "SearchBar"));
            AutomationElement searchBar 
= tw.GetFirstChild(myElement);
 
            
object valuePattern;
            searchBar.TryGetCurrentPattern(ValuePattern.Pattern, 
out valuePattern);
            ((ValuePattern)valuePattern).SetValue(
"Hello, world!");
        }


over.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章