2

如果我记录将登录凭据输入到对话框中的操作并在 UImap1.uitests 中调用此 Submit()。自动生成的代码将如下所示:

 public void Launch()
    {
        #region Variable Declarations
        WpfEdit uIUsernameBoxEdit = this.UIOCC600OILoginWindow.UIUsernameBoxEdit;
        WpfEdit uIPasswordBoxEdit = this.UIOCC600OILoginWindow.UIPasswordBoxEdit;
        WpfButton uIOKButton = this.UIOCC600OILoginWindow.UIOKButton;
        #endregion

        // Type 'username' in 'usernameBox' text box
        uIUsernameBoxEdit.Text = this.LaunchParams.UIUsernameBoxEditText;

        // Click 'passwordBox' text box
        Mouse.Click(uIPasswordBoxEdit, new Point(63, 13));

        // Type '********' in 'passwordBox' text box
        Keyboard.SendKeys(uIPasswordBoxEdit, this.LaunchParams.UIPasswordBoxEditSendKeys, true);

        // Click 'OK' button
        Mouse.Click(uIOKButton, new Point(33, 14));
    }

现在,如果我在 CodedUI 测试类中使用 ClassInitialize 修饰的方法手动启动应用程序,如下所示:

      [ClassInitialize()]
        public static void MyTestInitialize(TestContext context)
        {
           Process.Start(@"C:\Program Files (x86)\MyCompany\MyApp.exe");
            Playback.Wait(2000);

            var uimap = new LaunchApplicationMap();
            var loginParams = uimap.EnterLoginCredentialsParams;
            loginParams.UIUsernameBoxEditText = "username";
            loginParams.UIPasswordBoxEditSendKeys = Playback.EncryptText("password
");
            uimap.Launch();
            Playback.Wait(5000);
        }

为什么我会得到如下所示的空异常?

在此处输入图像描述

这也是堆栈跟踪:

System.NullReferenceException was unhandled by user code
  Message=Object reference not set to an instance of an object.
  Source=Microsoft.VisualStudio.TestTools.UITest.Framework
  StackTrace:
       at Microsoft.VisualStudio.TestTools.UITest.Framework.UITestService.TechnologyManagerByName(String technologyName)
       at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.ValidateSearchProperties()
       at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.FindInternal()
       at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.FindControlIfNecessary()
       at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.SetProperty(String propertyName, Object value)
       at Microsoft.VisualStudio.TestTools.UITesting.WpfControls.WpfEdit.set_Text(String value)
       at UITests.UIMaps.LaunchApplicationMapClasses.LaunchApplicationMap.Launch() in C:\dev\OCC600\Source - Copy\Tests\UITests\UIMaps\LaunchApplicationMap.Designer.cs:line 44
       at UITests.LogsViewTests.MyTestInitialize(TestContext context) in C:\dev\OCC600\Source - Copy\Tests\UITests\LogsViewTests.cs:line 70
  InnerException: 

TIA。

4

1 回答 1

4

您需要初始化播放引擎以在测试方法之外使用 CodedUI。该框架会在 testinitalize/cleanup 方法中自动初始化播放/清理,因此您看不到它。

ClassInitialize/AssemblyInitialize 发生在任何测试开始之前,因此您必须调用 Playback.Initialize()。

于 2012-05-15T04:50:34.307 回答