[英]Share Application under test object between multiple coded ui tests
我想在一個測試中啟動被測試的應用程序,並在其他測試中使用打開的應用程序。 這是因為啟動應用程序需要花費一些時間,如果我在每次測試中都重復一次,則可能會很昂貴。 我想在與UI映射對象一起初始化的對象映射中有一個由AUT提供的單個對象。
此方法失敗,因為即使對象是靜態的,也不會在不同的測試之間傳遞該對象。 這個問題有解決方案嗎?
UIMap
public partial class UIMap
{
public ApplicationUnderTest _aut { get; set; }
public void AUT_Open()
{
string AUTExecutable = ConfigurationManager.AppSettings["AUTExecutable"];
_aut = ApplicationUnderTest.Launch(AUTExecutable );
}
...
}
測試
private static UIMap map;
[TestMethod]
public void Test01()
{
...
this.UIMap.RecognitionDesigner_Open();
}
[TestMethod]
public void Test02()
{
//Do something on the UIMap that tries to use the same member variable- _aut
//in the UIMap
}
我可以使用_aut.CloseOnPlaybackCleanup = false;
來解決此問題_aut.CloseOnPlaybackCleanup = false;
。 顯然,在每個測試方法的結尾,對UIMap對象的引用似乎都丟失了。
public partial class UIMap
{
public ApplicationUnderTest _aut { get; set; }
public void AUT_Open()
{
string AUTExecutable = ConfigurationManager.AppSettings["AUTExecutable"];
_aut = ApplicationUnderTest.Launch(AUTExecutable );
_aut.CloseOnPlaybackCleanup = false;
}
...
}
測試彼此獨立運行,並且在每個測試上重新創建UIMap。 我建議使用ClassInitialize
屬性,因為標記有該屬性的方法僅執行一次,並且在類中的所有測試之前執行。 您將完全確定將啟動您的過程,因為現在您取決於測試的執行順序,這不好。
private static TestContext contextSave;
[ClassInitialize]
public static void DoOneTime(TestContext context)
{
string AUTExecutable = ConfigurationManager.AppSettings["AUTExecutable"];
_aut = ApplicationUnderTest.Launch(AUTExecutable );
context.Properties.Add("AUT", _aut);
contextSave = context;
}
[TestMethod]
public void Test01()
{
//...
DoSthmWithAUT(context.Properties["AUT"]);
}
[TestMethod]
public void Test02()
{
DoOtherWithAUT(context.Properties["AUT"]);
}
[ClassCleanup]
public static void Cleanup()
{
contextSave = null;
}
注意,通常,您將無法知道Test01在Test02之前且僅在Test02之前運行。 如果您真的想要訂購,請使用Ordered Tests
。
我有同樣的問題,即在第一個測試方法中初始化的AUT對象在第二個方法中為NULL。 我嘗試了很多事情,但是只有一個幫助我:我必須將類的AUT屬性設置為static !
這是我使用計算器應用程序的非常簡單的代碼。
using System;
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UITesting.WpfControls;
using Microsoft.VisualStudio.TestTools.UITesting.WinControls;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UITest.Extension;
using Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard;
namespace CUIT
{
[CodedUITest]
public class UITestDemo
{
private readonly string path = @"C:\Windows\SysWOW64\calc.exe";
static ApplicationUnderTest app = null;
[TestMethod]
public void Start()
{
app = ApplicationUnderTest.Launch(path);
Playback.Wait(1000);
app.CloseOnPlaybackCleanup = false;
app.SearchProperties[ApplicationUnderTest.PropertyNames.Name] = "Calculator";
app.SearchProperties[ApplicationUnderTest.PropertyNames.ClassName] = "CalcFrame";
}
[TestMethod]
public void TestButtonOne()
{
Assert.IsNotNull(app, "Should not be NULL!");
app.SetFocus();
WinButton btn1 = new WinButton(app);
btn1.SearchProperties[WinButton.PropertyNames.Name] = "1";
Mouse.Click(btn1);
Mouse.Click(btn1);
Mouse.Click(btn1);
Playback.Wait(3000);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.