针对Silverlight 4的UI自动化测试
针对Silverlight 4的UI自动化测试
目前Visual Studio 2010 SP1也早已发布,我们只需要在此基础上装Feature Pack 2补丁包,便可通过Coded UI Test来录制Silverlight 4的UI界面了,并Playback你的测试用例了。注意,Feature Pack2的用处是在被测试机器(MUT)打的补丁。当然你在所有机器上都装也不会有什么问题。
这里我们有一个DLL文件需要注意:Microsoft.VisualStudio.TestTools.UITest.Extension.SilverlightUIAutomationHelper.dll
这个DLL是用来帮助Coded UI Test识别Silverlight 4的UI控件的,注意它是被Silverlight程序引用,而不是Test Project,很多朋友起初容易混淆。
以下为BrowserWindow的录制结果:
[GeneratedCode("Coded UITest Builder", "10.0.40219.1")]
public class UISilverlightApplicatiWindow : BrowserWindow
{
public UISilverlightApplicatiWindow()
{
#region Search Criteria
this.SearchProperties[UITestControl.PropertyNames.Name] = "SilverlightApplication1";
this.SearchProperties[UITestControl.PropertyNames.ClassName] = "IEFrame";
this.WindowTitles.Add("SilverlightApplication1");
#endregion
}
public void LaunchUrl(System.Uri url)
{
this.CopyFrom(BrowserWindow.Launch(url));
}
#region Properties
public UISilverlightApplicatiDocument UISilverlightApplicatiDocument
{
get
{
if ((this.mUISilverlightApplicatiDocument == null))
{
this.mUISilverlightApplicatiDocument = new UISilverlightApplicatiDocument(this);
}
return this.mUISilverlightApplicatiDocument;
}
}
#endregion
#region Fields
private UISilverlightApplicatiDocument mUISilverlightApplicatiDocument;
#endregion
}
public class UISilverlightApplicatiWindow : BrowserWindow
{
public UISilverlightApplicatiWindow()
{
#region Search Criteria
this.SearchProperties[UITestControl.PropertyNames.Name] = "SilverlightApplication1";
this.SearchProperties[UITestControl.PropertyNames.ClassName] = "IEFrame";
this.WindowTitles.Add("SilverlightApplication1");
#endregion
}
public void LaunchUrl(System.Uri url)
{
this.CopyFrom(BrowserWindow.Launch(url));
}
#region Properties
public UISilverlightApplicatiDocument UISilverlightApplicatiDocument
{
get
{
if ((this.mUISilverlightApplicatiDocument == null))
{
this.mUISilverlightApplicatiDocument = new UISilverlightApplicatiDocument(this);
}
return this.mUISilverlightApplicatiDocument;
}
}
#endregion
#region Fields
private UISilverlightApplicatiDocument mUISilverlightApplicatiDocument;
#endregion
}
以下为HtmlDocument录制结果:
[GeneratedCode("Coded UITest Builder", "10.0.40219.1")]
public class UISilverlightApplicatiDocument : HtmlDocument
{
public UISilverlightApplicatiDocument(UITestControl searchLimitContainer) :
base(searchLimitContainer)
{
#region Search Criteria
this.SearchProperties[HtmlDocument.PropertyNames.Id] = null;
this.SearchProperties[HtmlDocument.PropertyNames.RedirectingPage] = "False";
this.SearchProperties[HtmlDocument.PropertyNames.FrameDocument] = "False";
this.FilterProperties[HtmlDocument.PropertyNames.Title] = "SilverlightApplication1";
this.FilterProperties[HtmlDocument.PropertyNames.AbsolutePath] = "/WebSite2/SilverlightApplication1TestPage.html";
this.FilterProperties[HtmlDocument.PropertyNames.PageUrl] = "http://localhost:24805/WebSite2/SilverlightApplication1TestPage.html";
this.WindowTitles.Add("SilverlightApplication1");
#endregion
}
#region Properties
public UISilverlightControlHoPane UISilverlightControlHoPane
{
get
{
if ((this.mUISilverlightControlHoPane == null))
{
this.mUISilverlightControlHoPane = new UISilverlightControlHoPane(this);
}
return this.mUISilverlightControlHoPane;
}
}
#endregion
#region Fields
private UISilverlightControlHoPane mUISilverlightControlHoPane;
#endregion
}
public class UISilverlightApplicatiDocument : HtmlDocument
{
public UISilverlightApplicatiDocument(UITestControl searchLimitContainer) :
base(searchLimitContainer)
{
#region Search Criteria
this.SearchProperties[HtmlDocument.PropertyNames.Id] = null;
this.SearchProperties[HtmlDocument.PropertyNames.RedirectingPage] = "False";
this.SearchProperties[HtmlDocument.PropertyNames.FrameDocument] = "False";
this.FilterProperties[HtmlDocument.PropertyNames.Title] = "SilverlightApplication1";
this.FilterProperties[HtmlDocument.PropertyNames.AbsolutePath] = "/WebSite2/SilverlightApplication1TestPage.html";
this.FilterProperties[HtmlDocument.PropertyNames.PageUrl] = "http://localhost:24805/WebSite2/SilverlightApplication1TestPage.html";
this.WindowTitles.Add("SilverlightApplication1");
#endregion
}
#region Properties
public UISilverlightControlHoPane UISilverlightControlHoPane
{
get
{
if ((this.mUISilverlightControlHoPane == null))
{
this.mUISilverlightControlHoPane = new UISilverlightControlHoPane(this);
}
return this.mUISilverlightControlHoPane;
}
}
#endregion
#region Fields
private UISilverlightControlHoPane mUISilverlightControlHoPane;
#endregion
}
Silverlight的MainPage录制结果:
[GeneratedCode("Coded UITest Builder", "10.0.40219.1")]
public class UIMainPageMainPage : SilverlightControl
{
public UIMainPageMainPage(UITestControl searchLimitContainer) :
base(searchLimitContainer)
{
#region Search Criteria
this.SearchProperties[UITestControl.PropertyNames.ControlType] = "MainPage";
this.WindowTitles.Add("SilverlightApplication1");
#endregion
}
#region Properties
public SilverlightText UITextBlockText
{
get
{
if ((this.mUITextBlockText == null))
{
this.mUITextBlockText = new SilverlightText(this);
#region Search Criteria
this.mUITextBlockText.SearchProperties[SilverlightText.PropertyNames.AutomationId] = "textBlock1";
this.mUITextBlockText.WindowTitles.Add("SilverlightApplication1");
#endregion
}
return this.mUITextBlockText;
}
}
#endregion
#region Fields
private SilverlightText mUITextBlockText;
#endregion
}
public class UIMainPageMainPage : SilverlightControl
{
public UIMainPageMainPage(UITestControl searchLimitContainer) :
base(searchLimitContainer)
{
#region Search Criteria
this.SearchProperties[UITestControl.PropertyNames.ControlType] = "MainPage";
this.WindowTitles.Add("SilverlightApplication1");
#endregion
}
#region Properties
public SilverlightText UITextBlockText
{
get
{
if ((this.mUITextBlockText == null))
{
this.mUITextBlockText = new SilverlightText(this);
#region Search Criteria
this.mUITextBlockText.SearchProperties[SilverlightText.PropertyNames.AutomationId] = "textBlock1";
this.mUITextBlockText.WindowTitles.Add("SilverlightApplication1");
#endregion
}
return this.mUITextBlockText;
}
}
#endregion
#region Fields
private SilverlightText mUITextBlockText;
#endregion
}
另外:
但是这里有一个问题,如果作为数据挖掘的蜘蛛爬虫来说,如果数据源网站用silverlight做,但没有引用Microsoft.VisualStudio.TestTools.UITest.Extension.SilverlightUIAutomationHelper.dll ,那么无论是数据抓取还是Coded UI Test,都无能为力了!!!
可有高手解决此问题?