針對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,都無能為力了!!!
可有高手解決此問題?
浙公網安備 33010602011771號