C# 存在编码的UI控件。System.NullReferenceException

C# 存在编码的UI控件。System.NullReferenceException,c#,coded-ui-tests,C#,Coded Ui Tests,我想检查一些操作后窗口是否存在。 我尝试: 但是如果控件不存在mainWindow.Exists则抛出System.NullReferenceException。我无法理解为什么会发生这种情况,因为此代码中的mainWindow引用不能为null。 如何验证是否建立了_mainWindow 我这样做是为了等待窗口加载超时。我还尝试使用MainWindow.FindMainWindow().WaitForControlExist(100000),但它不等待所需的超时。 此代码也未设置所需的超时:

我想检查一些操作后窗口是否存在。 我尝试:

但是如果控件不存在
mainWindow.Exists
则抛出System.NullReferenceException。我无法理解为什么会发生这种情况,因为此代码中的mainWindow引用不能为null。 如何验证是否建立了_mainWindow

我这样做是为了等待窗口加载超时。我还尝试使用
MainWindow.FindMainWindow().WaitForControlExist(100000)
,但它不等待所需的超时。 此代码也未设置所需的超时:

Playback.PlaybackSettings.SearchTimeout = 100000;
Playback.PlaybackSettings.WaitForReadyTimeout = 100000;
我使用VS2013

UPD:

这是我的NRE检查代码:

protected override Boolean IsPresent()
{
    if (_mainWindow == null)
    {
        _mainWindow = new WinWindow();
        _mainWindow.SearchProperties[WinWindow.PropertyNames.ControlName] = "MainWindow";
    }
    try
    {
        return _mainWindow.TryFind(); //TODO WTF?
    }
    catch (NullReferenceException e)
    {
        Console.WriteLine("We've got a NullReferenceException");
        Console.WriteLine("_mainWindow reference is " + ((_mainWindow == null) ? "NULL" : "NOT NULL"));
        throw e;  //Line 41
    }
}
这就是结果:

We've got a NullReferenceException
_mainWindow reference is NOT NULL
Attachments:

file:///Project/TestResults/User_WIN-FP7FMM7PUB1%202017-04-09%2015_57_34/In/4acd6ac8-92ce-4746-8787-3aecfd63bdd8/WIN-FP7FMM7PUB1/SuccessLoginTest.png

Test method UITest.AutoTests.LoginTests.SuccessLoginTest threw exception: 

System.NullReferenceException: object reference not set to an instance of an object.
   in UITest.Locators.MainWindow.IsPresent() in MainWindow.cs: line 41
   in UITest.Locators.BaseWindow.Wait() in BaseWindow.cs: line 34
   in UITest.Locators.MainWindow..ctor() in MainWindow.cs: line 18
   in UITest.Locators.LoginWindow.ClickEnterButton() in LoginWindow.cs: line 57
   in UITest.AutoTests.LoginTests.SuccessLoginTest() in LoginTests.cs: line 32

“存在”不是您想要使用的。您可以改为使用TryFind()


有关如何使用编码用户界面执行各种任务的更多示例,请参见我的网站。

存在并不是您想要使用的。您可以改为使用TryFind()


有关如何使用编码UI执行各种任务的更多示例,请参见我的网站。问题出在VS2013版本中。
我已经安装了Update 5,现在如果找不到对象,TryFind()将返回False。

问题出在VS2013版本中。
我已经安装了Update 5,现在如果找不到对象,TryFind()将返回False。

\u mainWindow。TryFind()也会抛出System.NullReferenceException。您确定这就是引发NRE的地方吗?是的,此行中的异常:\u mainWindow.TryFind()。谢谢你们的回答。你们能把这句话用Try块括起来,然后抓拍一张屏幕截图吗?我很难相信这就是引发NRE的原因。我只是做了一个简单的测试,以TryFind()一些不在屏幕上的内容,它只是为我返回了false。\u mainWindow.TryFind()也引发System.NullReferenceException。你确定这就是引发NRE的地方吗?是的,这一行中的异常:\u mainWindow.TryFind()。谢谢你们的回答。你们能把这句话用Try块括起来,然后抓拍一张屏幕截图吗?我很难相信这就是抛出NRE的原因。我只是做了一个简单的测试来TryFind()一些不在屏幕上的东西,它只是简单地为我返回false。最初的例外是
\u mainWindow.Exists
给出了null。这并不意味着
\u主窗口
为空。可能是隐式
\u mainWindow.Find()
Exists
属性中的代码正在抛出。
\u main window.TryFind()
也抛出了一个事实,这表明隐式的
查找失败。这些想法让我想知道正在使用什么样的窗口。申请是如何提出的?它是标准的Windows应用程序吗?它使用自定义控件吗?编码UI对于通常的Microsoft应用程序类型非常可靠,但它(…续)依赖于这些类型中的支持代码。该支持代码仅由编码的UI(和类似的访问)使用,在正常执行中不使用。您的应用程序可能基于不正确支持编码UI的非Microsoft应用程序。最初的例外情况是,
\u mainWindow.Exists
提供空值。这并不意味着
\u主窗口
为空。可能是隐式
\u mainWindow.Find()
Exists
属性中的代码正在抛出。
\u main window.TryFind()
也抛出了一个事实,这表明隐式的
查找失败。这些想法让我想知道正在使用什么样的窗口。申请是如何提出的?它是标准的Windows应用程序吗?它使用自定义控件吗?编码UI对于通常的Microsoft应用程序类型非常可靠,但它(…续)依赖于这些类型中的支持代码。该支持代码仅由编码的UI(和类似的访问)使用,在正常执行中不使用。也许您的应用程序是基于非Microsoft的,它不正确地支持编码的UI。
We've got a NullReferenceException
_mainWindow reference is NOT NULL
Attachments:

file:///Project/TestResults/User_WIN-FP7FMM7PUB1%202017-04-09%2015_57_34/In/4acd6ac8-92ce-4746-8787-3aecfd63bdd8/WIN-FP7FMM7PUB1/SuccessLoginTest.png

Test method UITest.AutoTests.LoginTests.SuccessLoginTest threw exception: 

System.NullReferenceException: object reference not set to an instance of an object.
   in UITest.Locators.MainWindow.IsPresent() in MainWindow.cs: line 41
   in UITest.Locators.BaseWindow.Wait() in BaseWindow.cs: line 34
   in UITest.Locators.MainWindow..ctor() in MainWindow.cs: line 18
   in UITest.Locators.LoginWindow.ClickEnterButton() in LoginWindow.cs: line 57
   in UITest.AutoTests.LoginTests.SuccessLoginTest() in LoginTests.cs: line 32
 protected override Boolean IsPresent()
 {
     if (_mainWindow == null)
     {
         _mainWindow = new WinWindow();
         _mainWindow.SearchProperties[WinWindow.PropertyNames.ControlName] = "MainWindow";
     }
     return _mainWindow.TryFind();
 }