[Silverlight] UI 测试/UI 自动化相关知识
2008-09-27 23:15
471 查看
(摘要自: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
{
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.
在 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
{
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.
相关文章推荐
- [Silverlight] UI 测试/UI 自动化相关知识
- 从UI Automation看Windows平台自动化测试原理
- UI相关基础知识
- Android自动化测试中uiautomator修改uiautomatorviewer获取不到动态界面的缺陷
- 测试运行: Microsoft UI 自动化库
- MyCat相关知识及测试要点
- Appium Python behave 自动化测试框架搭建(2) - behave 知识准备
- Testing - 软件测试知识梳理 - 相关词汇
- Android 自动化测试相关
- 测试相关知识总结
- web【UI】自动化测试
- LoadRunner测试结果中的吞吐量相关知识
- 软件测试的相关知识
- robotium android 自动化UI测试获取控件是否隐藏的属性
- 【测试】关于UI自动化的疑问(记录ing)
- 另眼看自动化测试工具-浅谈自动化测试的好处,缺陷及相关弥补策略
- Android UI自动化测试最佳实践 - 51Testing软件测试网-中国软件测试人的精神家园
- [转]Android UI 自动化测试
- 初学者的Selenium自动化测试指南,基于Python(七)——cookie相关操作