执行[ClassCleanup]方法时出现编码的UI测试单线程单元(STA)错误

时间:2013-05-21 10:06:08

标签: coded-ui-tests

为什么我的类在编码的UI测试中执行清理方法时会出现STA错误,但似乎具体的测试方法工作正常。我的代码看起来像ff:

    [CodedUITest]
    public class ConcreteCUIT : TestBase 
    {
        [ClassInitialize]
        public static void Initialize(TestContext context)
        {
          //do init
        }
        [ClassCleanup]
        public static void CleanUp()
        {
          //do clean-up - STA error!!!
        }
        [TestMethod]
        public void Concrete1CRUDTest()
        {
          //do UI test
        }
        [TestMethod]
        public void Concrete2CRUDTest()
        {
          //do UI test
        }
    } 

我在下面收到以下错误:

Class Cleanup method SampleCUIT.CleanUp failed. Error Message:Microsoft.VisualStudio.TestTools.UITest.Extension.UITestException: The Coded UI Test is running in Single Thread Apartment (STA) mode of COM.  In this mode, all the playback calls should happen from the TestMethod thread only and UITestControl should not be shared across TestMethods.. 
Stack Trace:
   at Microsoft.VisualStudio.TestTools.UITest.Playback.ScreenElement.ThrowExceptionIfCrossThreadAccess(IScreenElement uiElement)
   at Microsoft.VisualStudio.TestTools.UITest.Playback.ScreenElement.FindAllScreenElement(String queryId, Int32 depth, Boolean singleQueryId, Boolean throwException)
   at Microsoft.VisualStudio.TestTools.UITest.Playback.ScreenElement.FindScreenElement(String queryId, Int32 depth)
   at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.FindFirstDescendant(String queryId, Int32 maxDepth, Int32& timeLeft)
   at Microsoft.VisualStudio.TestTools.UITesting.SearchHelper.GetElement(Boolean useCache, ISearchArgument searchArg)
   at Microsoft.VisualStudio.TestTools.UITesting.SearchHelper.Search(ISearchArgument searchArg)
   at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.FindInternal()
   at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.FindControlIfNecessary()
   at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.Click(MouseButtons button, ModifierKeys modifierKeys, Point relativeCoordinates)
   at Microsoft.VisualStudio.TestTools.UITesting.Mouse.ClickImplementation(UITestControl control, MouseButtons button, ModifierKeys modifierKeys, Point relativeCoordinate)
   at Microsoft.VisualStudio.TestTools.UITesting.Mouse.ClickImplementationWrapper(UITestControl control, MouseButtons button, ModifierKeys modifierKeys, Point relativeCoordinate)
   at Microsoft.VisualStudio.TestTools.UITesting.Mouse.Click(UITestControl control)

1 个答案:

答案 0 :(得分:1)

堆栈跟踪的第一行说明了问题:“... 所有回放调用都应该来自TestMethod线程”。堆栈跟踪的后续行显示清理例程中的mouse.click调用。

请勿使用ClassCleanup方法调用UI操作。

相关问题