编码UI测试中的TestMethod

时间:2014-10-09 04:17:46

标签: coded-ui-tests

我在Coded UI Test中创建一个TestMethod,它将访问我的Windows Phone 8.1应用程序中的UI Control。我使用异步方法来执行多线程任务,但是我的代码有例外:

     [TestMethod]
            async public Task CodedUITestMethod1()
            {
                XamlWindow.Launch("{556EE9D4-5640-4120-9916-44B1CA27352F}:App:556ee9d4-5640-4120-9916-44b1ca27352f_tpza89sffjg1j!App");             
                await ExecuteOnUIThread(() =>
                {
                    MainPage mainPage = new MainPage();
                    Microsoft.VisualStudio.TestTools.UITest.Input.Point point = new Microsoft.VisualStudio.TestTools.UITest.Input.Point(mainPage.getX(), mainPage.getY());
                    Gesture.Tap(point);
                }
                    );
}

 public static IAsyncAction ExecuteOnUIThread(Windows.UI.Core.DispatchedHandler action)
        {
            return Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, action);
        }

但我得到的异常消息是:测试名称:CodedUITestMethod1 测试FullName:CodedUITestProject2.CodedUITest1.CodedUITestMethod1 测试源:c:\ Users \ Thanh \ Desktop \ Calculator \ CodedUITestProject2 \ CodedUITest1.cs:第28行 测试结果:失败 测试持续时间:0:00:02.3440269

结果讯息: 测试方法CodedUITestProject2.CodedUITest1.CodedUITestMethod1抛出异常: System.InvalidOperationException:在意外时间调用了一个方法。

在意外时间调用了一个方法。 结果StackTrace:
在Windows.ApplicationModel.Core.CoreApplication.get_MainView()    在CodedUITestProject2.CodedUITest1.ExecuteOnUIThread(DispatchedHandler action)    在CodedUITestProject2.CodedUITest1.d__2.MoveNext() ---从抛出异常的先前位置开始的堆栈跟踪结束---    在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)    在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)
那么我该如何解决这个问题呢?请帮帮我。

1 个答案:

答案 0 :(得分:0)

编码的UI测试旨在由Visual Studio中的Coded UI测试控制机制或相关程序(如mstest.exe)调用。我见过的所有Coded UI测试都有如下代码:

[TestMethod]
public void CodedUITestMethod1() { ... }

他们也可能拥有[DataSource(...)]属性。您尝试的方法有一个非常不同的类型:

[TestMethod] async public Task CodedUITestMethod1() { ... }

我没有看到任何文档或示例表明此类方法可以与Coded UI一起使用。

XamlWindow.Launch(...)内调用[TestMethod]表示您正在尝试将编码的UI测试和应用程序一起构建到一个程序中,然后通过调用该方法中的方法来获取测试以调用该应用程序程序。这不是Coded UI的用途。编码的UI测试套件应该作为单独的程序(实际上是由Visual Studio调用的DLL,由mstest.exe或类似的)从正在测试的应用程序创建。然后,编码的UI将应用程序作为程序运行(而不是通过调用应用程序中的方法)。然后,编码的UI测试通过阅读屏幕并通过发送键盘输入和鼠标点击程序与程序进行交互。